use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method collapseReferent.
/**
* This method will collapse a referent relation such as follows. e.g.:
* "The man that I love ... " ref(man, that) dobj(love, that) -> ref(man, that) dobj(love,
* man)
*/
private static void collapseReferent(SemanticGraph sg) {
// find typed deps of form ref(gov, dep)
// put them in a List for processing
List<SemanticGraphEdge> refs = new ArrayList<>(sg.findAllRelns(REFERENT));
SemanticGraph sgCopy = sg.makeSoftCopy();
// now substitute target of referent where possible
for (SemanticGraphEdge ref : refs) {
// take the relative word
IndexedWord dep = ref.getDependent();
// take the antecedent
IndexedWord ant = ref.getGovernor();
for (Iterator<SemanticGraphEdge> iter = sgCopy.incomingEdgeIterator(dep); iter.hasNext(); ) {
SemanticGraphEdge edge = iter.next();
// disconnected) [cdm Jan 2010]
if (edge.getRelation() != REFERENT && !edge.getGovernor().equals(ant)) {
sg.removeEdge(edge);
sg.addEdge(edge.getGovernor(), ant, edge.getRelation(), Double.NEGATIVE_INFINITY, true);
}
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method process3WP.
/**
* Processes all the three-word prepositions in THREE_WORD_PREPS.
*/
private static void process3WP(SemanticGraph sg, HashMap<String, HashSet<Integer>> trigrams) {
for (String trigram : THREE_WORD_PREPS) {
if (trigrams.get(trigram) == null) {
continue;
}
for (Integer i : trigrams.get(trigram)) {
IndexedWord w1 = sg.getNodeByIndexSafe(i);
IndexedWord w2 = sg.getNodeByIndexSafe(i + 1);
IndexedWord w3 = sg.getNodeByIndexSafe(i + 2);
if (w1 == null || w2 == null || w3 == null) {
continue;
}
SemgrexMatcher matcher = THREE_WORD_PREPS_PATTERN.matcher(sg);
IndexedWord gov = null;
IndexedWord gov2 = null;
while (matcher.find()) {
if (w1.equals(matcher.getNode("w1")) && w2.equals(matcher.getNode("w2")) && w3.equals(matcher.getNode("w3"))) {
gov = matcher.getNode("gov");
gov2 = matcher.getNode("gov2");
break;
}
}
if (gov2 == null) {
continue;
}
GrammaticalRelation markerReln = CASE_MARKER;
if (sg.getRoots().contains(w2)) {
SemanticGraphEdge edge = sg.getEdge(w2, gov2);
if (edge == null) {
continue;
}
sg.removeEdge(edge);
sg.getRoots().remove(w2);
sg.addRoot(gov2);
} else {
SemanticGraphEdge edge = sg.getEdge(w2, gov2);
if (edge == null) {
continue;
}
sg.removeEdge(edge);
gov = gov == null ? sg.getParent(w2) : gov;
if (gov == null) {
continue;
}
GrammaticalRelation reln = sg.getEdge(gov, w2).getRelation();
if (reln == NOMINAL_MODIFIER && (edge.getRelation() == CLAUSAL_MODIFIER || edge.getRelation() == ADV_CLAUSE_MODIFIER)) {
reln = edge.getRelation();
markerReln = MARKER;
}
sg.addEdge(gov, gov2, reln, Double.NEGATIVE_INFINITY, false);
}
/* Make children of w2 dependents of gov2. */
for (SemanticGraphEdge edge2 : sg.getOutEdgesSorted(w2)) {
sg.removeEdge(edge2);
sg.addEdge(gov2, edge2.getDependent(), edge2.getRelation(), edge2.getWeight(), edge2.isExtra());
}
createMultiWordExpression(sg, gov2, markerReln, w1, w2, w3);
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method convertRel.
/**
* What we do in this method is look for temporary dependencies of
* the type "rel" and "prep". These occur in sentences such as "I saw the man
* who you love". In that case, we should produce dobj(love, who).
* On the other hand, in the sentence "... which Mr. Bush was
* fighting for", we should have case(which, for).
*/
private static void convertRel(SemanticGraph sg) {
for (SemanticGraphEdge prep : sg.findAllRelns(PREPOSITION)) {
boolean changedPrep = false;
for (SemanticGraphEdge nmod : sg.outgoingEdgeIterable(prep.getGovernor())) {
// then we could get right: Which eco-friendly options do you think there will be on the new Lexus?
if (nmod.getRelation() != NOMINAL_MODIFIER && nmod.getRelation() != RELATIVE) {
continue;
}
if (prep.getDependent().index() < nmod.getDependent().index()) {
continue;
}
sg.removeEdge(prep);
sg.addEdge(nmod.getDependent(), prep.getDependent(), CASE_MARKER, Double.NEGATIVE_INFINITY, false);
changedPrep = true;
if (nmod.getRelation() == RELATIVE) {
nmod.setRelation(NOMINAL_MODIFIER);
}
break;
}
if (!changedPrep) {
prep.setRelation(NOMINAL_MODIFIER);
}
}
/* Rename remaining "rel" relations. */
for (SemanticGraphEdge edge : sg.findAllRelns(RELATIVE)) {
edge.setRelation(DIRECT_OBJECT);
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure 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 the 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);
}
}
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method processComplex2WP.
/**
* Processes all the two-word prepositions in TWO_WORD_PREPS_COMPLEX.
*/
private static void processComplex2WP(SemanticGraph sg, HashMap<String, HashSet<Integer>> bigrams) {
for (String bigram : TWO_WORD_PREPS_COMPLEX) {
if (bigrams.get(bigram) == null) {
continue;
}
for (Integer i : bigrams.get(bigram)) {
IndexedWord w1 = sg.getNodeByIndexSafe(i);
IndexedWord w2 = sg.getNodeByIndexSafe(i + 1);
if (w1 == null || w2 == null) {
continue;
}
SemgrexMatcher matcher = TWO_WORD_PREPS_COMPLEX_PATTERN.matcher(sg);
IndexedWord gov = null;
IndexedWord gov2 = null;
while (matcher.find()) {
if (w1.equals(matcher.getNode("w1")) && w2.equals(matcher.getNode("w2"))) {
gov = matcher.getNode("gov");
gov2 = matcher.getNode("gov2");
break;
}
}
if (gov2 == null) {
continue;
}
/* Attach the head of the prepositional phrase to
* the head of w1. */
if (sg.getRoots().contains(w1)) {
SemanticGraphEdge edge = sg.getEdge(w1, gov2);
if (edge == null) {
continue;
}
sg.removeEdge(edge);
sg.getRoots().remove(w1);
sg.addRoot(gov2);
} else {
SemanticGraphEdge edge = sg.getEdge(w1, gov2);
if (edge == null) {
continue;
}
sg.removeEdge(edge);
gov = gov == null ? sg.getParent(w1) : gov;
if (gov == null) {
continue;
}
/* Determine the relation to use. If it is a relation that can
* join two clauses and w1 is the head of a copular construction, then
* use the relation of w1 and its parent. Otherwise use the relation of edge. */
GrammaticalRelation reln = edge.getRelation();
if (sg.hasChildWithReln(w1, COPULA)) {
GrammaticalRelation reln2 = sg.getEdge(gov, w1).getRelation();
if (clauseRelations.contains(reln2)) {
reln = reln2;
}
}
sg.addEdge(gov, gov2, reln, Double.NEGATIVE_INFINITY, false);
}
/* Make children of w1 dependents of gov2. */
for (SemanticGraphEdge edge2 : sg.getOutEdgesSorted(w1)) {
sg.removeEdge(edge2);
sg.addEdge(gov2, edge2.getDependent(), edge2.getRelation(), edge2.getWeight(), edge2.isExtra());
}
createMultiWordExpression(sg, gov2, CASE_MARKER, w1, w2);
}
}
}
Aggregations