Example usage for edu.stanford.nlp.trees GrammaticalRelation ROOT

List of usage examples for edu.stanford.nlp.trees GrammaticalRelation ROOT

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees GrammaticalRelation ROOT.

Prototype

GrammaticalRelation ROOT

To view the source code for edu.stanford.nlp.trees GrammaticalRelation ROOT.

Click Source Link

Document

The "root" grammatical relation between a faked "ROOT" node, and the root of the sentence.

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 {/* w  ww. j  a va  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:edu.jhu.hlt.concrete.stanford.PreNERCoreMapWrapper.java

License:Open Source License

private List<Dependency> makeDependencies(SemanticGraph graph) {
    List<Dependency> depList = new ArrayList<Dependency>();
    for (IndexedWord root : graph.getRoots()) {
        // this mimics CoreNLP's handling
        String rel = GrammaticalRelation.ROOT.getLongName().replaceAll("\\s+", "");
        int dep = root.index() - 1;
        Dependency depend = DependencyFactory.create(dep, rel);
        depList.add(depend);//from   w w w.  j  a va 2 s. c o m
    }
    for (SemanticGraphEdge edge : graph.edgeListSorted()) {
        String rel = edge.getRelation().toString().replaceAll("\\s+", "");
        int gov = edge.getSource().index() - 1;
        int dep = edge.getTarget().index() - 1;
        Dependency depend = DependencyFactory.create(dep, rel, gov);
        depList.add(depend);
    }
    return depList;
}

From source file:opendial.bn.values.RelationalVal.java

License:Open Source License

private static GrammaticalRelation recordRelation(String rel) {
    return relations.computeIfAbsent(rel,
            l -> new GrammaticalRelation(Language.Any, l, l, GrammaticalRelation.ROOT));
}