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

Sentiment attributes(sentimentValue, sentiment) were added to token tag #10

Closed
wants to merge 1 commit into from
Closed
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
53 changes: 42 additions & 11 deletions src/edu/stanford/nlp/pipeline/XMLOutputter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.ProcessingInstruction;
import nu.xom.Serializer;
import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations;
import edu.stanford.nlp.ie.machinereading.structure.EntityMention;
Expand All @@ -16,22 +22,22 @@
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.rnn.RNNCoreAnnotations;
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.sentiment.SentimentUtils;
import edu.stanford.nlp.stats.Counters;
import edu.stanford.nlp.time.TimeAnnotations;
import edu.stanford.nlp.time.Timex;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreePrint;
import edu.stanford.nlp.trees.TreeCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.trees.TreePrint;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Pair;
import edu.stanford.nlp.util.StringUtils;
import nu.xom.*;


public class XMLOutputter {
Expand Down Expand Up @@ -132,12 +138,17 @@ public static Document annotationToDoc(Annotation annotation, Options options) {
}
sentCount ++;

Map<Integer, Integer> sentimentScores = new HashMap<Integer, Integer>(0);
Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
getSentimentScores(sentimentTree, null, sentimentScores);


// add the word table with all token-level annotations
Element wordTable = new Element("tokens", NAMESPACE_URI);
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
for(int j = 0; j < tokens.size(); j ++){
Element wordInfo = new Element("token", NAMESPACE_URI);
addWordInfo(wordInfo, tokens.get(j), j + 1, NAMESPACE_URI);
addWordInfo(wordInfo, tokens.get(j), j + 1, NAMESPACE_URI, sentimentScores);
wordTable.appendChild(wordInfo);
}
sentElem.appendChild(wordTable);
Expand Down Expand Up @@ -190,12 +201,11 @@ public static Document annotationToDoc(Annotation annotation, Options options) {
/**
* Adds sentiment as an attribute of this sentence.
*/
Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);

if (sentimentTree != null) {
int sentiment = RNNCoreAnnotations.getPredictedClass(sentimentTree);
sentElem.addAttribute(new Attribute("sentimentValue", Integer.toString(sentiment)));
String sentimentClass = sentence.get(SentimentCoreAnnotations.ClassName.class);
sentElem.addAttribute(new Attribute("sentiment", sentimentClass.replaceAll(" ", "")));
sentElem.addAttribute(new Attribute("sentiment", SentimentUtils.sentimentString(sentiment).replaceAll(" ", "")));
}


Expand Down Expand Up @@ -223,6 +233,20 @@ public static Document annotationToDoc(Annotation annotation, Options options) {
return xmlDoc;
}


private static void getSentimentScores(Tree rootTree, Tree parentTree, Map<Integer, Integer> scores) {
if (rootTree.isLeaf() && parentTree != null) {
scores.put(scores.size() +1, RNNCoreAnnotations.getPredictedClass(parentTree));
return;
}

for (Tree child : rootTree.children()) {
getSentimentScores(child, rootTree, scores);
}

}


/**
* Generates the XML content for a constituent tree
*/
Expand Down Expand Up @@ -378,9 +402,15 @@ private static void addCorefMention(Options options,
chainElem.appendChild(mentionElem);
}

private static void addWordInfo(Element wordInfo, CoreMap token, int id, String curNS) {
private static void addWordInfo(Element wordInfo, CoreMap token, int id, String curNS, Map<Integer, Integer> sentimentScores) {
// store the position of this word in the sentence
wordInfo.addAttribute(new Attribute("id", Integer.toString(id)));

if (sentimentScores.containsKey(id)) {
wordInfo.addAttribute(new Attribute("sentimentValue", Integer.toString(sentimentScores.get(id))));
wordInfo.addAttribute(new Attribute("sentiment", SentimentUtils.sentimentString(sentimentScores.get(id)).replaceAll(" ", "")));
}


setSingleElement(wordInfo, "word", curNS, token.get(CoreAnnotations.TextAnnotation.class));
setSingleElement(wordInfo, "lemma", curNS, token.get(CoreAnnotations.LemmaAnnotation.class));
Expand All @@ -394,6 +424,7 @@ private static void addWordInfo(Element wordInfo, CoreMap token, int id, String
setSingleElement(wordInfo, "POS", curNS, token.get(CoreAnnotations.PartOfSpeechAnnotation.class));
}


if (token.containsKey(CoreAnnotations.NamedEntityTagAnnotation.class)) {
setSingleElement(wordInfo, "NER", curNS, token.get(CoreAnnotations.NamedEntityTagAnnotation.class));
}
Expand Down