Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sentiment attributes to XML output #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/pipeline/MorphaAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void annotate(Annotation annotation) {
for (CoreLabel token : tokens) {
String text = token.get(CoreAnnotations.TextAnnotation.class);
String posTag = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
this.addLemma(morphology, CoreAnnotations.LemmaAnnotation.class, token, text, posTag);
addLemma(morphology, CoreAnnotations.LemmaAnnotation.class, token, text, posTag);
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/pipeline/StanfordCoreNLP.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
annotators = tokenize, ssplit, pos, lemma, ner, parse, dcoref
annotators = tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment

# A true-casing annotator is also available (see below)
#annotators = tokenize, ssplit, pos, lemma, truecase
Expand Down
26 changes: 25 additions & 1 deletion src/edu/stanford/nlp/pipeline/XMLOutputter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.stats.Counters;
import edu.stanford.nlp.time.TimeAnnotations;
import edu.stanford.nlp.time.Timex;
Expand All @@ -26,6 +27,7 @@
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Pair;
import edu.stanford.nlp.util.StringUtils;
Expand Down Expand Up @@ -121,6 +123,9 @@ public static Document annotationToDoc(Annotation annotation, Options options) {
//
if(annotation.get(CoreAnnotations.SentencesAnnotation.class) != null){
int sentCount = 1;
int totalSentiment = 0;
// keep track separately to accommodate the chance that a sentence doesn't have sentiment.
int sentimentSentCount = 0;
for (CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Element sentElem = new Element("sentence", NAMESPACE_URI);
sentElem.addAttribute(new Attribute("id", Integer.toString(sentCount)));
Expand Down Expand Up @@ -183,12 +188,31 @@ public static Document annotationToDoc(Annotation annotation, Options options) {

sentElem.appendChild(mrElem);
}

/**
* Adds sentiment as an attribute of this sentence.
*/
Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
if(sentimentTree != null){
int sentiment = RNNCoreAnnotations.getPredictedClass(sentimentTree);
totalSentiment += sentiment;
sentimentSentCount++;
sentElem.addAttribute(new Attribute("sentiment", Integer.toString(sentiment)));
}

// add the sentence to the root
sentencesElem.appendChild(sentElem);
}
/**
* Provides an average sentiment value for the document as a whole.
*/
if(sentimentSentCount > 0){
// shift the scale from 0-4 to 1-5 to avoid dividing by zero, and then shift it back
// note that we're only computing against sentences that presented with sentiment
double averageSentiment = (((double) totalSentiment + sentimentSentCount) / ((double) sentimentSentCount)) - 1.0;
sentencesElem.addAttribute(new Attribute("averageSentiment", Double.toString(averageSentiment)));
}
}

//
// add the coref graph
//
Expand Down