All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.language.sentencepiece.SentencePieceConfig Maven / Gradle / Ivy

The newest version!
// ------------   D O   N O T   E D I T !   ------------
// This file is generated from a config definition file.

package com.yahoo.language.sentencepiece;

import java.util.*;
import java.io.File;
import java.nio.file.Path;
import com.yahoo.config.*;

/**
 * This class represents the root node of sentence-piece
 *
 * Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
 * Configures com.yahoo.language.sentencepiece.SentencePieceEmbedder
 */
public final class SentencePieceConfig extends ConfigInstance {

  public final static String CONFIG_DEF_MD5 = "53e079539fe07490484363d3a48a84b1";
  public final static String CONFIG_DEF_NAME = "sentence-piece";
  public final static String CONFIG_DEF_NAMESPACE = "language.sentencepiece";
  public final static String[] CONFIG_DEF_SCHEMA = {
    "namespace=language.sentencepiece",
    "collapseUnknowns bool default=true",
    "scoring enum { highestScore, fewestSegments } default=fewestSegments",
    "model[].language string",
    "model[].path path"
  };

  public static String getDefMd5()       { return CONFIG_DEF_MD5; }
  public static String getDefName()      { return CONFIG_DEF_NAME; }
  public static String getDefNamespace() { return CONFIG_DEF_NAMESPACE; }

  public interface Producer extends ConfigInstance.Producer {
    void getConfig(Builder builder);
  }

  public static final class Builder implements ConfigInstance.Builder {
    private Set __uninitialized = new HashSet();

    private Boolean collapseUnknowns = null;
    private Scoring.Enum scoring = null;
    public List model = new ArrayList<>();

    public Builder() { }

    public Builder(SentencePieceConfig config) {
      collapseUnknowns(config.collapseUnknowns());
      scoring(config.scoring());
      for (Model m : config.model()) {
        model(new Model.Builder(m));
      }
    }

    private Builder override(Builder __superior) {
      if (__superior.collapseUnknowns != null)
        collapseUnknowns(__superior.collapseUnknowns);
      if (__superior.scoring != null)
        scoring(__superior.scoring);
      if (!__superior.model.isEmpty())
        model.addAll(__superior.model);
      return this;
    }

    public Builder collapseUnknowns(boolean __value) {
      collapseUnknowns = __value;
      return this;
    }

    private Builder collapseUnknowns(String __value) {
      return collapseUnknowns(Boolean.valueOf(__value));
    }

    public Builder scoring(Scoring.Enum __value) {
    if (__value == null) throw new IllegalArgumentException("Null value is not allowed.");
      scoring = __value;
      return this;
    }

    private Builder scoring(String __value) {
      return scoring(Scoring.Enum.valueOf(__value));
    }

    /**
     * Add the given builder to this builder's list of Model builders
     * @param __builder a builder
     * @return this builder
     */
    public Builder model(Model.Builder __builder) {
      model.add(__builder);
      return this;
    }

    /**
     * Make a new builder and run the supplied function on it before adding it to the list
     * @param __func lambda that modifies the given builder
     * @return this builder
     */
    public Builder model(java.util.function.Consumer __func) {
      Model.Builder __inner = new Model.Builder();
      __func.accept(__inner);
      model.add(__inner);
      return this;
    }

    /**
     * Set the given list as this builder's list of Model builders
     * @param __builders a list of builders
     * @return this builder
     */
    public Builder model(List __builders) {
      model = __builders;
      return this;
    }

    private boolean _applyOnRestart = false;

    @java.lang.Override
    public final boolean dispatchGetConfig(ConfigInstance.Producer producer) {
      if (producer instanceof Producer) {
        ((Producer)producer).getConfig(this);
        return true;
      }
      return false;
    }

    @java.lang.Override
    public final String getDefMd5() { return CONFIG_DEF_MD5; }

    @java.lang.Override
    public final String getDefName() { return CONFIG_DEF_NAME; }

    @java.lang.Override
    public final String getDefNamespace() { return CONFIG_DEF_NAMESPACE; }

    @java.lang.Override
    public final boolean getApplyOnRestart() { return _applyOnRestart; }

    @java.lang.Override
    public final void setApplyOnRestart(boolean applyOnRestart) { _applyOnRestart = applyOnRestart; }

    public SentencePieceConfig build() {
      return new SentencePieceConfig(this);
    }

  }

  // Whether consecutive unknown character should be collapsed into one large unknown token (default
  // or be returned as single character tokens.
  private final BooleanNode collapseUnknowns;
  // The scoring strategy to use when picking a segmentation.
  private final Scoring scoring;
  private final InnerNodeVector model;

  public SentencePieceConfig(Builder builder) {
    this(builder, true);
  }

  private SentencePieceConfig(Builder builder, boolean throwIfUninitialized) {
    if (throwIfUninitialized && ! builder.__uninitialized.isEmpty())
      throw new IllegalArgumentException("The following builder parameters for " +
          "sentence-piece must be initialized: " + builder.__uninitialized);

    collapseUnknowns = (builder.collapseUnknowns == null) ?
        new BooleanNode(true) : new BooleanNode(builder.collapseUnknowns);
    scoring = (builder.scoring == null) ?
        new Scoring(Scoring.fewestSegments) : new Scoring(builder.scoring);
    model = Model.createVector(builder.model);
  }

  /**
   * @return sentence-piece.collapseUnknowns
   */
  public boolean collapseUnknowns() {
    return collapseUnknowns.value();
  }

  /**
   * @return sentence-piece.scoring
   */
  public Scoring.Enum scoring() {
    return scoring.value();
  }

  /**
   * @return sentence-piece.model[]
   */
  public List model() {
    return model;
  }

  /**
   * @param i the index of the value to return
   * @return sentence-piece.model[]
   */
  public Model model(int i) {
    return model.get(i);
  }

  private ChangesRequiringRestart getChangesRequiringRestart(SentencePieceConfig newConfig) {
    ChangesRequiringRestart changes = new ChangesRequiringRestart("sentence-piece");
    return changes;
  }

  private static boolean containsFieldsFlaggedWithRestart() {
    return false;
  }

  /**
   * This class represents sentence-piece.scoring
   * 
   * The scoring strategy to use when picking a segmentation.
   */
  public final static class Scoring extends EnumNode {

    public Scoring(){
      this.value = null;
    }

    public Scoring(Enum enumValue) {
      super(enumValue != null);
      this.value = enumValue;
    }

    public enum Enum {highestScore, fewestSegments}
    public final static Enum highestScore = Enum.highestScore;
    public final static Enum fewestSegments = Enum.fewestSegments;

    @Override
    protected boolean doSetValue(String name) {
      try {
        value = Enum.valueOf(name);
        return true;
      } catch (IllegalArgumentException e) {
      }
      return false;
    }
  }

  /**
   * This class represents sentence-piece.model[]
   */
  public final static class Model extends InnerNode { 

    public static final class Builder implements ConfigBuilder {
      private Set __uninitialized = new HashSet(List.of(
        "language",
        "path"
        ));

      private String language = null;
      private FileReference path = null;

      public Builder() { }

      public Builder(Model config) {
        language(config.language());
        path(config.path.getFileReference());
      }

      private Builder override(Builder __superior) {
        if (__superior.language != null)
          language(__superior.language);
        if (__superior.path != null)
          path(__superior.path);
        return this;
      }

      public Builder language(String __value) {
      if (__value == null) throw new IllegalArgumentException("Null value is not allowed.");
        language = __value;
        __uninitialized.remove("language");
        return this;
      }


      public Builder path(FileReference __value) {
      if (__value == null) throw new IllegalArgumentException("Null value is not allowed.");
        path = __value;
        __uninitialized.remove("path");
        return this;
      }


      public Model build() {
        return new Model(this);
      }

    }

    // The language a model is for, one of the language tags in com.yahoo.language.Language.
    // Use "unknown" for models to be used with any language.
    private final StringNode language;
    // The path to the model relative to the application package root
    private final PathNode path;

    public Model(Builder builder) {
      this(builder, true);
    }

    private Model(Builder builder, boolean throwIfUninitialized) {
      if (throwIfUninitialized && ! builder.__uninitialized.isEmpty())
        throw new IllegalArgumentException("The following builder parameters for " +
            "sentence-piece.model[] must be initialized: " + builder.__uninitialized);

      language = (builder.language == null) ?
          new StringNode() : new StringNode(builder.language);
      path = (builder.path == null) ?
          new PathNode() : new PathNode(builder.path);
    }

    /**
     * @return sentence-piece.model[].language
     */
    public String language() {
      return language.value();
    }

    /**
     * @return sentence-piece.model[].path
     */
    public Path path() {
      return path.value();
    }

    private ChangesRequiringRestart getChangesRequiringRestart(Model newConfig) {
      ChangesRequiringRestart changes = new ChangesRequiringRestart("model");
      return changes;
    }

    private static InnerNodeVector createVector(List builders) {
        List elems = new ArrayList<>();
        for (Builder b : builders) {
            elems.add(new Model(b));
        }
        return new InnerNodeVector(elems);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy