jzonthemtn commented on code in PR #523:
URL: https://github.com/apache/opennlp/pull/523#discussion_r1154812833


##########
opennlp-dl/src/main/java/opennlp/dl/vectors/SentenceVectorsDL.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.dl.vectors;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.LongBuffer;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import ai.onnxruntime.OnnxTensor;
+import ai.onnxruntime.OrtEnvironment;
+import ai.onnxruntime.OrtException;
+import ai.onnxruntime.OrtSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import opennlp.dl.AbstractDL;
+import opennlp.dl.Tokens;
+import opennlp.tools.tokenize.Tokenizer;
+import opennlp.tools.tokenize.WordpieceTokenizer;
+
+/**
+ * Facilitates the generation of sentence vectors using
+ * a sentence-transformers model converted to ONNX.
+ */
+public class SentenceVectorsDL extends AbstractDL {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(SentenceVectorsDL.class);
+
+  /**
+   * Creates an instance of the class.
+   * @param model The file name of a sentence vectors ONNX model.
+   * @param vocabulary The file name of the vocabulary file for the model.
+   * @throws OrtException Thrown if the model cannot be loaded.
+   * @throws IOException Thrown if the vocabulary file cannot be loaded.
+   */
+  public SentenceVectorsDL(final File model, final File vocabulary)
+      throws OrtException, IOException {
+
+    env = OrtEnvironment.getEnvironment();
+    session = env.createSession(model.getPath(), new 
OrtSession.SessionOptions());
+    vocab = loadVocab(new File(vocabulary.getPath()));
+    tokenizer = new WordpieceTokenizer(vocab.keySet());

Review Comment:
   Not a stupid question at all -- it does not. It uses a WordPiece-based 
tokenizer. We still need an implementation of `Tokenizer` that can use a 
pre-trained model. The sentence transformers tokenizer 
[models](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/blob/main/tokenizer_config.json)
 were made using `BertTokenizer` which is based on WordPiece. When I tested 
this branch I did a simplified vector search by generating vectors for multiple 
sentences. I think used those vectors to find the most related sentences. 
Visually, the results made sense, but that's not to say there are lots of ways 
to improve.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to