Example usage for edu.stanford.nlp.trees TreeGraphNode label

List of usage examples for edu.stanford.nlp.trees TreeGraphNode label

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees TreeGraphNode label.

Prototype

CoreLabel label

To view the source code for edu.stanford.nlp.trees TreeGraphNode label.

Click Source Link

Document

Label for this node.

Usage

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  w  w  w.j a v a2s.  c  o  m
        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:edu.jhu.agiga.StanfordAgigaSentence.java

License:Open Source License

public List<TreeGraphNode> getStanfordTreeGraphNodes(DependencyForm form) {
    if (this.nodes != null)
        return this.nodes;

    this.nodes = new ArrayList<TreeGraphNode>();
    // Add an explicit root node
    nodes.add(new TreeGraphNode(ROOT_LABEL));

    List<WordLemmaTag> labels = getStanfordWordLemmaTags();
    for (WordLemmaTag curToken : labels) {
        // Create the tree node
        TreeGraphNode treeNode = new TreeGraphNode(curToken);
        treeNode.label().setTag(curToken.tag());
        /**/*from   w w w.  j a v  a 2s.c  o m*/
         * Caution, the order to call is to first setWord(), then setlemma()
         * From the Stanford source code:
         *   
        public void setWord(String word) {
        set(WordAnnotation.class, word);
        // pado feb 09: if you change the word, delete the lemma.
        remove(LemmaAnnotation.class);
        }
                
        public void setLemma(String lemma) {
        set(LemmaAnnotation.class, lemma);
        }
          */
        treeNode.label().setWord(curToken.word());
        treeNode.label().setLemma(curToken.lemma());
        nodes.add(treeNode);
    }

    List<AgigaTypedDependency> agigaDeps = getAgigaDeps(form);
    for (AgigaTypedDependency agigaDep : agigaDeps) {
        // Add one, since the tokens are zero-indexed but the TreeGraphNodes are one-indexed
        TreeGraphNode gov = nodes.get(agigaDep.getGovIdx() + 1);
        TreeGraphNode dep = nodes.get(agigaDep.getDepIdx() + 1);

        // Add gov/dep to TreeGraph
        gov.addChild(dep);
        dep.setParent(gov);
        require(dep.parent() == gov);
    }

    return nodes;
}

From source file:org.devboost.stanford.language.LanguageCreator.java

License:Open Source License

private List<Dependency> createDependencies(GrammaticalStructure gs, Sentence sentence) {
    List<Dependency> dependencies = new ArrayList<Dependency>();
    List<TypedDependency> typedDependencies = gs.typedDependenciesCollapsed(true);
    for (TypedDependency typedDependency : typedDependencies) {
        TreeGraphNode dep = typedDependency.dep();
        TreeGraphNode gov = typedDependency.gov();
        GrammaticalRelation relation = typedDependency.reln();
        String shortName = relation.getShortName();
        String specific = relation.getSpecific();
        EClassifier classifier = LanguagePackage.eINSTANCE.getEClassifier(DEPENDENCY_PREFIX + shortName);
        if (classifier instanceof EClass) {
            Dependency dependency = (Dependency) LanguageFactory.eINSTANCE.create((EClass) classifier);
            Word dependant = getWordInSentence(sentence, dep);
            Word governor = null;//ww  w .  ja  v a  2s.  c  o m
            if (!gov.label().word().equals("ROOT")) {
                governor = getWordInSentence(sentence, gov);
            }
            if (governor != null) {
                dependency.setGovernor(governor);
            }
            if (dependant != null) {
                dependency.setDependent(dependant);
                sentence.getDependencies().add(dependency);
            }
            if (specific != null) {
                List<Word> words = sentence.findWords(specific);
                if (dependency instanceof Collapse) {
                    if (words.size() == 1) {
                        ((Collapse) dependency).setCollapsedWord(words.get(0));
                    }
                    ((Collapse) dependency).setCollapsedWordString(specific);
                }
            }
        }
    }
    return dependencies;
}

From source file:org.devboost.stanford.language.LanguageCreator.java

License:Open Source License

private Word getWordInSentence(Sentence sentence, TreeGraphNode node) {
    CyclicCoreLabel label = node.label();
    int begin = label.get(CharacterOffsetBeginAnnotation.class);
    int end = label.get(CharacterOffsetEndAnnotation.class);
    Word word = sentence.getWord(begin, end);
    return word;//w w  w . j a  v a 2s .  com
}