use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.
the class DependencyIndexITest method checkTree.
private static void checkTree(Tree tree) {
List<Tree> leaves = tree.getLeaves();
for (Tree leaf : leaves) {
CoreLabel l = null;
if (leaf.label() instanceof CoreLabel)
l = (CoreLabel) leaf.label();
if (l != null) {
// System.err.println(l + " " + l.get(CoreAnnotations.IndexAnnotation.class));
int index = l.get(CoreAnnotations.IndexAnnotation.class);
String text = l.get(CoreAnnotations.TextAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
} else {
// System.err.println(leaf + " is not a CoreLabel.");
}
}
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> deps = gs.typedDependenciesCCprocessed(GrammaticalStructure.Extras.MAXIMAL);
// System.out.println(deps);
// collect all nodes in deps
Set<IndexedWord> nodes = Generics.newHashSet();
for (TypedDependency dep : deps) {
nodes.add(dep.gov());
nodes.add(dep.dep());
}
// check the indices for all nodes
for (IndexedWord n : nodes) {
String text = n.value();
int index = n.get(CoreAnnotations.IndexAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
}
}
use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.
the class SceneGraphImageUtils method getSemanticGraph.
public static GrammaticalStructure getSemanticGraph(List<String> depTriplets, List<CoreLabel> tokens) {
List<TypedDependency> dependencies = Generics.newArrayList(depTriplets.size());
Map<Integer, IndexedWord> idx2Node = Generics.newHashMap(depTriplets.size());
IndexedWord root = new IndexedWord(new Word("ROOT"));
root.set(CoreAnnotations.IndexAnnotation.class, 0);
for (String depTriplet : depTriplets) {
String[] parts = depTriplet.split(SEPARATOR_PATTERN);
int depIdx = Integer.parseInt(parts[0]);
int govIdx = Integer.parseInt(parts[1]);
String relnName = parts[2];
IndexedWord dep = idx2Node.get(depIdx);
IndexedWord gov = govIdx == 0 ? root : idx2Node.get(govIdx);
if (dep == null) {
dep = new IndexedWord(tokens.get(depIdx - 1));
idx2Node.put(depIdx, dep);
}
if (gov == null) {
gov = new IndexedWord(tokens.get(govIdx - 1));
idx2Node.put(govIdx, gov);
}
GrammaticalRelation reln = govIdx == 0 ? GrammaticalRelation.ROOT : GrammaticalRelation.valueOf(Language.UniversalEnglish, relnName);
TypedDependency td = new TypedDependency(reln, gov, dep);
dependencies.add(td);
}
TreeGraphNode rootNode = new TreeGraphNode(root);
GrammaticalStructure gs = new UniversalEnglishGrammaticalStructure(dependencies, rootNode);
return gs;
}
use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.
the class DependencyParser method parseTextFile.
private void parseTextFile(BufferedReader input, PrintWriter output) {
DocumentPreprocessor preprocessor = new DocumentPreprocessor(input);
preprocessor.setSentenceFinalPuncWords(config.tlp.sentenceFinalPunctuationWords());
preprocessor.setEscaper(config.escaper);
preprocessor.setSentenceDelimiter(config.sentenceDelimiter);
if (config.preTokenized) {
preprocessor.setTokenizerFactory(edu.stanford.nlp.process.WhitespaceTokenizer.factory());
} else {
preprocessor.setTokenizerFactory(config.tlp.getTokenizerFactory());
}
Timing timer = new Timing();
MaxentTagger tagger = new MaxentTagger(config.tagger);
List<List<TaggedWord>> tagged = new ArrayList<>();
for (List<HasWord> sentence : preprocessor) {
tagged.add(tagger.tagSentence(sentence));
}
log.info(String.format("Tagging completed in %.2f sec.%n", timer.stop() / 1000.0));
timer.start();
int numSentences = 0;
for (List<TaggedWord> taggedSentence : tagged) {
GrammaticalStructure parse = predict(taggedSentence);
Collection<TypedDependency> deps = parse.typedDependencies();
for (TypedDependency dep : deps) output.println(dep);
output.println();
numSentences++;
}
long millis = timer.stop();
double seconds = millis / 1000.0;
log.info(String.format("Parsed %d sentences in %.2f seconds (%.2f sents/sec).%n", numSentences, seconds, numSentences / seconds));
}
use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.
the class LexicalizedParserServer method handleDependencies.
// TODO: when this method throws an exception (for whatever reason)
// a waiting client might hang. There should be some graceful
// handling of that.
public void handleDependencies(String arg, OutputStream outStream, String commandArgs) throws IOException {
Tree tree = parse(arg, false);
if (tree == null) {
return;
}
// TODO: this might throw an exception if the parser doesn't support dependencies. Handle that cleaner?
GrammaticalStructure gs = parser.getTLPParams().getGrammaticalStructure(tree, parser.treebankLanguagePack().punctuationWordRejectFilter(), parser.getTLPParams().typedDependencyHeadFinder());
Collection<TypedDependency> deps = null;
switch(commandArgs.toUpperCase()) {
case "COLLAPSED_TREE":
deps = gs.typedDependenciesCollapsedTree();
break;
default:
throw new UnsupportedOperationException("Dependencies type not implemented: " + commandArgs);
}
OutputStreamWriter osw = new OutputStreamWriter(outStream, "utf-8");
for (TypedDependency dep : deps) {
osw.write(dep.toString());
osw.write("\n");
}
osw.flush();
}
use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.
the class DependencyParserITest method testCCProcess.
/**
* Test that postprocessing like CC-processing can handle the parser
* output properly
*/
@Test
public void testCCProcess() {
Properties props = PropertiesUtils.fromString("annotators=tokenize,ssplit,pos,depparse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "Chris and John went to the store.";
Annotation document = new Annotation(text);
pipeline.annotate(document);
SemanticGraph enhancedPlusPlus = document.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
Collection<TypedDependency> dependencies = enhancedPlusPlus.typedDependencies();
GrammaticalRelation expected = UniversalEnglishGrammaticalRelations.getConj("and");
assertTrue(dependencies.stream().map(TypedDependency::reln).collect(Collectors.toList()).contains(expected));
}
Aggregations