Search in sources :

Example 21 with GrammaticalRelation

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

the class OpenIETest method clauses.

protected Set<String> clauses(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.English, reln, null, null), 1.0, false);
        }
        i += 1;
    }
    // Run extractor
    ClauseSplitterSearchProblem problem = new ClauseSplitterSearchProblem(tree, true);
    Set<String> clauses = new HashSet<>();
    problem.search(triple -> {
        clauses.add(triple.third.get().toString());
        return true;
    }, new LinearClassifier<>(new ClassicCounter<>()), ClauseSplitterSearchProblem.HARD_SPLITS, triple -> new ClassicCounter<String>() {

        {
            setCount("__undocumented_junit_no_classifier", 1.0);
        }
    }, 100000);
    return clauses;
}
Also used : ArrayList(java.util.ArrayList) CoreLabel(edu.stanford.nlp.ling.CoreLabel) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) HashSet(java.util.HashSet)

Example 22 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 23 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 24 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 25 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)

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