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);
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations