use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method processNames.
/**
*
* Looks for NPs that should have the {@code name} relation and
* a) changes the structure such that the leftmost token becomes the head
* b) changes the relation from {@code compound} to {@code name}.
*
* Requires NER tags.
*
* @param sg A semantic graph.
*/
private static void processNames(SemanticGraph sg) {
if (!USE_NAME) {
return;
}
// check whether NER tags are available
IndexedWord rootToken = sg.getFirstRoot();
if (rootToken == null || !rootToken.containsKey(CoreAnnotations.NamedEntityTagAnnotation.class)) {
return;
}
SemanticGraph sgCopy = sg.makeSoftCopy();
for (SemgrexPattern pattern : NAME_PATTERNS) {
SemgrexMatcher matcher = pattern.matcher(sgCopy);
List<IndexedWord> nameParts = new ArrayList<>();
IndexedWord head = null;
while (matcher.find()) {
IndexedWord w1 = matcher.getNode("w1");
IndexedWord w2 = matcher.getNode("w2");
if (head != w1) {
if (head != null) {
processNamesHelper(sg, head, nameParts);
nameParts = new ArrayList<>();
}
head = w1;
}
if (w2.ner().equals(w1.ner())) {
nameParts.add(w2);
}
}
if (head != null) {
processNamesHelper(sg, head, nameParts);
sgCopy = sg.makeSoftCopy();
}
}
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method expandPrepConjunctions.
/**
* Expands prepositions with conjunctions such as in the sentence
* "Bill flies to and from Serbia." by copying the verb resulting
* in the following relations:
* <p/>
* {@code conj:and(flies, flies')}<br/>
* {@code case(Serbia, to)}<br/>
* {@code cc(to, and)}<br/>
* {@code conj(to, from)}<br/>
* {@code nmod(flies, Serbia)}<br/>
* {@code nmod(flies', Serbia)}<br/>
* <p/>
* The label of the conjunct relation includes the conjunction type
* because if the verb has multiple cc relations then it can be impossible
* to infer which coordination marker belongs to which conjuncts.
*
* @param sg A SemanticGraph for a sentence
*/
private static void expandPrepConjunctions(SemanticGraph sg) {
/* Semgrexes require a graph with a root. */
if (sg.getRoots().isEmpty())
return;
SemanticGraph sgCopy = sg.makeSoftCopy();
SemgrexMatcher matcher = PREP_CONJP_PATTERN.matcher(sgCopy);
IndexedWord oldGov = null;
IndexedWord oldCcDep = null;
List<IndexedWord> conjDeps = Generics.newLinkedList();
while (matcher.find()) {
IndexedWord ccDep = matcher.getNode("cc");
IndexedWord conjDep = matcher.getNode("conj");
IndexedWord gov = matcher.getNode("gov");
if (oldGov != null && (!gov.equals(oldGov) || !ccDep.equals(oldCcDep))) {
expandPrepConjunction(sg, oldGov, conjDeps, oldCcDep);
conjDeps = Generics.newLinkedList();
}
oldCcDep = ccDep;
oldGov = gov;
conjDeps.add(conjDep);
}
if (oldGov != null) {
expandPrepConjunction(sg, oldGov, conjDeps, oldCcDep);
}
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class Mention method findDependentVerb.
private static Pair<IndexedWord, String> findDependentVerb(Mention m) {
if (m.enhancedDependency.getRoots().size() == 0) {
return new Pair<>();
}
// would be nice to condense this pattern, but sadly =reln
// always uses the last relation in the sequence, not the first
SemgrexPattern pattern = SemgrexPattern.compile("{idx:" + (m.headIndex + 1) + "} [ <=reln {tag:/^V.*/}=verb | <=reln ({} << {tag:/^V.*/}=verb) ]");
SemgrexMatcher matcher = pattern.matcher(m.enhancedDependency);
while (matcher.find()) {
return Pair.makePair(matcher.getNode("verb"), matcher.getRelnString("reln"));
}
return new Pair<>();
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class Mention method findDependentVerb.
private static Pair<IndexedWord, String> findDependentVerb(Mention m) {
if (m.dependency.getRoots().size() == 0) {
return new Pair<>();
}
// would be nice to condense this pattern, but sadly =reln
// always uses the last relation in the sequence, not the first
SemgrexPattern pattern = SemgrexPattern.compile("{idx:" + (m.headIndex + 1) + "} [ <=reln {tag:/^V.*/}=verb | <=reln ({} << {tag:/^V.*/}=verb) ]");
SemgrexMatcher matcher = pattern.matcher(m.dependency);
while (matcher.find()) {
return Pair.makePair(matcher.getNode("verb"), matcher.getRelnString("reln"));
}
return new Pair<>();
}
use of edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher in project CoreNLP by stanfordnlp.
the class OpenIE method entailmentsFromClause.
/**
* Returns all of the entailed shortened clauses (as per natural logic) from the given clause.
* This runs the forward entailment component of the OpenIE system only.
* It is usually chained together with the clause splitting component: {@link OpenIE#clausesInSentence(CoreMap)}.
*
* @param clause The premise clause, as a sentence fragment in itself.
*
* @return A list of entailed clauses.
*/
@SuppressWarnings("unchecked")
public List<SentenceFragment> entailmentsFromClause(SentenceFragment clause) {
if (clause.parseTree.isEmpty()) {
return Collections.emptyList();
} else {
// Get the forward entailments
List<SentenceFragment> list = new ArrayList<>();
if (entailmentsPerSentence > 0) {
list.addAll(forwardEntailer.apply(clause.parseTree, true).search().stream().map(x -> x.changeScore(x.score * clause.score)).collect(Collectors.toList()));
}
list.add(clause);
// A special case for adjective entailments
List<SentenceFragment> adjFragments = new ArrayList<>();
SemgrexMatcher matcher = adjectivePattern.matcher(clause.parseTree);
OUTER: while (matcher.find()) {
// (get nodes)
IndexedWord subj = matcher.getNode("subj");
IndexedWord be = matcher.getNode("be");
IndexedWord adj = matcher.getNode("adj");
IndexedWord obj = matcher.getNode("obj");
IndexedWord pobj = matcher.getNode("pobj");
String prep = matcher.getRelnString("prep");
// (if the adjective, or any earlier adjective, is privative, then all bets are off)
for (SemanticGraphEdge edge : clause.parseTree.outgoingEdgeIterable(obj)) {
if ("amod".equals(edge.getRelation().toString()) && edge.getDependent().index() <= adj.index() && Util.PRIVATIVE_ADJECTIVES.contains(edge.getDependent().word().toLowerCase())) {
continue OUTER;
}
}
// (create the core tree)
SemanticGraph tree = new SemanticGraph();
tree.addRoot(adj);
tree.addVertex(subj);
tree.addVertex(be);
tree.addEdge(adj, be, GrammaticalRelation.valueOf(Language.English, "cop"), Double.NEGATIVE_INFINITY, false);
tree.addEdge(adj, subj, GrammaticalRelation.valueOf(Language.English, "nsubj"), Double.NEGATIVE_INFINITY, false);
// (add pp attachment, if it existed)
if (pobj != null) {
assert prep != null;
tree.addEdge(adj, pobj, GrammaticalRelation.valueOf(Language.English, prep), Double.NEGATIVE_INFINITY, false);
}
// (check for monotonicity)
if (adj.get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards() && be.get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards()) {
// (add tree)
adjFragments.add(new SentenceFragment(tree, clause.assumedTruth, false));
}
}
list.addAll(adjFragments);
return list;
}
}
Aggregations