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);
}
}
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project CoreNLP by stanfordnlp.
the class UniversalDependenciesFeatureAnnotator method wasPerson.
/**
* Determine the person of "was".
*/
private static String wasPerson(SemanticGraph sg, IndexedWord word) {
IndexedWord subj = sg.getChildWithReln(word, UniversalEnglishGrammaticalRelations.NOMINAL_SUBJECT);
if (subj == null) {
subj = sg.getChildWithReln(word, UniversalEnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT);
}
if (subj != null) {
if (subj.word().equalsIgnoreCase("i")) {
/* "I" is the subject of "was". */
return "1";
}
}
IndexedWord parent = sg.getParent(word);
if (parent == null) {
return subj != null ? "3" : null;
}
SemanticGraphEdge edge = sg.getEdge(parent, word);
if (edge == null) {
return subj != null ? "3" : null;
}
if (UniversalEnglishGrammaticalRelations.AUX_MODIFIER.equals(edge.getRelation()) || UniversalEnglishGrammaticalRelations.AUX_PASSIVE_MODIFIER.equals(edge.getRelation())) {
return wasPerson(sg, parent);
}
if (UniversalEnglishGrammaticalRelations.CONJUNCT.isAncestor(edge.getRelation())) {
/* Check if the subject of the head of a conjunction is "I". */
return wasPerson(sg, parent);
}
return "3";
}
use of edu.stanford.nlp.semgraph.SemanticGraphEdge in project Info-Evaluation by TechnionYP5777.
the class AnalyzeParagragh method InteractiveAnalyze.
public InteractiveTableTuple InteractiveAnalyze() {
final String $ = getName();
final String input_date = getDate(year);
String accurate_name = "";
LinkedList<ReasonPair> reasons = InteractiveReasonFinding();
final Properties props = new Properties();
props.put("annotators", "tokenize,ssplit, pos, regexner, parse,lemma,natlog,openie");
final StanfordCoreNLP pipeLine = new StanfordCoreNLP(props);
final String inputText = input + "";
final Annotation document = new Annotation(inputText);
pipeLine.annotate(document);
for (final CoreMap sentence : document.get(SentencesAnnotation.class)) {
final SemanticGraph dependencies = sentence.get(CollapsedDependenciesAnnotation.class);
for (final IndexedWord root : dependencies.getRoots()) for (final SemanticGraphEdge edge : dependencies.getOutEdgesSorted(root)) {
final IndexedWord dep = edge.getDependent();
if ("nsubjpass".equals((edge.getRelation() + ""))) {
for (final SemanticGraphEdge keshet : dependencies.getOutEdgesSorted(dep)) {
final IndexedWord dep2 = keshet.getDependent();
final String rel2 = keshet.getRelation() + "";
if ("arrested".equals(edge.getGovernor().word()) && ((dep2.ner() != null && "PERSON".equals(dep2.ner())) || "compound".equals(rel2) || "det".equals(rel2)))
accurate_name += dep2.word() + " ";
}
accurate_name += dep.word();
}
}
}
return new InteractiveTableTuple(accurate_name.isEmpty() ? $ : accurate_name, input_date, reasons);
}
Aggregations