use of edu.stanford.nlp.semgraph.semgrex.SemgrexPattern in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenterTest method testVPOnlyReplacedWith.
public void testVPOnlyReplacedWith() {
String conll = "1\treplaced\t0\tconj:and\tVBD\n" + "2\twith\t5\tcase\tIN\n" + "3\ta\t5\tdet\tDT\n" + "4\tdifferent\t5\tamod\tJJ\n" + "5\ttype\t1\tnmod:with\tNN\n" + "6\tof\t7\tcase\tIN\n" + "7\tfilter\t5\tnmod:of\tNN\n";
// Positive case
boolean matches = false;
SemanticGraph tree = mkTree(conll).first;
for (SemgrexPattern candidate : new RelationTripleSegmenter().VP_PATTERNS) {
if (candidate.matcher(tree).matches()) {
matches = true;
}
}
assertTrue(matches);
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexPattern in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method demoteQuantificationalModifiers.
private static void demoteQuantificationalModifiers(SemanticGraph sg) {
/* Semgrexes require a graph with a root. */
if (sg.getRoots().isEmpty()) {
return;
}
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.SemgrexPattern 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 prepositions 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") && !matcher.getRelnString("reln").equals("obl")) {
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") && !matcher.getRelnString("reln").equals("obl")) {
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") && !matcher.getRelnString("reln").equals("obl")) {
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;
}
}
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexPattern in project CoreNLP by stanfordnlp.
the class KBPSemgrexExtractor method matches.
/**
* Returns whether any of the given patterns match this tree.
*/
private boolean matches(CoreMap sentence, Collection<SemgrexPattern> rulesForRel, KBPInput input, SemanticGraph graph) {
if (graph == null || graph.isEmpty()) {
return false;
}
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
for (int i : input.subjectSpan) {
if ("O".equals(tokens.get(i).ner())) {
tokens.get(i).setNER(input.subjectType.name);
}
}
for (int i : input.objectSpan) {
if ("O".equals(tokens.get(i).ner())) {
tokens.get(i).setNER(input.objectType.name);
}
}
for (SemgrexPattern p : rulesForRel) {
SemgrexMatcher n = p.matcher(graph);
while (n.find()) {
IndexedWord entity = n.getNode("entity");
IndexedWord slot = n.getNode("slot");
if (entity == null) {
// really this is a hideous bug, right? these rules
// should all have entity and slot set
logger.warn("Found a relation with a missing entity: " + p);
continue;
}
if (slot == null) {
logger.warn("Found a relation with a missing slot: " + p);
continue;
}
boolean hasSubject = entity.index() >= input.subjectSpan.start() + 1 && entity.index() <= input.subjectSpan.end();
boolean hasObject = slot.index() >= input.objectSpan.start() + 1 && slot.index() <= input.objectSpan.end();
if (hasSubject && hasObject) {
return true;
}
}
}
return false;
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexPattern in project CoreNLP by stanfordnlp.
the class KBPSemgrexExtractor method classify.
@Override
public Pair<String, Double> classify(KBPInput input) {
for (RelationType rel : RelationType.values()) {
if (rules.containsKey(rel) && rel.entityType == input.subjectType && rel.validNamedEntityLabels.contains(input.objectType)) {
Collection<SemgrexPattern> rulesForRel = rules.get(rel);
CoreMap sentence = input.sentence.asCoreMap(Sentence::nerTags, Sentence::dependencyGraph);
boolean matches = (matches(sentence, rulesForRel, input, sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class)) || matches(sentence, rulesForRel, input, sentence.get(SemanticGraphCoreAnnotations.AlternativeDependenciesAnnotation.class)));
if (matches) {
// logger.log("MATCH for " + rel + ". sentence:" + sentence + " with rules for " + rel);
return Pair.makePair(rel.canonicalName, 1.0);
}
}
}
return Pair.makePair(NO_RELATION, 1.0);
}
Aggregations