From 24555fae4aac0dadde821cac0b7cf85321027bce Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Mon, 21 Mar 2022 14:13:12 +0100 Subject: Add convenience function to represent embedder as map --- linguistics/abi-spec.json | 4 +++ .../java/com/yahoo/language/process/Embedder.java | 29 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'linguistics') 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 ()", + "public void (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 asMap() { + return asMap(defaultEmbedderName); + } + + /** Returns this embedder instance as a map with the given name */ + default Map 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 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); } } -- cgit v1.2.3