diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-10-01 11:09:08 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-10-01 11:09:08 +0200 |
commit | ac2519a8842a6397e4abd434439e9dddd2924394 (patch) | |
tree | 792275efbb88966a27a7ce54cc31465b563d7ad0 /linguistics | |
parent | 380b9fa780ead9bcce0e824f7b6ee305e37dec43 (diff) |
Encapsulate in a context
Diffstat (limited to 'linguistics')
-rw-r--r-- | linguistics/abi-spec.json | 23 | ||||
-rw-r--r-- | linguistics/src/main/java/com/yahoo/language/process/Embedder.java | 58 |
2 files changed, 65 insertions, 16 deletions
diff --git a/linguistics/abi-spec.json b/linguistics/abi-spec.json index 5865c28bbb6..31612bea983 100644 --- a/linguistics/abi-spec.json +++ b/linguistics/abi-spec.json @@ -328,6 +328,21 @@ ], "fields": [] }, + "com.yahoo.language.process.Embedder$Context": { + "superClass": "java.lang.Object", + "interfaces": [], + "attributes": [ + "public" + ], + "methods": [ + "public void <init>(java.lang.String)", + "public com.yahoo.language.Language getLanguage()", + "public com.yahoo.language.process.Embedder$Context setLanguage(com.yahoo.language.Language)", + "public java.lang.String getDestination()", + "public com.yahoo.language.process.Embedder$Context setDestination(java.lang.String)" + ], + "fields": [] + }, "com.yahoo.language.process.Embedder$FailingEmbedder": { "superClass": "java.lang.Object", "interfaces": [ @@ -338,8 +353,8 @@ ], "methods": [ "public void <init>()", - "public java.util.List embed(java.lang.String, com.yahoo.language.Language, java.lang.String)", - "public com.yahoo.tensor.Tensor embed(java.lang.String, com.yahoo.language.Language, java.lang.String, com.yahoo.tensor.TensorType)" + "public java.util.List embed(java.lang.String, com.yahoo.language.process.Embedder$Context)", + "public com.yahoo.tensor.Tensor embed(java.lang.String, com.yahoo.language.process.Embedder$Context, com.yahoo.tensor.TensorType)" ], "fields": [] }, @@ -352,8 +367,8 @@ "abstract" ], "methods": [ - "public abstract java.util.List embed(java.lang.String, com.yahoo.language.Language, java.lang.String)", - "public abstract com.yahoo.tensor.Tensor embed(java.lang.String, com.yahoo.language.Language, java.lang.String, com.yahoo.tensor.TensorType)" + "public abstract java.util.List embed(java.lang.String, com.yahoo.language.process.Embedder$Context)", + "public abstract com.yahoo.tensor.Tensor embed(java.lang.String, com.yahoo.language.process.Embedder$Context, com.yahoo.tensor.TensorType)" ], "fields": [ "public static final com.yahoo.language.process.Embedder throwsOnUse" diff --git a/linguistics/src/main/java/com/yahoo/language/process/Embedder.java b/linguistics/src/main/java/com/yahoo/language/process/Embedder.java index 1f4473220d7..17ee0419cea 100644 --- a/linguistics/src/main/java/com/yahoo/language/process/Embedder.java +++ b/linguistics/src/main/java/com/yahoo/language/process/Embedder.java @@ -21,39 +21,73 @@ public interface Embedder { * Converts text into a list of token id's (a vector embedding) * * @param text the text to embed - * @param language the language of the text, or UNKNOWN to use language independent embedding - * @param destination the name of the recipient of this tensor, either a query feature name - * ("query(feature)"), or a schema and field name concatenated by a dot ("schema.field"). - * This is useful for embedder components that alters behavior depending on the receiver. + * @param context the context which may influence an embedder's behavior * @return the text embedded as a list of token ids * @throws IllegalArgumentException if the language is not supported by this embedder */ - List<Integer> embed(String text, Language language, String destination); + List<Integer> embed(String text, Context context); /** * Converts text into tokens in a tensor. * The information contained in the embedding may depend on the tensor type. * * @param text the text to embed - * @param language the language of the text, or UNKNOWN to use language independent embedding - * @param destination the name of the recipient of this tensor, either a query feature name - * ("query(feature)"), or a schema and field name concatenated by a dot ("schema.field"). - * This is useful for embedder components that alters behavior depending on the receiver. + * @param context the context which may influence an embedder's behavior * @param tensorType the type of the tensor to be returned * @return the tensor embedding of the text, as the spoecified tensor type * @throws IllegalArgumentException if the language or tensor type is not supported by this embedder */ - Tensor embed(String text, Language language, String destination, TensorType tensorType); + Tensor embed(String text, Context context, TensorType tensorType); + + class Context { + + private Language language = Language.UNKNOWN; + private String destination; + + public Context(String destination) { + this.destination = destination; + } + + /** Returns the language of the text, or UNKNOWN (default) to use a language independent embedding */ + public Language getLanguage() { return language; } + + /** Sets the language of the text, or UNKNOWN to use language independent embedding */ + public Context setLanguage(Language language) { + this.language = language; + return this; + } + + /** + * Returns the name of the recipient of this tensor. + * + * This is either a query feature name + * ("query(feature)"), or a schema and field name concatenated by a dot ("schema.field"). + * This cannot be null. + */ + public String getDestination() { return destination; } + + /** + * Sets the name of the recipient of this tensor. + * + * This iseither a query feature name + * ("query(feature)"), or a schema and field name concatenated by a dot ("schema.field"). + */ + public Context setDestination(String destination) { + this.destination = destination; + return this; + } + + } class FailingEmbedder implements Embedder { @Override - public List<Integer> embed(String text, Language language, String destination) { + public List<Integer> embed(String text, Context context) { throw new IllegalStateException("No embedder has been configured"); } @Override - public Tensor embed(String text, Language language, String destination, TensorType tensorType) { + public Tensor embed(String text, Context context, TensorType tensorType) { throw new IllegalStateException("No embedder has been configured"); } |