Search in sources :

Example 51 with SemanticGraphEdge

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

the class ProtobufAnnotationSerializer method fromProto.

/**
   * Returns a sentence fragment from a given protocol buffer, and an associated parse tree.
   *
   * @param fragment The saved sentence fragment.
   * @param tree The parse tree for the whole sentence.
   *
   * @return A {@link SentenceFragment} object corresponding to the saved proto.
   */
public static SentenceFragment fromProto(CoreNLPProtos.SentenceFragment fragment, SemanticGraph tree) {
    if (Thread.interrupted()) {
        throw new RuntimeInterruptedException();
    }
    SemanticGraph fragmentTree = new SemanticGraph(tree);
    // Set the new root
    if (fragment.hasRoot()) {
        fragmentTree.resetRoots();
        fragmentTree.vertexSet().stream().filter(vertex -> vertex.index() - 1 == fragment.getRoot()).forEach(fragmentTree::setRoot);
    }
    // Set the new vertices
    Set<Integer> keptIndices = new HashSet<>(fragment.getTokenIndexList());
    tree.vertexSet().stream().filter(vertex -> !keptIndices.contains(vertex.index() - 1)).forEach(fragmentTree::removeVertex);
    // Apparently this sometimes screws up the tree
    fragmentTree.vertexSet().stream().filter(vertex -> fragmentTree.getFirstRoot() != vertex && tree.getFirstRoot() != vertex && !fragmentTree.incomingEdgeIterable(vertex).iterator().hasNext()).forEach(vertex -> {
        SemanticGraphEdge edge = tree.incomingEdgeIterable(vertex).iterator().next();
        fragmentTree.addEdge(fragmentTree.getFirstRoot(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
    });
    //noinspection SimplifiableConditionalExpression
    return new SentenceFragment(fragmentTree, fragment.hasAssumedTruth() ? fragment.getAssumedTruth() : true, false).changeScore(fragment.hasScore() ? fragment.getScore() : 1.0);
}
Also used : ExtractionObject(edu.stanford.nlp.ie.machinereading.structure.ExtractionObject) java.util(java.util) CorefChain(edu.stanford.nlp.coref.data.CorefChain) edu.stanford.nlp.util(edu.stanford.nlp.util) Tree(edu.stanford.nlp.trees.Tree) Dictionaries(edu.stanford.nlp.coref.data.Dictionaries) MachineReadingAnnotations(edu.stanford.nlp.ie.machinereading.structure.MachineReadingAnnotations) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations) RelationMention(edu.stanford.nlp.ie.machinereading.structure.RelationMention) Mention(edu.stanford.nlp.coref.data.Mention) CoreAnnotation(edu.stanford.nlp.ling.CoreAnnotation) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) Language(edu.stanford.nlp.international.Language) RNNCoreAnnotations(edu.stanford.nlp.neural.rnn.RNNCoreAnnotations) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) LabeledScoredTreeNode(edu.stanford.nlp.trees.LabeledScoredTreeNode) Timex(edu.stanford.nlp.time.Timex) IndexedWord(edu.stanford.nlp.ling.IndexedWord) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) CoreLabel(edu.stanford.nlp.ling.CoreLabel) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) edu.stanford.nlp.naturalli(edu.stanford.nlp.naturalli) SentimentCoreAnnotations(edu.stanford.nlp.sentiment.SentimentCoreAnnotations) NumberNormalizer(edu.stanford.nlp.ie.NumberNormalizer) Collectors(java.util.stream.Collectors) EntityMention(edu.stanford.nlp.ie.machinereading.structure.EntityMention) SegmenterCoreAnnotations(edu.stanford.nlp.ling.SegmenterCoreAnnotations) SpeakerInfo(edu.stanford.nlp.coref.data.SpeakerInfo) Span(edu.stanford.nlp.ie.machinereading.structure.Span) Word(edu.stanford.nlp.ling.Word) java.io(java.io) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 52 with SemanticGraphEdge

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

the class ProtobufAnnotationSerializer method toProto.

/**
   * Create a compact representation of the semantic graph for this dependency parse.
   * @param graph The dependency graph to save.
   * @return A protocol buffer message corresponding to this parse.
   */
public static CoreNLPProtos.DependencyGraph toProto(SemanticGraph graph) {
    CoreNLPProtos.DependencyGraph.Builder builder = CoreNLPProtos.DependencyGraph.newBuilder();
    // Roots
    Set<Integer> rootSet = graph.getRoots().stream().map(IndexedWord::index).collect(Collectors.toCollection(IdentityHashSet::new));
    // Nodes
    for (IndexedWord node : graph.vertexSet()) {
        // Register node
        CoreNLPProtos.DependencyGraph.Node.Builder nodeBuilder = CoreNLPProtos.DependencyGraph.Node.newBuilder().setSentenceIndex(node.get(SentenceIndexAnnotation.class)).setIndex(node.index());
        if (node.copyCount() > 0) {
            nodeBuilder.setCopyAnnotation(node.copyCount());
        }
        builder.addNode(nodeBuilder.build());
        // Register root
        if (rootSet.contains(node.index())) {
            builder.addRoot(node.index());
        }
    }
    // Edges
    for (SemanticGraphEdge edge : graph.edgeIterable()) {
        // Set edge
        builder.addEdge(CoreNLPProtos.DependencyGraph.Edge.newBuilder().setSource(edge.getSource().index()).setTarget(edge.getTarget().index()).setDep(edge.getRelation().toString()).setIsExtra(edge.isExtra()).setSourceCopy(edge.getSource().copyCount()).setTargetCopy(edge.getTarget().copyCount()).setLanguage(toProto(edge.getRelation().getLanguage())));
    }
    // Return
    return builder.build();
}
Also used : LabeledScoredTreeNode(edu.stanford.nlp.trees.LabeledScoredTreeNode) IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 53 with SemanticGraphEdge

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

the class XMLOutputter method buildDependencyTreeInfo.

private static Element buildDependencyTreeInfo(String dependencyType, SemanticGraph graph, List<CoreLabel> tokens, String curNS) {
    if (graph != null) {
        Element depInfo = new Element("dependencies", curNS);
        depInfo.addAttribute(new Attribute("type", dependencyType));
        // so we print that out ourselves
        for (IndexedWord root : graph.getRoots()) {
            String rel = GrammaticalRelation.ROOT.getLongName();
            // future proofing
            rel = rel.replaceAll("\\s+", "");
            int source = 0;
            int target = root.index();
            String sourceWord = "ROOT";
            String targetWord = tokens.get(target - 1).word();
            final boolean isExtra = false;
            addDependencyInfo(depInfo, rel, isExtra, source, sourceWord, null, target, targetWord, null, curNS);
        }
        for (SemanticGraphEdge edge : graph.edgeListSorted()) {
            String rel = edge.getRelation().toString();
            rel = rel.replaceAll("\\s+", "");
            int source = edge.getSource().index();
            int target = edge.getTarget().index();
            String sourceWord = tokens.get(source - 1).word();
            String targetWord = tokens.get(target - 1).word();
            Integer sourceCopy = edge.getSource().copyCount();
            Integer targetCopy = edge.getTarget().copyCount();
            boolean isExtra = edge.isExtra();
            addDependencyInfo(depInfo, rel, isExtra, source, sourceWord, sourceCopy, target, targetWord, targetCopy, curNS);
        }
        return depInfo;
    }
    return null;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) TreePrint(edu.stanford.nlp.trees.TreePrint) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 54 with SemanticGraphEdge

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

the class AddEdge method evaluate.

@Override
public void evaluate(SemanticGraph sg, SemgrexMatcher sm) {
    IndexedWord govNode = getNamedNode(govName, sm);
    IndexedWord depNode = getNamedNode(depName, sm);
    SemanticGraphEdge existingEdge = sg.getEdge(govNode, depNode, relation);
    if (existingEdge == null) {
        // 
        if (!sg.containsVertex(govNode))
            sg.addVertex(govNode);
        if (!sg.containsVertex(depNode))
            sg.addVertex(depNode);
        sg.addEdge(govNode, depNode, relation, weight, false);
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge)

Example 55 with SemanticGraphEdge

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

the class EnglishGrammaticalStructure method correctWHAttachment.

/**
   * Tries to correct complicated cases of WH-movement in
   * sentences such as "What does Mary seem to have?" in
   * which "What" should attach to "have" instead of the
   * control verb.
   *
   * @param sg The Semantic graph to operate on.
   */
private static void correctWHAttachment(SemanticGraph sg) {
    /* Semgrexes require a graph with a root. */
    if (sg.getRoots().isEmpty())
        return;
    SemanticGraph sgCopy = sg.makeSoftCopy();
    SemgrexMatcher matcher = XCOMP_PATTERN.matcher(sgCopy);
    while (matcher.findNextMatchingNode()) {
        IndexedWord root = matcher.getNode("root");
        IndexedWord embeddedVerb = matcher.getNode("embedded");
        IndexedWord wh = matcher.getNode("wh");
        IndexedWord dobj = matcher.getNode("obj");
        /* Check if the object is a WH-word. */
        if (wh.tag().startsWith("W")) {
            boolean reattach = false;
            /* If the control verb already has an object, then
           we have to reattach th WH-word to the verb in the embedded clause. */
            if (dobj != null) {
                reattach = true;
            } else {
                /* If the control verb can't have an object, we also have to reattach. */
                String lemma = Morphology.lemmaStatic(root.value(), root.tag());
                if (lemma.matches(EnglishPatterns.NP_V_S_INF_VERBS_REGEX)) {
                    reattach = true;
                }
            }
            if (reattach) {
                SemanticGraphEdge edge = sg.getEdge(root, wh);
                if (edge != null) {
                    sg.removeEdge(edge);
                    sg.addEdge(embeddedVerb, wh, DIRECT_OBJECT, Double.NEGATIVE_INFINITY, false);
                }
            }
        }
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) 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