rzo1 opened a new pull request, #998:
URL: https://github.com/apache/opennlp/pull/998
### For all changes:
- [x] Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
- [x] Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA
number you are trying to resolve? Pay particular attention to the hyphen "-"
character.
- [x] Has your PR been rebased against the latest commit within the target
branch (typically main)?
- [x] Is your initial contribution a single, squashed commit?
### For code changes:
- [x] Have you ensured that the full suite of tests is executed via mvn
clean install at the root opennlp folder?
- [x] Have you written or updated unit tests to verify your changes?
- [ ] If adding new dependencies to the code, are these dependencies
licensed in a way that is compatible for inclusion under [ASF
2.0](http://www.apache.org/legal/resolved.html#category-a)?
- [ ] If applicable, have you updated the LICENSE file, including the main
LICENSE file in opennlp folder?
- [ ] If applicable, have you updated the NOTICE file, including the main
NOTICE file found in opennlp folder?
### For documentation related changes:
- [ ] Have you ensured that format looks appropriate for the output in which
it is rendered?
### Note:
Converted the model mentioned in the issue to ONXX first. Did run a test via
```
public class RobertaGoEmotionsTest {
private static final String MODEL_DIR = "roberta-base-go_emotions";
public static void main(String[] args) throws Exception {
final File modelDir = new File(MODEL_DIR);
if (!modelDir.exists()) {
System.err.println("Model directory not found: " +
modelDir.getAbsolutePath());
System.err.println("Clone it with: git clone
https://huggingface.co/SamLowe/roberta-base-go_emotions");
return;
}
final File onnxModel = new File(modelDir, "model.onnx");
if (!onnxModel.exists()) {
System.err.println("ONNX model file not found: " +
onnxModel.getAbsolutePath());
System.err.println("Download it from:
https://huggingface.co/SamLowe/roberta-base-go_emotions/resolve/main/onnx/model.onnx");
return;
}
final File vocabFile = new File(modelDir, "vocab.json");
final File configFile = new File(modelDir, "config.json");
// RoBERTa does not use token_type_ids
final InferenceOptions inferenceOptions = new InferenceOptions();
inferenceOptions.setIncludeTokenTypeIds(false);
try (DocumentCategorizerDL categorizer = new DocumentCategorizerDL(
onnxModel, vocabFile, configFile,
new AverageClassificationScoringStrategy(),
inferenceOptions)) {
final String[] testSentences = {
"I am so happy today!",
"This is terrible and I hate it.",
"Thank you so much for your help!",
"I'm not sure what to think about this.",
"That's hilarious, I can't stop laughing!"
};
for (final String sentence : testSentences) {
System.out.println("Input: \"" + sentence + "\"");
final SortedMap<Double, Set<String>> scores =
categorizer.sortedScoreMap(new String[] {sentence});
System.out.println(" Top emotions:");
final List<Double> sortedScores = new ArrayList<>(scores.keySet());
Collections.reverse(sortedScores);
int count = 0;
for (final Double score : sortedScores) {
if (count++ >= 3) break;
System.out.printf(" %-20s %.4f%n", scores.get(score), score);
}
System.out.println();
}
}
}
}
```
This will fail with the unpatched version but will work with the patch:
```
Input: "I am so happy today!"
Top emotions:
[joy] 0,8805
[excitement] 0,0736
[neutral] 0,0105
Input: "This is terrible and I hate it."
Top emotions:
[anger] 0,6766
[annoyance] 0,1317
[disapproval] 0,1008
Input: "Thank you so much for your help!"
Top emotions:
[gratitude] 0,9992
[approval] 0,0001
[admiration] 0,0001
Input: "I'm not sure what to think about this."
Top emotions:
[confusion] 0,9703
[neutral] 0,0082
[disapproval] 0,0025
Input: "That's hilarious, I can't stop laughing!"
Top emotions:
[amusement] 0,7920
[joy] 0,1405
[neutral] 0,0279
```
Note: Since we do not use a JSON library (like jackson) here, I used a regex
approach with a quirky check to parse a json based vocab file. The original
issue failed due to missing Roberta special tokens.
--
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]