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