Search in sources :

Example 31 with GrammaticalRelation

use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.

the class UniversalEnglishGrammaticalRelations method getConj.

/**
   * The "conj" grammatical relation. Used to enhance conjunct relations.
   * They will be turned into conj:word, where "word" is a conjunction.
   *
   * @param conjunctionString The conjunction to make a GrammaticalRelation out of
   * @return A grammatical relation for this conjunction
   */
public static GrammaticalRelation getConj(String conjunctionString) {
    GrammaticalRelation result = conjs.get(conjunctionString);
    if (result == null) {
        synchronized (conjs) {
            result = conjs.get(conjunctionString);
            if (result == null) {
                result = new GrammaticalRelation(Language.UniversalEnglish, "conj", "conj_collapsed", CONJUNCT, conjunctionString);
                conjs.put(conjunctionString, result);
                threadSafeAddRelation(result);
            }
        }
    }
    return result;
}
Also used : GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation)

Example 32 with GrammaticalRelation

use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.

the class UniversalEnglishGrammaticalStructure method addPassiveAgentToReln.

private static void addPassiveAgentToReln(SemanticGraph sg, IndexedWord gov, IndexedWord mod, IndexedWord caseMarker) {
    SemanticGraphEdge edge = sg.getEdge(gov, mod);
    GrammaticalRelation reln = UniversalEnglishGrammaticalRelations.getNmod("agent");
    edge.setRelation(reln);
}
Also used : GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 33 with GrammaticalRelation

use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.

the class IETestUtils method parseCoNLL.

/**
   * Parse a CoNLL formatted string into a SemanticGraph.
   * This is useful for tests so that you don't need to load the model (and are robust to
   * model changes).
   *
   * @param conll The CoNLL format for the tree.
   * @return A semantic graph, as well as the flat tokens of the sentence.
   */
public static Pair<SemanticGraph, List<CoreLabel>> parseCoNLL(String conll) {
    List<CoreLabel> sentence = new ArrayList<>();
    SemanticGraph tree = new SemanticGraph();
    for (String line : conll.split("\n")) {
        if (line.trim().equals("")) {
            continue;
        }
        String[] fields = line.trim().split("\\s+");
        int index = Integer.parseInt(fields[0]);
        String word = fields[1];
        CoreLabel label = mkWord(word, index);
        sentence.add(label);
        if (fields[2].equals("0")) {
            tree.addRoot(new IndexedWord(label));
        } else {
            tree.addVertex(new IndexedWord(label));
        }
        if (fields.length > 4) {
            label.setTag(fields[4]);
        }
        if (fields.length > 5) {
            label.setNER(fields[5]);
        }
        if (fields.length > 6) {
            label.setLemma(fields[6]);
        }
    }
    int i = 0;
    for (String line : conll.split("\n")) {
        if (line.trim().equals("")) {
            continue;
        }
        String[] fields = line.trim().split("\\s+");
        int parent = Integer.parseInt(fields[2]);
        String reln = fields[3];
        if (parent > 0) {
            tree.addEdge(new IndexedWord(sentence.get(parent - 1)), new IndexedWord(sentence.get(i)), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
        }
        i += 1;
    }
    return Pair.makePair(tree, sentence);
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) ArrayList(java.util.ArrayList) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 34 with GrammaticalRelation

use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.

the class RelationTripleSegmenterTest method mkTree.

/**
   * Parse a CoNLL formatted tree into a SemanticGraph object (along with a list of tokens).
   *
   * @param conll The CoNLL formatted tree.
   *
   * @return A pair of a SemanticGraph and a token list, corresponding to the parse of the sentence
   *         and to tokens in the sentence.
   */
protected Pair<SemanticGraph, List<CoreLabel>> mkTree(String conll) {
    List<CoreLabel> sentence = new ArrayList<>();
    SemanticGraph tree = new SemanticGraph();
    for (String line : conll.split("\n")) {
        if (line.trim().equals("")) {
            continue;
        }
        String[] fields = line.trim().split("\\s+");
        int index = Integer.parseInt(fields[0]);
        String word = fields[1];
        CoreLabel label = IETestUtils.mkWord(word, index);
        sentence.add(label);
        if (fields[2].equals("0")) {
            tree.addRoot(new IndexedWord(label));
        } else {
            tree.addVertex(new IndexedWord(label));
        }
        if (fields.length > 4) {
            label.setTag(fields[4]);
        }
        if (fields.length > 5) {
            label.setNER(fields[5]);
        }
        if (fields.length > 6) {
            label.setLemma(fields[6]);
        }
    }
    int i = 0;
    for (String line : conll.split("\n")) {
        if (line.trim().equals("")) {
            continue;
        }
        String[] fields = line.trim().split("\\s+");
        int parent = Integer.parseInt(fields[2]);
        String reln = fields[3];
        if (parent > 0) {
            tree.addEdge(new IndexedWord(sentence.get(parent - 1)), new IndexedWord(sentence.get(i)), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
        }
        i += 1;
    }
    return Pair.makePair(tree, sentence);
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) ArrayList(java.util.ArrayList) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 35 with GrammaticalRelation

use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.

the class Document method findNextParagraphSpeaker.

private String findNextParagraphSpeaker(List<CoreMap> paragraph, int paragraphOffset, Dictionaries dict) {
    CoreMap lastSent = paragraph.get(paragraph.size() - 1);
    String speaker = "";
    for (CoreLabel w : lastSent.get(CoreAnnotations.TokensAnnotation.class)) {
        if (w.get(CoreAnnotations.LemmaAnnotation.class).equals("report") || w.get(CoreAnnotations.LemmaAnnotation.class).equals("say")) {
            String word = w.get(CoreAnnotations.TextAnnotation.class);
            SemanticGraph dependency = lastSent.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
            IndexedWord t = dependency.getNodeByWordPattern(word);
            for (Pair<GrammaticalRelation, IndexedWord> child : dependency.childPairs(t)) {
                if (child.first().getShortName().equals("nsubj")) {
                    // start from 1
                    int subjectIndex = child.second().index();
                    IntTuple headPosition = new IntTuple(2);
                    headPosition.set(0, paragraph.size() - 1 + paragraphOffset);
                    headPosition.set(1, subjectIndex - 1);
                    if (mentionheadPositions.containsKey(headPosition) && mentionheadPositions.get(headPosition).nerString.startsWith("PER")) {
                        speaker = Integer.toString(mentionheadPositions.get(headPosition).mentionID);
                    }
                }
            }
        }
    }
    return speaker;
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) IntTuple(edu.stanford.nlp.util.IntTuple) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) CoreMap(edu.stanford.nlp.util.CoreMap)

Aggregations

GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)49 IndexedWord (edu.stanford.nlp.ling.IndexedWord)38 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)13 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)13 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)11 CoreLabel (edu.stanford.nlp.ling.CoreLabel)11 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)9 ArrayList (java.util.ArrayList)5 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)4 IntTuple (edu.stanford.nlp.util.IntTuple)4 Tree (edu.stanford.nlp.trees.Tree)3 Word (edu.stanford.nlp.ling.Word)2 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)2 TypedDependency (edu.stanford.nlp.trees.TypedDependency)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)1 CorefChain (edu.stanford.nlp.coref.data.CorefChain)1 Dictionaries (edu.stanford.nlp.coref.data.Dictionaries)1 Mention (edu.stanford.nlp.coref.data.Mention)1 SpeakerInfo (edu.stanford.nlp.coref.data.SpeakerInfo)1