Search in sources :

Example 41 with GrammaticalRelation

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

the class DependencyParserITest method testCCProcess.

/**
   * Test that postprocessing like CC-processing can handle the parser
   * output properly
   */
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 ccProcessed = document.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
    Collection<TypedDependency> dependencies = ccProcessed.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) 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) Properties(java.util.Properties) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 42 with GrammaticalRelation

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

the class DocumentPreprocessor method findNextParagraphSpeaker.

private static String findNextParagraphSpeaker(Document doc, List<CoreMap> paragraph, int paragraphOffset, Dictionaries dict) {
    if (paragraph.isEmpty()) {
        return "";
    }
    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);
            if (dependency == null) {
                dependency = lastSent.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.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 (doc.mentionheadPositions.containsKey(headPosition) && doc.mentionheadPositions.get(headPosition).nerString.startsWith("PER")) {
                        speaker = Integer.toString(doc.mentionheadPositions.get(headPosition).mentionID);
                    }
                }
            }
        }
    }
    return speaker;
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) IntTuple(edu.stanford.nlp.util.IntTuple) 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) BasicDependenciesAnnotation(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.BasicDependenciesAnnotation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) CoreMap(edu.stanford.nlp.util.CoreMap)

Example 43 with GrammaticalRelation

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

the class DocumentPreprocessor method findSubject.

private static boolean findSubject(Document doc, SemanticGraph dependency, IndexedWord w, int sentNum, int utterNum) {
    for (Pair<GrammaticalRelation, IndexedWord> child : dependency.childPairs(w)) {
        if (child.first().getShortName().equals("nsubj")) {
            String subjectString = child.second().word();
            // start from 1
            int subjectIndex = child.second().index();
            IntTuple headPosition = new IntTuple(2);
            headPosition.set(0, sentNum);
            headPosition.set(1, subjectIndex - 1);
            String speaker;
            if (doc.mentionheadPositions.containsKey(headPosition)) {
                speaker = Integer.toString(doc.mentionheadPositions.get(headPosition).mentionID);
            } else {
                speaker = subjectString;
            }
            doc.speakers.put(utterNum, speaker);
            return true;
        }
    }
    return false;
}
Also used : IntTuple(edu.stanford.nlp.util.IntTuple) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 44 with GrammaticalRelation

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

the class DocumentPreprocessor method isSpeaker.

/** Check one mention is the speaker of the other mention */
public static boolean isSpeaker(Mention m, Mention ant, Dictionaries dict) {
    if (!dict.firstPersonPronouns.contains(ant.spanToString().toLowerCase()) || ant.number == Number.PLURAL || ant.sentNum != m.sentNum)
        return false;
    int countQuotationMark = 0;
    for (int i = Math.min(m.headIndex, ant.headIndex) + 1; i < Math.max(m.headIndex, ant.headIndex); i++) {
        String word = m.sentenceWords.get(i).get(CoreAnnotations.TextAnnotation.class);
        if (word.equals("``") || word.equals("''"))
            countQuotationMark++;
    }
    if (countQuotationMark != 1)
        return false;
    IndexedWord w = m.enhancedDependency.getNodeByWordPattern(m.sentenceWords.get(m.headIndex).get(CoreAnnotations.TextAnnotation.class));
    if (w == null)
        return false;
    for (Pair<GrammaticalRelation, IndexedWord> parent : m.enhancedDependency.parentPairs(w)) {
        if (parent.first().getShortName().equals("nsubj") && dict.reportVerb.contains(parent.second().get(CoreAnnotations.LemmaAnnotation.class))) {
            return true;
        }
    }
    return false;
}
Also used : SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 45 with GrammaticalRelation

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

the class DependencyCorefMentionFinder method extractNPorPRPFromDependency.

private void extractNPorPRPFromDependency(CoreMap s, List<Mention> mentions, Set<IntPair> mentionSpanSet, Set<IntPair> namedEntitySpanSet) {
    List<CoreLabel> sent = s.get(CoreAnnotations.TokensAnnotation.class);
    SemanticGraph basic = s.get(BasicDependenciesAnnotation.class);
    // DT is for "this, these, etc"
    List<IndexedWord> nounsOrPrp = basic.getAllNodesByPartOfSpeechPattern("N.*|PRP.*|DT");
    Tree tree = s.get(TreeAnnotation.class);
    for (IndexedWord w : nounsOrPrp) {
        SemanticGraphEdge edge = basic.getEdge(basic.getParent(w), w);
        GrammaticalRelation rel = null;
        // if edge is null, it's root
        String shortname = "root";
        if (edge != null) {
            rel = edge.getRelation();
            shortname = rel.getShortName();
        }
        // TODO: what to remove? remove more?
        if (shortname.matches("det|compound")) {
            continue;
        } else {
            extractMentionForHeadword(w, basic, s, mentions, mentionSpanSet, namedEntitySpanSet);
        }
    }
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) Tree(edu.stanford.nlp.trees.Tree) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

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