summaryrefslogtreecommitdiffstats
path: root/linguistics
diff options
context:
space:
mode:
authorLester Solbakken <lesters@oath.com>2022-03-21 14:13:12 +0100
committerLester Solbakken <lesters@oath.com>2022-03-21 14:13:12 +0100
commit24555fae4aac0dadde821cac0b7cf85321027bce (patch)
treee323a494f1c326929d940345fc822848475c110b /linguistics
parentd0f6d0105b1a21f4871bdd347487c267399327c3 (diff)
Add convenience function to represent embedder as map
Diffstat (limited to 'linguistics')
-rw-r--r--linguistics/abi-spec.json4
-rw-r--r--linguistics/src/main/java/com/yahoo/language/process/Embedder.java29
2 files changed, 30 insertions, 3 deletions
diff --git a/linguistics/abi-spec.json b/linguistics/abi-spec.json
index 910056286ec..76fc071f440 100644
--- a/linguistics/abi-spec.json
+++ b/linguistics/abi-spec.json
@@ -354,6 +354,7 @@
],
"methods": [
"public void <init>()",
+ "public void <init>(java.lang.String)",
"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)"
],
@@ -368,10 +369,13 @@
"abstract"
],
"methods": [
+ "public java.util.Map asMap()",
+ "public java.util.Map asMap(java.lang.String)",
"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 java.lang.String defaultEmbedderName",
"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 dd9c3847314..fe9fbcd727b 100644
--- a/linguistics/src/main/java/com/yahoo/language/process/Embedder.java
+++ b/linguistics/src/main/java/com/yahoo/language/process/Embedder.java
@@ -3,10 +3,10 @@ package com.yahoo.language.process;
import com.yahoo.language.Language;
import com.yahoo.tensor.Tensor;
-import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import java.util.List;
+import java.util.Map;
/**
* An embedder converts a text string to a tensor
@@ -15,9 +15,22 @@ import java.util.List;
*/
public interface Embedder {
+ /** Name of embedder when none is explicity given */
+ String defaultEmbedderName = "defaultEmbedder";
+
/** An instance of this which throws IllegalStateException if attempted used */
Embedder throwsOnUse = new FailingEmbedder();
+ /** Returns this embedder instance as a map with the default embedder name */
+ default Map<String, Embedder> asMap() {
+ return asMap(defaultEmbedderName);
+ }
+
+ /** Returns this embedder instance as a map with the given name */
+ default Map<String, Embedder> asMap(String name) {
+ return Map.of(name, this);
+ }
+
/**
* Converts text into a list of token id's (a vector embedding)
*
@@ -82,14 +95,24 @@ public interface Embedder {
class FailingEmbedder implements Embedder {
+ private final String message;
+
+ public FailingEmbedder() {
+ this("No embedder has been configured");
+ }
+
+ public FailingEmbedder(String message) {
+ this.message = message;
+ }
+
@Override
public List<Integer> embed(String text, Context context) {
- throw new IllegalStateException("No embedder has been configured");
+ throw new IllegalStateException(message);
}
@Override
public Tensor embed(String text, Context context, TensorType tensorType) {
- throw new IllegalStateException("No embedder has been configured");
+ throw new IllegalStateException(message);
}
}