use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher 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.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method demoteQuantificationalModifiers.
private static void demoteQuantificationalModifiers(SemanticGraph sg) {
SemanticGraph sgCopy = sg.makeSoftCopy();
SemgrexMatcher matcher = QUANT_MOD_3W_PATTERN.matcher(sgCopy);
while (matcher.findNextMatchingNode()) {
IndexedWord w1 = matcher.getNode("w1");
IndexedWord w2 = matcher.getNode("w2");
IndexedWord w3 = matcher.getNode("w3");
IndexedWord gov = matcher.getNode("gov");
demoteQmodParentHelper(sg, gov, w2);
List<IndexedWord> otherDeps = Generics.newLinkedList();
otherDeps.add(w1);
otherDeps.add(w2);
otherDeps.add(w3);
demoteQmodMWEHelper(sg, otherDeps, gov, w2);
}
for (SemgrexPattern p : QUANT_MOD_2W_PATTERNS) {
sgCopy = sg.makeSoftCopy();
matcher = p.matcher(sgCopy);
while (matcher.findNextMatchingNode()) {
IndexedWord w1 = matcher.getNode("w1");
IndexedWord w2 = matcher.getNode("w2");
IndexedWord gov = matcher.getNode("gov");
demoteQmodParentHelper(sg, gov, w1);
List<IndexedWord> otherDeps = Generics.newLinkedList();
otherDeps.add(w1);
otherDeps.add(w2);
demoteQmodMWEHelper(sg, otherDeps, gov, w1);
}
}
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method addCaseMarkerInformation.
/**
* Adds the case marker(s) to all nmod, acl and advcl relations that are
* modified by one or more case markers(s).
*
* @param enhanceOnlyNmods If this is set to true, then prepositons will only be appended to nmod
* relations (and not to acl or advcl) relations.
*
* @see UniversalEnglishGrammaticalStructure#addCaseMarkersToReln
*/
private static void addCaseMarkerInformation(SemanticGraph sg, boolean enhanceOnlyNmods) {
/* Semgrexes require a graph with a root. */
if (sg.getRoots().isEmpty())
return;
/* passive agent */
SemanticGraph sgCopy = sg.makeSoftCopy();
SemgrexMatcher matcher = PASSIVE_AGENT_PATTERN.matcher(sgCopy);
while (matcher.find()) {
IndexedWord caseMarker = matcher.getNode("c1");
IndexedWord gov = matcher.getNode("gov");
IndexedWord mod = matcher.getNode("mod");
addPassiveAgentToReln(sg, gov, mod, caseMarker);
}
List<IndexedWord> oldCaseMarkers = Generics.newArrayList();
/* 3-word prepositions */
for (SemgrexPattern p : PREP_MW3_PATTERNS) {
sgCopy = sg.makeSoftCopy();
matcher = p.matcher(sgCopy);
while (matcher.find()) {
if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
continue;
}
List<IndexedWord> caseMarkers = Generics.newArrayList(3);
caseMarkers.add(matcher.getNode("c1"));
caseMarkers.add(matcher.getNode("c2"));
caseMarkers.add(matcher.getNode("c3"));
Collections.sort(caseMarkers);
/* We only want to match every case marker once. */
if (caseMarkers.equals(oldCaseMarkers))
continue;
IndexedWord gov = matcher.getNode("gov");
IndexedWord mod = matcher.getNode("mod");
addCaseMarkersToReln(sg, gov, mod, caseMarkers);
oldCaseMarkers = caseMarkers;
}
}
/* 2-word prepositions */
for (SemgrexPattern p : PREP_MW2_PATTERNS) {
sgCopy = sg.makeSoftCopy();
matcher = p.matcher(sgCopy);
while (matcher.find()) {
if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
continue;
}
List<IndexedWord> caseMarkers = Generics.newArrayList(2);
caseMarkers.add(matcher.getNode("c1"));
caseMarkers.add(matcher.getNode("c2"));
Collections.sort(caseMarkers);
/* We only want to match every case marker once. */
if (caseMarkers.equals(oldCaseMarkers))
continue;
IndexedWord gov = matcher.getNode("gov");
IndexedWord mod = matcher.getNode("mod");
addCaseMarkersToReln(sg, gov, mod, caseMarkers);
oldCaseMarkers = caseMarkers;
}
}
/* Single-word prepositions */
for (SemgrexPattern p : PREP_PATTERNS) {
sgCopy = sg.makeSoftCopy();
matcher = p.matcher(sgCopy);
while (matcher.find()) {
if (enhanceOnlyNmods && !matcher.getRelnString("reln").equals("nmod")) {
continue;
}
List<IndexedWord> caseMarkers = Generics.newArrayList(1);
caseMarkers.add(matcher.getNode("c1"));
if (caseMarkers.equals(oldCaseMarkers))
continue;
IndexedWord gov = matcher.getNode("gov");
IndexedWord mod = matcher.getNode("mod");
addCaseMarkersToReln(sg, gov, mod, caseMarkers);
oldCaseMarkers = caseMarkers;
}
}
}
Aggregations