Wenn ich Eingabe der Satz:Falsche Ausgabe bei der Verwendung von Stanford CoreNLP Sentiment Analysis
„So aufgeregt, zurück zu sein Wir sind hier mit & treffen neue Innovatoren bei ghc16 wieder zu verbinden!“
Dann ist die Stimmung, die zurückgegeben wird, negativ. Ich kann den Grund nicht verstehen, warum das passiert. Die Aussage ist positiv, liefert aber immer noch einen negativen Wert.
class SentimentAnalyzer {
public TweetWithSentiment findSentiment(String line) {
if(line == null || line.isEmpty()) {
throw new IllegalArgumentException("The line must not be null or empty.");
}
Annotation annotation = processLine(line);
int mainSentiment = findMainSentiment(annotation);
if(mainSentiment < 0 || mainSentiment > 4) { //You should avoid magic numbers like 2 or 4 try to create a constant that will provide a description why 2
return null; //You should avoid null returns
}
TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line, toCss(mainSentiment));
return tweetWithSentiment;
}
private String toCss(int sentiment) {
switch (sentiment) {
case 0:
return "very negative";
case 1:
return "negative";
case 2:
return "neutral";
case 3:
return "positive";
case 4:
return "very positive";
default:
return "default";
}
}
private int findMainSentiment(Annotation annotation) {
int mainSentiment = Integer.MIN_VALUE;
int longest = Integer.MIN_VALUE;
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
String word = token.get(CoreAnnotations.TextAnnotation.class);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
System.out.println("word: " + word);
System.out.println("pos: " + pos);
System.out.println("ne: " + ne);
System.out.println("Lemmas: " + lemma);
}
int sentenceLength = String.valueOf(sentence).length();
if(sentenceLength > longest) {
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
mainSentiment = RNNCoreAnnotations.getPredictedClass(tree);
longest = sentenceLength ;
}
}
return mainSentiment;
}
private Annotation processLine(String line) {
StanfordCoreNLP pipeline = createPieline();
return pipeline.process(line);
}
private StanfordCoreNLP createPieline() {
Properties props = createPipelineProperties();
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
return pipeline;
}
private Properties createPipelineProperties() {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment");
return props;
}
}
also bedeutet das, dass '&' als negativ interpretiert wird und gibt es andere Symbole, die ähnliche Probleme haben? –
Es ist nicht so, dass sie negativ sind, aber ihre Anwesenheit ist verwirrend und die Software kann nicht verstehen, was das bedeutet. Diese Verwirrung führt dazu, dass nlp zum negativen Pfad neigt (mit der Abwesenheit von ** you **, was es noch verwirrender macht). Der Punkt ist: Wenn Sie können, ersetzen Sie spezielle Zeichen. :) – bosco