Search in sources :

Example 61 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 62 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 63 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)

Example 64 with SemanticGraphEdge

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

the class UniversalDependenciesFeatureAnnotator method wasPerson.

/**
   * Determine the person of "was".
   */
private static String wasPerson(SemanticGraph sg, IndexedWord word) {
    IndexedWord subj = sg.getChildWithReln(word, UniversalEnglishGrammaticalRelations.NOMINAL_SUBJECT);
    if (subj == null) {
        subj = sg.getChildWithReln(word, UniversalEnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT);
    }
    if (subj != null) {
        if (subj.word().equalsIgnoreCase("i")) {
            /* "I" is the subject of "was". */
            return "1";
        }
    }
    IndexedWord parent = sg.getParent(word);
    if (parent == null) {
        return subj != null ? "3" : null;
    }
    SemanticGraphEdge edge = sg.getEdge(parent, word);
    if (edge == null) {
        return subj != null ? "3" : null;
    }
    if (UniversalEnglishGrammaticalRelations.AUX_MODIFIER.equals(edge.getRelation()) || UniversalEnglishGrammaticalRelations.AUX_PASSIVE_MODIFIER.equals(edge.getRelation())) {
        return wasPerson(sg, parent);
    }
    if (UniversalEnglishGrammaticalRelations.CONJUNCT.isAncestor(edge.getRelation())) {
        /* Check if the subject of the head of a conjunction is "I". */
        return wasPerson(sg, parent);
    }
    return "3";
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 65 with SemanticGraphEdge

use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project Info-Evaluation by TechnionYP5777.

the class AnalyzeParagragh method InteractiveAnalyze.

public InteractiveTableTuple InteractiveAnalyze() {
    final String $ = getName();
    final String input_date = getDate(year);
    String accurate_name = "";
    LinkedList<ReasonPair> reasons = InteractiveReasonFinding();
    final Properties props = new Properties();
    props.put("annotators", "tokenize,ssplit, pos, regexner, parse,lemma,natlog,openie");
    final StanfordCoreNLP pipeLine = new StanfordCoreNLP(props);
    final String inputText = input + "";
    final Annotation document = new Annotation(inputText);
    pipeLine.annotate(document);
    for (final CoreMap sentence : document.get(SentencesAnnotation.class)) {
        final SemanticGraph dependencies = sentence.get(CollapsedDependenciesAnnotation.class);
        for (final IndexedWord root : dependencies.getRoots()) for (final SemanticGraphEdge edge : dependencies.getOutEdgesSorted(root)) {
            final IndexedWord dep = edge.getDependent();
            if ("nsubjpass".equals((edge.getRelation() + ""))) {
                for (final SemanticGraphEdge keshet : dependencies.getOutEdgesSorted(dep)) {
                    final IndexedWord dep2 = keshet.getDependent();
                    final String rel2 = keshet.getRelation() + "";
                    if ("arrested".equals(edge.getGovernor().word()) && ((dep2.ner() != null && "PERSON".equals(dep2.ner())) || "compound".equals(rel2) || "det".equals(rel2)))
                        accurate_name += dep2.word() + " ";
                }
                accurate_name += dep.word();
            }
        }
    }
    return new InteractiveTableTuple(accurate_name.isEmpty() ? $ : accurate_name, input_date, reasons);
}
Also used : SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) ReasonPair(main.database.ReasonPair) Properties(java.util.Properties) IndexedWord(edu.stanford.nlp.ling.IndexedWord) CoreMap(edu.stanford.nlp.util.CoreMap) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) SentencesAnnotation(edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation) Annotation(edu.stanford.nlp.pipeline.Annotation) CollapsedDependenciesAnnotation(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) InteractiveTableTuple(main.database.InteractiveTableTuple)

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