我希望传递一个字符串来解析并返回具有最高评级的数字(1-5)。
例如,这将返回2:
答案 0 :(得分:0)
我假设您正在使用带有sentiment
annotator的Stanford CoreNLP管道。
此注释器在每个名为SentimentCoreAnnotations.ClassName
的句子上设置注释。只需检索此值:(未经测试的示例)
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// read some text in the text variable
String text = ... // Add your text here!
// create an empty Annotation just with the given text
Annotation document = new Annotation(text);
// run all Annotators on this text
pipeline.annotate(document);
// these are all the sentences in this document
// a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
String sentimentLabel = sentence.get(SentimentCoreAnnotations.ClassName.class);
// ...
}