use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method createMultiWordExpression.
private static void createMultiWordExpression(SemanticGraph sg, IndexedWord gov, GrammaticalRelation reln, IndexedWord... words) {
if (sg.getRoots().isEmpty() || gov == null || words.length < 1) {
return;
}
boolean first = true;
IndexedWord mweHead = null;
for (IndexedWord word : words) {
IndexedWord wordGov = sg.getParent(word);
if (wordGov != null) {
SemanticGraphEdge edge = sg.getEdge(wordGov, word);
if (edge != null) {
sg.removeEdge(edge);
}
}
if (first) {
sg.addEdge(gov, word, reln, Double.NEGATIVE_INFINITY, false);
mweHead = word;
first = false;
} else {
sg.addEdge(mweHead, word, MULTI_WORD_EXPRESSION, Double.NEGATIVE_INFINITY, false);
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method correctSubjPass.
/**
* This method corrects subjects of verbs for which we identified an auxpass,
* but didn't identify the subject as passive.
*
* @param sg SemanticGraph to work on
*/
private static void correctSubjPass(SemanticGraph sg) {
/* If the graph doesn't have a root (most likely because
* a parsing error, we can't match Semgrexes, so do
* nothing. */
if (sg.getRoots().isEmpty())
return;
SemanticGraph sgCopy = sg.makeSoftCopy();
SemgrexMatcher matcher = CORRECT_SUBJPASS_PATTERN.matcher(sgCopy);
while (matcher.find()) {
IndexedWord gov = matcher.getNode("gov");
IndexedWord subj = matcher.getNode("subj");
SemanticGraphEdge edge = sg.getEdge(gov, subj);
GrammaticalRelation reln = null;
if (edge.getRelation() == NOMINAL_SUBJECT) {
reln = NOMINAL_PASSIVE_SUBJECT;
} else if (edge.getRelation() == CLAUSAL_SUBJECT) {
reln = CLAUSAL_PASSIVE_SUBJECT;
} else if (edge.getRelation() == CONTROLLING_NOMINAL_SUBJECT) {
reln = CONTROLLING_NOMINAL_PASSIVE_SUBJECT;
} else if (edge.getRelation() == CONTROLLING_CLAUSAL_SUBJECT) {
reln = CONTROLLING_CLAUSAL_PASSIVE_SUBJECT;
}
if (reln != null) {
sg.removeEdge(edge);
sg.addEdge(gov, subj, reln, Double.NEGATIVE_INFINITY, false);
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method addPassiveAgentToReln.
private static void addPassiveAgentToReln(SemanticGraph sg, IndexedWord gov, IndexedWord mod, IndexedWord caseMarker) {
SemanticGraphEdge edge = sg.getEdge(gov, mod);
GrammaticalRelation reln = UniversalEnglishGrammaticalRelations.getNmod("agent");
edge.setRelation(reln);
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method addExtraNSubj.
/**
* Add extra nsubj dependencies when collapsing basic dependencies.
* <br/>
* In the general case, we look for an aux modifier under an xcomp
* modifier, and assuming there aren't already associated nsubj
* dependencies as daughters of the original xcomp dependency, we
* add nsubj dependencies for each nsubj daughter of the aux.
* <br/>
* There is also a special case for "to" words, in which case we add
* a dependency if and only if there is no nsubj associated with the
* xcomp and there is no other aux dependency. This accounts for
* sentences such as "he decided not to" with no following verb.
*/
private static void addExtraNSubj(SemanticGraph sg) {
for (SemanticGraphEdge xcomp : sg.findAllRelns(XCLAUSAL_COMPLEMENT)) {
IndexedWord modifier = xcomp.getDependent();
IndexedWord head = xcomp.getGovernor();
boolean hasSubjectDaughter = false;
boolean hasAux = false;
List<IndexedWord> subjects = Generics.newArrayList();
List<IndexedWord> objects = Generics.newArrayList();
for (SemanticGraphEdge dep : sg.edgeIterable()) {
// already have a subject dependency
if ((dep.getRelation() == NOMINAL_SUBJECT || dep.getRelation() == NOMINAL_PASSIVE_SUBJECT) && dep.getGovernor().equals(modifier)) {
hasSubjectDaughter = true;
break;
}
if ((dep.getRelation() == AUX_MODIFIER || dep.getRelation() == MARKER) && dep.getGovernor().equals(modifier)) {
hasAux = true;
}
if ((dep.getRelation() == NOMINAL_SUBJECT || dep.getRelation() == NOMINAL_PASSIVE_SUBJECT) && dep.getGovernor().equals(head)) {
subjects.add(dep.getDependent());
}
if (dep.getRelation() == DIRECT_OBJECT && dep.getGovernor().equals(head)) {
objects.add(dep.getDependent());
}
}
// if we already have an nsubj dependency, no need to add an extra nsubj
if (hasSubjectDaughter) {
continue;
}
if ((modifier.value().equalsIgnoreCase("to") && hasAux) || (!modifier.value().equalsIgnoreCase("to") && !hasAux)) {
continue;
}
// Instead of nsubj(do, law) we want nsubj(do, them)
if (!objects.isEmpty()) {
for (IndexedWord object : objects) {
if (!sg.containsEdge(modifier, object))
sg.addEdge(modifier, object, CONTROLLING_NOMINAL_SUBJECT, Double.NEGATIVE_INFINITY, true);
}
} else {
for (IndexedWord subject : subjects) {
if (!sg.containsEdge(modifier, subject))
sg.addEdge(modifier, subject, CONTROLLING_NOMINAL_SUBJECT, Double.NEGATIVE_INFINITY, true);
}
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method addRef.
/**
* Look for ref rules for a given word. We look through the
* children and grandchildren of the acl:relcl dependency, and if any
* children or grandchildren depend on a that/what/which/etc word,
* we take the leftmost that/what/which/etc word as the dependent
* for the ref TypedDependency.
*/
private static void addRef(SemanticGraph sg) {
for (SemanticGraphEdge edge : sg.findAllRelns(RELATIVE_CLAUSE_MODIFIER)) {
IndexedWord head = edge.getGovernor();
IndexedWord modifier = edge.getDependent();
SemanticGraphEdge leftChildEdge = null;
for (SemanticGraphEdge childEdge : sg.outgoingEdgeIterable(modifier)) {
if (EnglishPatterns.RELATIVIZING_WORD_PATTERN.matcher(childEdge.getDependent().value()).matches() && (leftChildEdge == null || childEdge.getDependent().index() < leftChildEdge.getDependent().index())) {
leftChildEdge = childEdge;
}
}
SemanticGraphEdge leftGrandchildEdge = null;
for (SemanticGraphEdge childEdge : sg.outgoingEdgeIterable(modifier)) {
for (SemanticGraphEdge grandchildEdge : sg.outgoingEdgeIterable(childEdge.getDependent())) {
if (EnglishPatterns.RELATIVIZING_WORD_PATTERN.matcher(grandchildEdge.getDependent().value()).matches() && (leftGrandchildEdge == null || grandchildEdge.getDependent().index() < leftGrandchildEdge.getDependent().index())) {
leftGrandchildEdge = grandchildEdge;
}
}
}
IndexedWord newDep = null;
if (leftGrandchildEdge != null && (leftChildEdge == null || leftGrandchildEdge.getDependent().index() < leftChildEdge.getDependent().index())) {
newDep = leftGrandchildEdge.getDependent();
} else if (leftChildEdge != null) {
newDep = leftChildEdge.getDependent();
}
if (newDep != null && !sg.containsEdge(head, newDep)) {
sg.addEdge(head, newDep, REFERENT, Double.NEGATIVE_INFINITY, false);
}
}
}
Aggregations