use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class UniversalEnglishGrammaticalRelations method getConj.
/**
* The "conj" grammatical relation. Used to enhance 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.UniversalEnglish, "conj", "conj_collapsed", CONJUNCT, conjunctionString);
conjs.put(conjunctionString, result);
threadSafeAddRelation(result);
}
}
}
return result;
}
use of edu.stanford.nlp.trees.GrammaticalRelation 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.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class IETestUtils method parseCoNLL.
/**
* Parse a CoNLL formatted string into a SemanticGraph.
* This is useful for tests so that you don't need to load the model (and are robust to
* model changes).
*
* @param conll The CoNLL format for the tree.
* @return A semantic graph, as well as the flat tokens of the sentence.
*/
public static Pair<SemanticGraph, List<CoreLabel>> parseCoNLL(String conll) {
List<CoreLabel> sentence = new ArrayList<>();
SemanticGraph tree = new SemanticGraph();
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int index = Integer.parseInt(fields[0]);
String word = fields[1];
CoreLabel label = mkWord(word, index);
sentence.add(label);
if (fields[2].equals("0")) {
tree.addRoot(new IndexedWord(label));
} else {
tree.addVertex(new IndexedWord(label));
}
if (fields.length > 4) {
label.setTag(fields[4]);
}
if (fields.length > 5) {
label.setNER(fields[5]);
}
if (fields.length > 6) {
label.setLemma(fields[6]);
}
}
int i = 0;
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int parent = Integer.parseInt(fields[2]);
String reln = fields[3];
if (parent > 0) {
tree.addEdge(new IndexedWord(sentence.get(parent - 1)), new IndexedWord(sentence.get(i)), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
}
i += 1;
}
return Pair.makePair(tree, sentence);
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class RelationTripleSegmenterTest method mkTree.
/**
* Parse a CoNLL formatted tree into a SemanticGraph object (along with a list of tokens).
*
* @param conll The CoNLL formatted tree.
*
* @return A pair of a SemanticGraph and a token list, corresponding to the parse of the sentence
* and to tokens in the sentence.
*/
protected Pair<SemanticGraph, List<CoreLabel>> mkTree(String conll) {
List<CoreLabel> sentence = new ArrayList<>();
SemanticGraph tree = new SemanticGraph();
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int index = Integer.parseInt(fields[0]);
String word = fields[1];
CoreLabel label = IETestUtils.mkWord(word, index);
sentence.add(label);
if (fields[2].equals("0")) {
tree.addRoot(new IndexedWord(label));
} else {
tree.addVertex(new IndexedWord(label));
}
if (fields.length > 4) {
label.setTag(fields[4]);
}
if (fields.length > 5) {
label.setNER(fields[5]);
}
if (fields.length > 6) {
label.setLemma(fields[6]);
}
}
int i = 0;
for (String line : conll.split("\n")) {
if (line.trim().equals("")) {
continue;
}
String[] fields = line.trim().split("\\s+");
int parent = Integer.parseInt(fields[2]);
String reln = fields[3];
if (parent > 0) {
tree.addEdge(new IndexedWord(sentence.get(parent - 1)), new IndexedWord(sentence.get(i)), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
}
i += 1;
}
return Pair.makePair(tree, sentence);
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class Document method findNextParagraphSpeaker.
private String findNextParagraphSpeaker(List<CoreMap> paragraph, int paragraphOffset, Dictionaries dict) {
CoreMap lastSent = paragraph.get(paragraph.size() - 1);
String speaker = "";
for (CoreLabel w : lastSent.get(CoreAnnotations.TokensAnnotation.class)) {
if (w.get(CoreAnnotations.LemmaAnnotation.class).equals("report") || w.get(CoreAnnotations.LemmaAnnotation.class).equals("say")) {
String word = w.get(CoreAnnotations.TextAnnotation.class);
SemanticGraph dependency = lastSent.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
IndexedWord t = dependency.getNodeByWordPattern(word);
for (Pair<GrammaticalRelation, IndexedWord> child : dependency.childPairs(t)) {
if (child.first().getShortName().equals("nsubj")) {
// start from 1
int subjectIndex = child.second().index();
IntTuple headPosition = new IntTuple(2);
headPosition.set(0, paragraph.size() - 1 + paragraphOffset);
headPosition.set(1, subjectIndex - 1);
if (mentionheadPositions.containsKey(headPosition) && mentionheadPositions.get(headPosition).nerString.startsWith("PER")) {
speaker = Integer.toString(mentionheadPositions.get(headPosition).mentionID);
}
}
}
}
}
return speaker;
}
Aggregations