Search in sources :

Example 1 with TypedDependency

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);
    }
}
Also used : TypedDependency(edu.stanford.nlp.trees.TypedDependency) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalStructureFactory(edu.stanford.nlp.trees.GrammaticalStructureFactory) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) Tree(edu.stanford.nlp.trees.Tree) TreebankLanguagePack(edu.stanford.nlp.trees.TreebankLanguagePack) PennTreebankLanguagePack(edu.stanford.nlp.trees.PennTreebankLanguagePack) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 2 with TypedDependency

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;
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) Word(edu.stanford.nlp.ling.Word) IndexedWord(edu.stanford.nlp.ling.IndexedWord) UniversalEnglishGrammaticalStructure(edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure) TypedDependency(edu.stanford.nlp.trees.TypedDependency) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) UniversalEnglishGrammaticalStructure(edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) TreeGraphNode(edu.stanford.nlp.trees.TreeGraphNode) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 3 with TypedDependency

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));
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) TypedDependency(edu.stanford.nlp.trees.TypedDependency) TaggedWord(edu.stanford.nlp.ling.TaggedWord) MaxentTagger(edu.stanford.nlp.tagger.maxent.MaxentTagger) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) ChineseGrammaticalStructure(edu.stanford.nlp.trees.international.pennchinese.ChineseGrammaticalStructure) EnglishGrammaticalStructure(edu.stanford.nlp.trees.EnglishGrammaticalStructure) UniversalEnglishGrammaticalStructure(edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure) Collectors.toList(java.util.stream.Collectors.toList) DocumentPreprocessor(edu.stanford.nlp.process.DocumentPreprocessor)

Example 4 with TypedDependency

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();
}
Also used : TypedDependency(edu.stanford.nlp.trees.TypedDependency) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) Tree(edu.stanford.nlp.trees.Tree) OutputStreamWriter(java.io.OutputStreamWriter)

Example 5 with TypedDependency

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));
}
Also used : TypedDependency(edu.stanford.nlp.trees.TypedDependency) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) Properties(java.util.Properties) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) Test(org.junit.Test)

Aggregations

TypedDependency (edu.stanford.nlp.trees.TypedDependency)6 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)4 GrammaticalStructure (edu.stanford.nlp.trees.GrammaticalStructure)4 HasWord (edu.stanford.nlp.ling.HasWord)3 IndexedWord (edu.stanford.nlp.ling.IndexedWord)3 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 TaggedWord (edu.stanford.nlp.ling.TaggedWord)2 Word (edu.stanford.nlp.ling.Word)2 Tree (edu.stanford.nlp.trees.Tree)2 TreeGraphNode (edu.stanford.nlp.trees.TreeGraphNode)2 UniversalEnglishGrammaticalStructure (edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure)2 Annotation (edu.stanford.nlp.pipeline.Annotation)1 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)1 DocumentPreprocessor (edu.stanford.nlp.process.DocumentPreprocessor)1 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)1 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)1 MaxentTagger (edu.stanford.nlp.tagger.maxent.MaxentTagger)1 EnglishGrammaticalStructure (edu.stanford.nlp.trees.EnglishGrammaticalStructure)1 GrammaticalStructureFactory (edu.stanford.nlp.trees.GrammaticalStructureFactory)1