Search in sources :

Example 21 with SemgrexMatcher

use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.

the class UniversalEnglishGrammaticalStructure method correctSubjPass.

/**
   * This method corrects subjects of verbs for which we identified an auxpass,
   * but didn't identify the subject as passive.
   *
   * @param sg SemanticGraph to work on
   */
private static void correctSubjPass(SemanticGraph sg) {
    /* If the graph doesn't have a root (most likely because
     * a parsing error, we can't match Semgrexes, so do
     * nothing. */
    if (sg.getRoots().isEmpty())
        return;
    SemanticGraph sgCopy = sg.makeSoftCopy();
    SemgrexMatcher matcher = CORRECT_SUBJPASS_PATTERN.matcher(sgCopy);
    while (matcher.find()) {
        IndexedWord gov = matcher.getNode("gov");
        IndexedWord subj = matcher.getNode("subj");
        SemanticGraphEdge edge = sg.getEdge(gov, subj);
        GrammaticalRelation reln = null;
        if (edge.getRelation() == NOMINAL_SUBJECT) {
            reln = NOMINAL_PASSIVE_SUBJECT;
        } else if (edge.getRelation() == CLAUSAL_SUBJECT) {
            reln = CLAUSAL_PASSIVE_SUBJECT;
        } else if (edge.getRelation() == CONTROLLING_NOMINAL_SUBJECT) {
            reln = CONTROLLING_NOMINAL_PASSIVE_SUBJECT;
        } else if (edge.getRelation() == CONTROLLING_CLAUSAL_SUBJECT) {
            reln = CONTROLLING_CLAUSAL_PASSIVE_SUBJECT;
        }
        if (reln != null) {
            sg.removeEdge(edge);
            sg.addEdge(gov, subj, reln, Double.NEGATIVE_INFINITY, false);
        }
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 22 with SemgrexMatcher

use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.

the class UniversalEnglishGrammaticalStructure method demoteQuantificationalModifiers.

private static void demoteQuantificationalModifiers(SemanticGraph sg) {
    SemanticGraph sgCopy = sg.makeSoftCopy();
    SemgrexMatcher matcher = QUANT_MOD_3W_PATTERN.matcher(sgCopy);
    while (matcher.findNextMatchingNode()) {
        IndexedWord w1 = matcher.getNode("w1");
        IndexedWord w2 = matcher.getNode("w2");
        IndexedWord w3 = matcher.getNode("w3");
        IndexedWord gov = matcher.getNode("gov");
        demoteQmodParentHelper(sg, gov, w2);
        List<IndexedWord> otherDeps = Generics.newLinkedList();
        otherDeps.add(w1);
        otherDeps.add(w2);
        otherDeps.add(w3);
        demoteQmodMWEHelper(sg, otherDeps, gov, w2);
    }
    for (SemgrexPattern p : QUANT_MOD_2W_PATTERNS) {
        sgCopy = sg.makeSoftCopy();
        matcher = p.matcher(sgCopy);
        while (matcher.findNextMatchingNode()) {
            IndexedWord w1 = matcher.getNode("w1");
            IndexedWord w2 = matcher.getNode("w2");
            IndexedWord gov = matcher.getNode("gov");
            demoteQmodParentHelper(sg, gov, w1);
            List<IndexedWord> otherDeps = Generics.newLinkedList();
            otherDeps.add(w1);
            otherDeps.add(w2);
            demoteQmodMWEHelper(sg, otherDeps, gov, w1);
        }
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 23 with SemgrexMatcher

use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.

the class UniversalEnglishGrammaticalStructure method addCaseMarkerInformation.

/**
   * Adds the case marker(s) to all nmod, acl and advcl relations that are
   * modified by one or more case markers(s).
   *
   * @param enhanceOnlyNmods If this is set to true, then prepositons will only be appended to nmod
   *                         relations (and not to acl or advcl) relations.
   *
   * @see UniversalEnglishGrammaticalStructure#addCaseMarkersToReln
   */
private static void addCaseMarkerInformation(SemanticGraph sg, boolean enhanceOnlyNmods) {
    /* Semgrexes require a graph with a root. */
    if (sg.getRoots().isEmpty())
        return;
    /* passive agent */
    SemanticGraph sgCopy = sg.makeSoftCopy();
    SemgrexMatcher matcher = PASSIVE_AGENT_PATTERN.matcher(sgCopy);
    while (matcher.find()) {
        IndexedWord caseMarker = matcher.getNode("c1");
        IndexedWord gov = matcher.getNode("gov");
        IndexedWord mod = matcher.getNode("mod");
        addPassiveAgentToReln(sg, gov, mod, caseMarker);
    }
    List<IndexedWord> oldCaseMarkers = Generics.newArrayList();
    /* 3-word prepositions */
    for (SemgrexPattern p : PREP_MW3_PATTERNS) {
        sgCopy = sg.makeSoftCopy();
        matcher = p.matcher(sgCopy);
        while (matcher.find()) {
            if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
                continue;
            }
            List<IndexedWord> caseMarkers = Generics.newArrayList(3);
            caseMarkers.add(matcher.getNode("c1"));
            caseMarkers.add(matcher.getNode("c2"));
            caseMarkers.add(matcher.getNode("c3"));
            Collections.sort(caseMarkers);
            /* We only want to match every case marker once. */
            if (caseMarkers.equals(oldCaseMarkers))
                continue;
            IndexedWord gov = matcher.getNode("gov");
            IndexedWord mod = matcher.getNode("mod");
            addCaseMarkersToReln(sg, gov, mod, caseMarkers);
            oldCaseMarkers = caseMarkers;
        }
    }
    /* 2-word prepositions */
    for (SemgrexPattern p : PREP_MW2_PATTERNS) {
        sgCopy = sg.makeSoftCopy();
        matcher = p.matcher(sgCopy);
        while (matcher.find()) {
            if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
                continue;
            }
            List<IndexedWord> caseMarkers = Generics.newArrayList(2);
            caseMarkers.add(matcher.getNode("c1"));
            caseMarkers.add(matcher.getNode("c2"));
            Collections.sort(caseMarkers);
            /* We only want to match every case marker once. */
            if (caseMarkers.equals(oldCaseMarkers))
                continue;
            IndexedWord gov = matcher.getNode("gov");
            IndexedWord mod = matcher.getNode("mod");
            addCaseMarkersToReln(sg, gov, mod, caseMarkers);
            oldCaseMarkers = caseMarkers;
        }
    }
    /* Single-word prepositions */
    for (SemgrexPattern p : PREP_PATTERNS) {
        sgCopy = sg.makeSoftCopy();
        matcher = p.matcher(sgCopy);
        while (matcher.find()) {
            if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
                continue;
            }
            List<IndexedWord> caseMarkers = Generics.newArrayList(1);
            caseMarkers.add(matcher.getNode("c1"));
            if (caseMarkers.equals(oldCaseMarkers))
                continue;
            IndexedWord gov = matcher.getNode("gov");
            IndexedWord mod = matcher.getNode("mod");
            addCaseMarkersToReln(sg, gov, mod, caseMarkers);
            oldCaseMarkers = caseMarkers;
        }
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Aggregations

SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)23 IndexedWord (edu.stanford.nlp.ling.IndexedWord)19 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)13 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)10 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)9 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)4 CoreLabel (edu.stanford.nlp.ling.CoreLabel)4 Span (edu.stanford.nlp.ie.machinereading.structure.Span)3 TokenSequenceMatcher (edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher)3 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)3 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)3 TokenSequencePattern (edu.stanford.nlp.ling.tokensregex.TokenSequencePattern)2 RelationTriple (edu.stanford.nlp.ie.util.RelationTriple)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 CoreAnnotation (edu.stanford.nlp.ling.CoreAnnotation)1 NaturalLogicAnnotations (edu.stanford.nlp.naturalli.NaturalLogicAnnotations)1 EnglishTreebankParserParams (edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams)1 TreebankLangParserParams (edu.stanford.nlp.parser.lexparser.TreebankLangParserParams)1 Annotation (edu.stanford.nlp.pipeline.Annotation)1 SentenceAnnotator (edu.stanford.nlp.pipeline.SentenceAnnotator)1