-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPENNLP-1442: Sentence transformers (#523)
* OPENNLP-1442: Adding sentence transformer model support via ONNX Runtime.
- Loading branch information
1 parent
14e0e47
commit 9b62d58
Showing
7 changed files
with
278 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
|
||
import ai.onnxruntime.OrtEnvironment; | ||
import ai.onnxruntime.OrtSession; | ||
|
||
import opennlp.tools.tokenize.Tokenizer; | ||
|
||
/** | ||
* Base class for OpenNLP deep-learning classes using ONNX Runtime. | ||
*/ | ||
public abstract class AbstractDL { | ||
|
||
public static final String INPUT_IDS = "input_ids"; | ||
public static final String ATTENTION_MASK = "attention_mask"; | ||
public static final String TOKEN_TYPE_IDS = "token_type_ids"; | ||
|
||
protected OrtEnvironment env; | ||
protected OrtSession session; | ||
protected Tokenizer tokenizer; | ||
protected Map<String, Integer> vocab; | ||
|
||
/** | ||
* Loads a vocabulary file from disk. | ||
* @param vocabFile The vocabulary file. | ||
* @return A map of vocabulary words to integer IDs. | ||
* @throws IOException Thrown if the vocabulary file cannot be opened and read. | ||
*/ | ||
public Map<String, Integer> loadVocab(final File vocabFile) throws IOException { | ||
|
||
final Map<String, Integer> vocab = new HashMap<>(); | ||
|
||
final AtomicInteger counter = new AtomicInteger(0); | ||
|
||
try (Stream<String> lines = Files.lines(Path.of(vocabFile.getPath()))) { | ||
|
||
lines.forEach(line -> { | ||
vocab.put(line, counter.getAndIncrement()); | ||
}); | ||
|
||
} | ||
|
||
return vocab; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.