use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class NaturalLogicAnnotator method getGeneralizedSubtreeSpan.
/** A helper method for
* {@link NaturalLogicAnnotator#getModifierSubtreeSpan(edu.stanford.nlp.semgraph.SemanticGraph, edu.stanford.nlp.ling.IndexedWord)} and
* {@link NaturalLogicAnnotator#getSubtreeSpan(edu.stanford.nlp.semgraph.SemanticGraph, edu.stanford.nlp.ling.IndexedWord)}.
*/
private static Pair<Integer, Integer> getGeneralizedSubtreeSpan(SemanticGraph tree, IndexedWord root, Set<String> validArcs) {
int min = root.index();
int max = root.index();
Queue<IndexedWord> fringe = new LinkedList<>();
for (SemanticGraphEdge edge : tree.outgoingEdgeIterable(root)) {
String edgeLabel = edge.getRelation().getShortName();
if ((validArcs == null || validArcs.contains(edgeLabel)) && !"punct".equals(edgeLabel)) {
fringe.add(edge.getDependent());
}
}
while (!fringe.isEmpty()) {
IndexedWord node = fringe.poll();
min = Math.min(node.index(), min);
max = Math.max(node.index(), max);
// ignore punctuation
fringe.addAll(tree.getOutEdgesSorted(node).stream().filter(edge -> edge.getGovernor().equals(node) && !(edge.getGovernor().equals(edge.getDependent())) && !"punct".equals(edge.getRelation().getShortName())).map(SemanticGraphEdge::getDependent).collect(Collectors.toList()));
}
return Pair.makePair(min, max + 1);
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class NaturalLogicWeights method ppDeletionProbability.
public double ppDeletionProbability(SemanticGraphEdge edge, Iterable<SemanticGraphEdge> neighbors) {
// Get information about the neighbors
// (in a totally not-creepy-stalker sort of way)
Optional<String> subj = Optional.empty();
Optional<String> obj = Optional.empty();
Optional<String> pp = Optional.empty();
for (SemanticGraphEdge neighbor : neighbors) {
if (neighbor != edge) {
String neighborRel = neighbor.getRelation().toString();
if (neighborRel.contains("subj")) {
subj = Optional.of(neighbor.getDependent().originalText().toLowerCase());
}
if (neighborRel.contains("obj")) {
obj = Optional.of(neighbor.getDependent().originalText().toLowerCase());
}
if (neighborRel.contains("prep")) {
pp = Optional.of(neighborRel);
}
}
}
String prep = edge.getRelation().toString();
String verb = edge.getGovernor().originalText().toLowerCase();
// Compute the most informative drop probability we can
Double rawScore = null;
if (subj.isPresent()) {
if (obj.isPresent()) {
// Case: subj+obj
rawScore = verbSubjObjPPAffinity.get(Quadruple.makeQuadruple(verb, subj.get(), obj.get(), prep));
}
if (rawScore == null && pp.isPresent()) {
// Case: subj+other_pp
rawScore = verbSubjPPPPAffinity.get(Quadruple.makeQuadruple(verb, subj.get(), pp.get(), prep));
}
if (rawScore == null) {
// Case: subj
rawScore = verbSubjPPAffinity.get(Triple.makeTriple(verb, subj.get(), prep));
}
}
if (rawScore == null) {
// Case: just the original pp
rawScore = verbPPAffinity.get(Pair.makePair(verb, prep));
}
if (rawScore == null) {
return deletionProbability(prep);
} else {
return 1.0 - Math.min(1.0, rawScore / upperProbabilityCap);
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class ClauseSplitterSearchProblem method simpleClause.
/**
* The basic method for splitting off a clause of a tree.
* This modifies the tree in place.
* This method addtionally follows ref edges.
*
* @param tree The tree to split a clause from.
* @param toKeep The edge representing the clause to keep.
*/
@SuppressWarnings("unchecked")
private void simpleClause(SemanticGraph tree, SemanticGraphEdge toKeep) {
splitToChildOfEdge(tree, toKeep);
// Follow 'ref' edges
Map<IndexedWord, IndexedWord> refReplaceMap = new HashMap<>();
// (find replacements)
for (IndexedWord vertex : tree.vertexSet()) {
for (SemanticGraphEdge edge : extraEdgesByDependent.get(vertex)) {
if (// it's a ref edge...
"ref".equals(edge.getRelation().toString()) && !tree.containsVertex(edge.getGovernor())) {
// ...that doesn't already exist in the tree.
refReplaceMap.put(vertex, edge.getGovernor());
}
}
}
// (do replacements)
for (Map.Entry<IndexedWord, IndexedWord> entry : refReplaceMap.entrySet()) {
Iterator<SemanticGraphEdge> iter = tree.incomingEdgeIterator(entry.getKey());
if (!iter.hasNext()) {
continue;
}
SemanticGraphEdge incomingEdge = iter.next();
IndexedWord governor = incomingEdge.getGovernor();
tree.removeVertex(entry.getKey());
addSubtree(tree, governor, incomingEdge.getRelation().toString(), this.tree, entry.getValue(), this.tree.incomingEdgeList(tree.getFirstRoot()));
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenter method segmentACL.
/**
* Same as {@link RelationTripleSegmenter#segmentVerb}, but with ACL clauses.
* This is a bit out of the ordinary, logic-wise, so it sits in its own function.
*/
private Optional<RelationTriple> segmentACL(SemanticGraph parse, Optional<Double> confidence, boolean consumeAll) {
IndexedWord subject = parse.getFirstRoot();
Optional<List<IndexedWord>> subjectSpan = getValidSubjectChunk(parse, subject, Optional.of("acl"));
if (subjectSpan.isPresent()) {
// found a valid subject
for (SemanticGraphEdge edgeFromSubj : parse.outgoingEdgeIterable(subject)) {
if ("acl".equals(edgeFromSubj.getRelation().toString())) {
// found a valid relation
IndexedWord relation = edgeFromSubj.getDependent();
List<IndexedWord> relationSpan = new ArrayList<>();
relationSpan.add(relation);
List<IndexedWord> objectSpan = new ArrayList<>();
List<IndexedWord> ppSpan = new ArrayList<>();
Optional<IndexedWord> pp = Optional.empty();
// Get other arguments
for (SemanticGraphEdge edgeFromRel : parse.outgoingEdgeIterable(relation)) {
String rel = edgeFromRel.getRelation().toString();
// Collect adverbs
if ("advmod".equals(rel)) {
Optional<List<IndexedWord>> advSpan = getValidAdverbChunk(parse, edgeFromRel.getDependent(), Optional.empty());
if (!advSpan.isPresent()) {
// bad adverb span!
return Optional.empty();
}
relationSpan.addAll(advSpan.get());
} else // Collect object
if (rel.endsWith("obj")) {
if (!objectSpan.isEmpty()) {
// duplicate objects!
return Optional.empty();
}
Optional<List<IndexedWord>> maybeObjSpan = getValidObjectChunk(parse, edgeFromRel.getDependent(), Optional.empty());
if (!maybeObjSpan.isPresent()) {
// bad object span!
return Optional.empty();
}
objectSpan.addAll(maybeObjSpan.get());
} else // Collect pp
if (rel.startsWith("nmod:")) {
if (!ppSpan.isEmpty()) {
// duplicate objects!
return Optional.empty();
}
Optional<List<IndexedWord>> maybePPSpan = getValidObjectChunk(parse, edgeFromRel.getDependent(), Optional.of("case"));
if (!maybePPSpan.isPresent()) {
// bad object span!
return Optional.empty();
}
ppSpan.addAll(maybePPSpan.get());
// Add the actual preposition, if we can find it
for (SemanticGraphEdge edge : parse.outgoingEdgeIterable(edgeFromRel.getDependent())) {
if ("case".equals(edge.getRelation().toString())) {
pp = Optional.of(edge.getDependent());
}
}
} else if (consumeAll) {
// bad edge out of the relation
return Optional.empty();
}
}
// (canonicalize the triple to be subject; relation; object, folding in the PP)
if (!ppSpan.isEmpty() && !objectSpan.isEmpty()) {
relationSpan.addAll(objectSpan);
objectSpan = ppSpan;
} else if (!ppSpan.isEmpty()) {
objectSpan = ppSpan;
}
// (last error checks -- shouldn't ever fire)
if (!subjectSpan.isPresent() || subjectSpan.get().isEmpty() || relationSpan.isEmpty() || objectSpan.isEmpty()) {
return Optional.empty();
}
// (sort the relation span)
Collections.sort(relationSpan, (a, b) -> {
double val = a.pseudoPosition() - b.pseudoPosition();
if (val < 0) {
return -1;
}
if (val > 0) {
return 1;
} else {
return 0;
}
});
// (add in the PP node, if it exists)
if (pp.isPresent()) {
relationSpan.add(pp.get());
}
// (success!)
RelationTriple.WithTree extraction = new RelationTriple.WithTree(subjectSpan.get().stream().map(IndexedWord::backingLabel).collect(Collectors.toList()), relationSpan.stream().map(IndexedWord::backingLabel).collect(Collectors.toList()), objectSpan.stream().map(IndexedWord::backingLabel).collect(Collectors.toList()), parse, confidence.orElse(1.0));
return Optional.of(extraction);
}
}
}
// Nothing found; return
return Optional.empty();
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class SentenceFragment method toString.
@Override
public String toString() {
List<Pair<String, Integer>> glosses = new ArrayList<>();
for (CoreLabel word : words) {
// Add the word itself
glosses.add(Pair.makePair(word.word(), word.index() - 1));
String addedConnective = null;
// Find additional connectives
for (SemanticGraphEdge edge : parseTree.incomingEdgeIterable(new IndexedWord(word))) {
String rel = edge.getRelation().toString();
if (rel.contains("_")) {
// for Stanford dependencies only
addedConnective = rel.substring(rel.indexOf('_') + 1);
}
}
if (addedConnective != null) {
// Found a connective (e.g., a preposition or conjunction)
Pair<Integer, Integer> yield = parseTree.yieldSpan(new IndexedWord(word));
glosses.add(Pair.makePair(addedConnective.replaceAll("_", " "), yield.first - 1));
}
}
// Sort the sentence
Collections.sort(glosses, (a, b) -> a.second - b.second);
// Return the sentence
return StringUtils.join(glosses.stream().map(Pair::first), " ");
}
Aggregations