package pkg;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.ValueAnnotation;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.BasicDependenciesAnnotation;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.util.CoreMap;
public class Main {
public static void main(String[] args) {
String text = "I bouht a new red Nissan car for my family.";
Properties properties = new Properties();
properties.setProperty("annotators", "tokenize, ssplit, pos, depparse");
StanfordCoreNLP coreNLP = new StanfordCoreNLP(properties);
Annotation annotation = new Annotation(text);
coreNLP.annotate(annotation);
System.err.println("---");
List<CoreMap> sentenceMap = annotation.get(SentencesAnnotation.class);
for (CoreMap label : sentenceMap) {
SemanticGraph graph = label.get(BasicDependenciesAnnotation.class);
System.err.println(graph);
System.err.println("---");
Collection<IndexedWord> roots = graph.getRoots();
for (IndexedWord root : roots) {
Set<IndexedWord> used = new HashSet<IndexedWord>();
printWord2(root, graph, 0, used, "root");
}
}
}
public static void printWord2(IndexedWord word, SemanticGraph graph, int depth, Set<IndexedWord> used,
String relationName) {
used.add(word);
for (int n = 0; n < depth; n++) {
System.err.print(" ");
}
System.err.println("" + word.get(ValueAnnotation.class) //
+ " : " //
+ word.getString(PartOfSpeechAnnotation.class) + " : " //
+ relationName //
);
List<SemanticGraphEdge> edges = graph.outgoingEdgeList(word);
Collections.sort(edges);
for (SemanticGraphEdge edge : edges) {
IndexedWord targetWord = edge.getTarget();
GrammaticalRelation relation = edge.getRelation();
printWord2(targetWord, graph, depth + 1, used, relation.getShortName());
List<SemanticGraphEdge> edges2 = graph.outgoingEdgeList(targetWord);
for (SemanticGraphEdge e2 : edges2) {
IndexedWord w2 = e2.getTarget();
GrammaticalRelation r2 = edge.getRelation();
if (used.contains(w2) == false) {
printWord2(w2, graph, depth + 1, used, r2.getShortName());
}
}
}
}
}