Search in sources :

Example 56 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 57 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 58 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 59 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 60 with SemanticGraphEdge

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

the class DependencyCorefMentionFinder method getNPSpan.

/**
   *  return the left and right most node except copula relation (nsubj & cop) and some others (maybe discourse?)
   *  e.g., you are the person -> return "the person"
   */
private IntPair getNPSpan(IndexedWord headword, SemanticGraph dep, List<CoreLabel> sent) {
    int headwordIdx = headword.index() - 1;
    List<IndexedWord> children = dep.getChildList(headword);
    //    if(children.size()==0) return new IntPair(headwordIdx, headwordIdx);    // the headword is the only word
    // check if we have copula relation
    IndexedWord cop = dep.getChildWithReln(headword, UniversalEnglishGrammaticalRelations.COPULA);
    int startIdx = (cop == null) ? 0 : children.indexOf(cop) + 1;
    // children which will be inside of NP
    List<IndexedWord> insideNP = Generics.newArrayList();
    for (int i = startIdx; i < children.size(); i++) {
        IndexedWord child = children.get(i);
        SemanticGraphEdge edge = dep.getEdge(headword, child);
        if (edge.getRelation().getShortName().matches("dep|discourse|punct")) {
            // skip
            continue;
        } else {
            insideNP.add(child);
        }
    }
    // the headword is the only word
    if (insideNP.size() == 0)
        return new IntPair(headwordIdx, headwordIdx);
    Pair<IndexedWord, IndexedWord> firstChildLeftRight = SemanticGraphUtils.leftRightMostChildVertices(insideNP.get(0), dep);
    Pair<IndexedWord, IndexedWord> lastChildLeftRight = SemanticGraphUtils.leftRightMostChildVertices(insideNP.get(insideNP.size() - 1), dep);
    // headword can be first or last word
    int beginIdx = Math.min(headwordIdx, firstChildLeftRight.first.index() - 1);
    int endIdx = Math.max(headwordIdx, lastChildLeftRight.second.index() - 1);
    return new IntPair(beginIdx, endIdx);
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) IntPair(edu.stanford.nlp.util.IntPair) 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