use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DependencyParserITest method testCCProcess.
/**
* Test that postprocessing like CC-processing can handle the parser
* output properly
*/
public void testCCProcess() {
Properties props = PropertiesUtils.fromString("annotators=tokenize,ssplit,pos,depparse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "Chris and John went to the store.";
Annotation document = new Annotation(text);
pipeline.annotate(document);
SemanticGraph ccProcessed = document.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
Collection<TypedDependency> dependencies = ccProcessed.typedDependencies();
GrammaticalRelation expected = UniversalEnglishGrammaticalRelations.getConj("and");
assertTrue(dependencies.stream().map(TypedDependency::reln).collect(Collectors.toList()).contains(expected));
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DocumentPreprocessor method findNextParagraphSpeaker.
private static String findNextParagraphSpeaker(Document doc, List<CoreMap> paragraph, int paragraphOffset, Dictionaries dict) {
if (paragraph.isEmpty()) {
return "";
}
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);
if (dependency == null) {
dependency = lastSent.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.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 (doc.mentionheadPositions.containsKey(headPosition) && doc.mentionheadPositions.get(headPosition).nerString.startsWith("PER")) {
speaker = Integer.toString(doc.mentionheadPositions.get(headPosition).mentionID);
}
}
}
}
}
return speaker;
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DocumentPreprocessor method findSubject.
private static boolean findSubject(Document doc, SemanticGraph dependency, IndexedWord w, int sentNum, int utterNum) {
for (Pair<GrammaticalRelation, IndexedWord> child : dependency.childPairs(w)) {
if (child.first().getShortName().equals("nsubj")) {
String subjectString = child.second().word();
// start from 1
int subjectIndex = child.second().index();
IntTuple headPosition = new IntTuple(2);
headPosition.set(0, sentNum);
headPosition.set(1, subjectIndex - 1);
String speaker;
if (doc.mentionheadPositions.containsKey(headPosition)) {
speaker = Integer.toString(doc.mentionheadPositions.get(headPosition).mentionID);
} else {
speaker = subjectString;
}
doc.speakers.put(utterNum, speaker);
return true;
}
}
return false;
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DocumentPreprocessor method isSpeaker.
/** Check one mention is the speaker of the other mention */
public static boolean isSpeaker(Mention m, Mention ant, Dictionaries dict) {
if (!dict.firstPersonPronouns.contains(ant.spanToString().toLowerCase()) || ant.number == Number.PLURAL || ant.sentNum != m.sentNum)
return false;
int countQuotationMark = 0;
for (int i = Math.min(m.headIndex, ant.headIndex) + 1; i < Math.max(m.headIndex, ant.headIndex); i++) {
String word = m.sentenceWords.get(i).get(CoreAnnotations.TextAnnotation.class);
if (word.equals("``") || word.equals("''"))
countQuotationMark++;
}
if (countQuotationMark != 1)
return false;
IndexedWord w = m.enhancedDependency.getNodeByWordPattern(m.sentenceWords.get(m.headIndex).get(CoreAnnotations.TextAnnotation.class));
if (w == null)
return false;
for (Pair<GrammaticalRelation, IndexedWord> parent : m.enhancedDependency.parentPairs(w)) {
if (parent.first().getShortName().equals("nsubj") && dict.reportVerb.contains(parent.second().get(CoreAnnotations.LemmaAnnotation.class))) {
return true;
}
}
return false;
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DependencyCorefMentionFinder method extractNPorPRPFromDependency.
private void extractNPorPRPFromDependency(CoreMap s, List<Mention> mentions, Set<IntPair> mentionSpanSet, Set<IntPair> namedEntitySpanSet) {
List<CoreLabel> sent = s.get(CoreAnnotations.TokensAnnotation.class);
SemanticGraph basic = s.get(BasicDependenciesAnnotation.class);
// DT is for "this, these, etc"
List<IndexedWord> nounsOrPrp = basic.getAllNodesByPartOfSpeechPattern("N.*|PRP.*|DT");
Tree tree = s.get(TreeAnnotation.class);
for (IndexedWord w : nounsOrPrp) {
SemanticGraphEdge edge = basic.getEdge(basic.getParent(w), w);
GrammaticalRelation rel = null;
// if edge is null, it's root
String shortname = "root";
if (edge != null) {
rel = edge.getRelation();
shortname = rel.getShortName();
}
// TODO: what to remove? remove more?
if (shortname.matches("det|compound")) {
continue;
} else {
extractMentionForHeadword(w, basic, s, mentions, mentionSpanSet, namedEntitySpanSet);
}
}
}
Aggregations