use of edu.stanford.nlp.trees.GrammaticalRelation 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.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalStructure method expandPrepConjunction.
/*
* Used by expandPrepConjunctions.
*/
private static void expandPrepConjunction(SemanticGraph sg, IndexedWord gov, List<IndexedWord> conjDeps, IndexedWord ccDep) {
IndexedWord caseGov = sg.getParent(gov);
if (caseGov == null)
return;
IndexedWord caseGovGov = sg.getParent(caseGov);
if (caseGovGov == null)
return;
IndexedWord conjGov = caseGovGov.getOriginal() != null ? caseGovGov.getOriginal() : caseGovGov;
GrammaticalRelation rel = sg.reln(caseGovGov, caseGov);
List<IndexedWord> newConjDeps = Generics.newLinkedList();
for (IndexedWord conjDep : conjDeps) {
//IndexedWord caseGovCopy = caseGov.makeSoftCopy();
IndexedWord caseGovGovCopy = caseGovGov.makeSoftCopy();
/* Change conj(prep-1, prep-2) to case(prep-1-gov-copy, prep-2) */
//SemanticGraphEdge edge = sg.getEdge(gov, conjDep);
//sg.removeEdge(edge);
//sg.addEdge(caseGovCopy, conjDep, CASE_MARKER, Double.NEGATIVE_INFINITY, false);
/* Add relation to copy node. */
//sg.addEdge(caseGovGovCopy, caseGovCopy, rel, Double.NEGATIVE_INFINITY, false);
sg.addEdge(conjGov, caseGovGovCopy, CONJUNCT, Double.NEGATIVE_INFINITY, false);
newConjDeps.add(caseGovGovCopy);
sg.addEdge(caseGovGovCopy, caseGov, rel, Double.NEGATIVE_INFINITY, true);
List<IndexedWord> caseMarkers = Generics.newArrayList();
caseMarkers.add(conjDep);
addCaseMarkersToReln(sg, caseGovGovCopy, caseGov, caseMarkers);
/* Attach all children except case markers of caseGov to caseGovCopy. */
//for (SemanticGraphEdge e : sg.outgoingEdgeList(caseGov)) {
// if (e.getRelation() != CASE_MARKER && ! e.getDependent().equals(ccDep)) {
// sg.addEdge(caseGovCopy, e.getDependent(), e.getRelation(), Double.NEGATIVE_INFINITY, false);
// }
// }
}
/* Attach CC node to caseGov */
//SemanticGraphEdge edge = sg.getEdge(gov, ccDep);
//sg.removeEdge(edge);
//sg.addEdge(conjGov, ccDep, COORDINATION, Double.NEGATIVE_INFINITY, false);
/* Add conjunction information for these relations already at this point.
* It could be that we add several coordinating conjunctions while collapsing
* and we might not know which conjunction belongs to which conjunct at a later
* point.
*/
addConjToReln(sg, conjGov, newConjDeps, ccDep);
}
use of edu.stanford.nlp.trees.GrammaticalRelation 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);
}
}
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class CoNLLUDocumentWriter method printSemanticGraph.
public String printSemanticGraph(SemanticGraph sg, boolean unescapeParenthesis) {
StringBuilder sb = new StringBuilder();
/* Print comments. */
for (String comment : sg.getComments()) {
sb.append(comment).append("\n");
}
for (IndexedWord token : sg.vertexListSorted()) {
/* Check for multiword tokens. */
if (token.containsKey(CoreAnnotations.CoNLLUTokenSpanAnnotation.class)) {
IntPair tokenSpan = token.get(CoreAnnotations.CoNLLUTokenSpanAnnotation.class);
if (tokenSpan.getSource() == token.index()) {
String range = String.format("%d-%d", tokenSpan.getSource(), tokenSpan.getTarget());
sb.append(String.format("%s\t%s\t_\t_\t_\t_\t_\t_\t_\t_%n", range, token.originalText()));
}
}
/* Try to find main governor and additional dependencies. */
int govIdx = -1;
GrammaticalRelation reln = null;
HashMap<Integer, String> additionalDeps = new HashMap<>();
for (IndexedWord parent : sg.getParents(token)) {
SemanticGraphEdge edge = sg.getEdge(parent, token);
if (govIdx == -1 && !edge.isExtra()) {
govIdx = parent.index();
reln = edge.getRelation();
} else {
additionalDeps.put(parent.index(), edge.getRelation().toString());
}
}
String additionalDepsString = CoNLLUUtils.toExtraDepsString(additionalDeps);
String word = token.word();
String featuresString = CoNLLUUtils.toFeatureString(token.get(CoreAnnotations.CoNLLUFeats.class));
String pos = token.getString(CoreAnnotations.PartOfSpeechAnnotation.class, "_");
String upos = token.getString(CoreAnnotations.CoarseTagAnnotation.class, "_");
String misc = token.getString(CoreAnnotations.CoNLLUMisc.class, "_");
String lemma = token.getString(CoreAnnotations.LemmaAnnotation.class, "_");
String relnName = reln == null ? "_" : reln.toString();
/* Root. */
if (govIdx == -1 && sg.getRoots().contains(token)) {
govIdx = 0;
relnName = GrammaticalRelation.ROOT.toString();
}
if (unescapeParenthesis) {
word = word.replaceAll(LRB_PATTERN, "(");
word = word.replaceAll(RRB_PATTERN, ")");
lemma = lemma.replaceAll(LRB_PATTERN, "(");
lemma = lemma.replaceAll(RRB_PATTERN, ")");
}
sb.append(String.format("%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s%n", token.index(), word, lemma, upos, pos, featuresString, govIdx, relnName, additionalDepsString, misc));
}
sb.append("\n");
return sb.toString();
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class EnglishGrammaticalRelations method getConj.
/**
* The "conj" grammatical relation. Used to collapse conjunct relations.
* They will be turned into conj_word, where "word" is a conjunction.
*
* @param conjunctionString The conjunction to make a GrammaticalRelation out of
* @return A grammatical relation for this conjunction
*/
public static GrammaticalRelation getConj(String conjunctionString) {
GrammaticalRelation result = conjs.get(conjunctionString);
if (result == null) {
synchronized (conjs) {
result = conjs.get(conjunctionString);
if (result == null) {
result = new GrammaticalRelation(Language.English, "conj", "conj_collapsed", CONJUNCT, conjunctionString);
conjs.put(conjunctionString, result);
threadSafeAddRelation(result);
}
}
}
return result;
}
Aggregations