List of usage examples for edu.stanford.nlp.trees TreeGraphNode index
public int index()
From source file:ca.ualberta.exemplar.core.ParserMalt.java
License:Open Source License
@Override public List<CoreMap> parseText(String text) { List<CoreMap> sentences = null; try {/*from ww w . j ava 2s. c om*/ Annotation document = new Annotation(text); pipeline.annotate(document); sentences = document.get(SentencesAnnotation.class); for (CoreMap sentence : sentences) { List<CoreLabel> tokens = sentence.get(TokensAnnotation.class); String[] conllInput = sentenceToCoNLLInput(tokens); DependencyGraph graph = (DependencyGraph) maltParser.parse(conllInput); Result result = graphToCoNLL(graph); List<List<String>> conll = result.conll; int rootIndex = result.rootIndex; EnglishGrammaticalStructure gs = EnglishGrammaticalStructure.buildCoNNLXGrammaticStructure(conll); TreeGraphNode root = null; List<TypedDependency> deps = gs.typedDependenciesCCprocessed(); // Add root dependency and ner annotations int size = deps.size(); for (int i = 0; i < size; i++) { TypedDependency td = deps.get(i); if (td.gov().index() == rootIndex) { root = td.gov(); deps.add(new TypedDependency(GrammaticalRelation.ROOT, td.gov(), td.gov())); } { TreeGraphNode n = td.dep(); if (n.label().ner() == null) { n.label().setNER(tokens.get(n.index() - 1).ner()); n.label().setBeginPosition(tokens.get(n.index() - 1).beginPosition()); n.label().setEndPosition(tokens.get(n.index() - 1).endPosition()); n.label().setLemma(tokens.get(n.index() - 1).lemma()); } } { TreeGraphNode n = td.gov(); if (n.label().ner() == null) { n.label().setNER(tokens.get(n.index() - 1).ner()); n.label().setBeginPosition(tokens.get(n.index() - 1).beginPosition()); n.label().setEndPosition(tokens.get(n.index() - 1).endPosition()); n.label().setLemma(tokens.get(n.index() - 1).lemma()); } } } if (root == null) continue; List<TreeGraphNode> roots = new ArrayList<TreeGraphNode>(); roots.add(gs.root()); SemanticGraph sg = new SemanticGraph(deps, roots); sentence.set(CollapsedCCProcessedDependenciesAnnotation.class, sg); } } catch (Exception e) { e.printStackTrace(); } return sentences; }
From source file:cc.vidr.parseviz.ParseViz.java
License:Open Source License
public static void printDependenciesDot(Tree tree, StringBuilder sb) { sb.append("digraph{\n"); for (TypedDependency td : typedDependencies(tree)) { GrammaticalRelation reln = td.reln(); TreeGraphNode gov = td.gov(); TreeGraphNode dep = td.dep();//from w ww . j av a 2s .c o m sb.append("n").append(gov.index()).append("[label=\"").append(gov).append("\"];\n").append("n") .append(dep.index()).append("[label=\"").append(dep).append("\"];\n").append("n") .append(gov.index()).append("->n").append(dep.index()).append("[label=\"").append(reln) .append("\"];\n"); } sb.append("}\n"); }