Search in sources :

Example 41 with SemanticGraphEdge

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

the class UniversalEnglishGrammaticalStructure method createMultiWordExpression.

private static void createMultiWordExpression(SemanticGraph sg, IndexedWord gov, GrammaticalRelation reln, IndexedWord... words) {
    if (sg.getRoots().isEmpty() || gov == null || words.length < 1) {
        return;
    }
    boolean first = true;
    IndexedWord mweHead = null;
    for (IndexedWord word : words) {
        IndexedWord wordGov = sg.getParent(word);
        if (wordGov != null) {
            SemanticGraphEdge edge = sg.getEdge(wordGov, word);
            if (edge != null) {
                sg.removeEdge(edge);
            }
        }
        if (first) {
            sg.addEdge(gov, word, reln, Double.NEGATIVE_INFINITY, false);
            mweHead = word;
            first = false;
        } else {
            sg.addEdge(mweHead, word, MULTI_WORD_EXPRESSION, Double.NEGATIVE_INFINITY, false);
        }
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 42 with SemanticGraphEdge

use of edu.stanford.nlp.semgraph.SemanticGraphEdge 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 43 with SemanticGraphEdge

use of edu.stanford.nlp.semgraph.SemanticGraphEdge 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 44 with SemanticGraphEdge

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

the class UniversalEnglishGrammaticalStructure method addExtraNSubj.

/**
   * Add extra nsubj dependencies when collapsing basic dependencies.
   * <br/>
   * In the general case, we look for an aux modifier under an xcomp
   * modifier, and assuming there aren't already associated nsubj
   * dependencies as daughters of the original xcomp dependency, we
   * add nsubj dependencies for each nsubj daughter of the aux.
   * <br/>
   * There is also a special case for "to" words, in which case we add
   * a dependency if and only if there is no nsubj associated with the
   * xcomp and there is no other aux dependency.  This accounts for
   * sentences such as "he decided not to" with no following verb.
   */
private static void addExtraNSubj(SemanticGraph sg) {
    for (SemanticGraphEdge xcomp : sg.findAllRelns(XCLAUSAL_COMPLEMENT)) {
        IndexedWord modifier = xcomp.getDependent();
        IndexedWord head = xcomp.getGovernor();
        boolean hasSubjectDaughter = false;
        boolean hasAux = false;
        List<IndexedWord> subjects = Generics.newArrayList();
        List<IndexedWord> objects = Generics.newArrayList();
        for (SemanticGraphEdge dep : sg.edgeIterable()) {
            // already have a subject dependency
            if ((dep.getRelation() == NOMINAL_SUBJECT || dep.getRelation() == NOMINAL_PASSIVE_SUBJECT) && dep.getGovernor().equals(modifier)) {
                hasSubjectDaughter = true;
                break;
            }
            if ((dep.getRelation() == AUX_MODIFIER || dep.getRelation() == MARKER) && dep.getGovernor().equals(modifier)) {
                hasAux = true;
            }
            if ((dep.getRelation() == NOMINAL_SUBJECT || dep.getRelation() == NOMINAL_PASSIVE_SUBJECT) && dep.getGovernor().equals(head)) {
                subjects.add(dep.getDependent());
            }
            if (dep.getRelation() == DIRECT_OBJECT && dep.getGovernor().equals(head)) {
                objects.add(dep.getDependent());
            }
        }
        // if we already have an nsubj dependency, no need to add an extra nsubj
        if (hasSubjectDaughter) {
            continue;
        }
        if ((modifier.value().equalsIgnoreCase("to") && hasAux) || (!modifier.value().equalsIgnoreCase("to") && !hasAux)) {
            continue;
        }
        // Instead of nsubj(do, law) we want nsubj(do, them)
        if (!objects.isEmpty()) {
            for (IndexedWord object : objects) {
                if (!sg.containsEdge(modifier, object))
                    sg.addEdge(modifier, object, CONTROLLING_NOMINAL_SUBJECT, Double.NEGATIVE_INFINITY, true);
            }
        } else {
            for (IndexedWord subject : subjects) {
                if (!sg.containsEdge(modifier, subject))
                    sg.addEdge(modifier, subject, CONTROLLING_NOMINAL_SUBJECT, Double.NEGATIVE_INFINITY, true);
            }
        }
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 45 with SemanticGraphEdge

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

the class UniversalEnglishGrammaticalStructure method addRef.

/**
   * Look for ref rules for a given word.  We look through the
   * children and grandchildren of the acl:relcl dependency, and if any
   * children or grandchildren depend on a that/what/which/etc word,
   * we take the leftmost that/what/which/etc word as the dependent
   * for the ref TypedDependency.
   */
private static void addRef(SemanticGraph sg) {
    for (SemanticGraphEdge edge : sg.findAllRelns(RELATIVE_CLAUSE_MODIFIER)) {
        IndexedWord head = edge.getGovernor();
        IndexedWord modifier = edge.getDependent();
        SemanticGraphEdge leftChildEdge = null;
        for (SemanticGraphEdge childEdge : sg.outgoingEdgeIterable(modifier)) {
            if (EnglishPatterns.RELATIVIZING_WORD_PATTERN.matcher(childEdge.getDependent().value()).matches() && (leftChildEdge == null || childEdge.getDependent().index() < leftChildEdge.getDependent().index())) {
                leftChildEdge = childEdge;
            }
        }
        SemanticGraphEdge leftGrandchildEdge = null;
        for (SemanticGraphEdge childEdge : sg.outgoingEdgeIterable(modifier)) {
            for (SemanticGraphEdge grandchildEdge : sg.outgoingEdgeIterable(childEdge.getDependent())) {
                if (EnglishPatterns.RELATIVIZING_WORD_PATTERN.matcher(grandchildEdge.getDependent().value()).matches() && (leftGrandchildEdge == null || grandchildEdge.getDependent().index() < leftGrandchildEdge.getDependent().index())) {
                    leftGrandchildEdge = grandchildEdge;
                }
            }
        }
        IndexedWord newDep = null;
        if (leftGrandchildEdge != null && (leftChildEdge == null || leftGrandchildEdge.getDependent().index() < leftChildEdge.getDependent().index())) {
            newDep = leftGrandchildEdge.getDependent();
        } else if (leftChildEdge != null) {
            newDep = leftChildEdge.getDependent();
        }
        if (newDep != null && !sg.containsEdge(head, newDep)) {
            sg.addEdge(head, newDep, REFERENT, Double.NEGATIVE_INFINITY, false);
        }
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Aggregations

SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)65 IndexedWord (edu.stanford.nlp.ling.IndexedWord)52 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)21 CoreLabel (edu.stanford.nlp.ling.CoreLabel)15 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)15 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)11 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)10 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)8 Pair (edu.stanford.nlp.util.Pair)6 Mention (edu.stanford.nlp.coref.data.Mention)5 Span (edu.stanford.nlp.ie.machinereading.structure.Span)5 Annotation (edu.stanford.nlp.pipeline.Annotation)5 Tree (edu.stanford.nlp.trees.Tree)5 CoreMap (edu.stanford.nlp.util.CoreMap)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 RelationTriple (edu.stanford.nlp.ie.util.RelationTriple)4 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)4 IntPair (edu.stanford.nlp.util.IntPair)4 java.util (java.util)4