use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class QuoteAttributionUtils method constructSentence.
// taken from WordToSentencesAnnotator
private static CoreMap constructSentence(List<CoreLabel> sentenceTokens, CoreMap prevSentence, CoreMap sentence, DependencyParser parser) {
// get the sentence text from the first and last character offsets
int begin = sentenceTokens.get(0).get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
int last = sentenceTokens.size() - 1;
int end = sentenceTokens.get(last).get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
String sentenceText = prevSentence.get(CoreAnnotations.TextAnnotation.class) + sentence.get(CoreAnnotations.TextAnnotation.class);
// create a sentence annotation with text and token offsets
Annotation newSentence = new Annotation(sentenceText);
newSentence.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, begin);
newSentence.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, end);
newSentence.set(CoreAnnotations.TokensAnnotation.class, sentenceTokens);
newSentence.set(CoreAnnotations.TokenBeginAnnotation.class, prevSentence.get(CoreAnnotations.TokenBeginAnnotation.class));
newSentence.set(CoreAnnotations.TokenEndAnnotation.class, sentence.get(CoreAnnotations.TokenEndAnnotation.class));
newSentence.set(CoreAnnotations.ParagraphIndexAnnotation.class, sentence.get(CoreAnnotations.ParagraphIndexAnnotation.class));
newSentence.set(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class, getParse(newSentence, parser));
return newSentence;
// newSentence.set(CoreAnnotations.SentenceIndexAnnotation.class, sentences.size());
}
use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class SimpleSentiment method classify.
/**
* @see SimpleSentiment#classify(CoreMap)
*/
public SentimentClass classify(String text) {
Annotation ann = new Annotation(text);
pipeline.get().annotate(ann);
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);
Counter<String> features = featurize(sentence);
RVFDatum<SentimentClass, String> datum = new RVFDatum<>(features);
return impl.classOf(datum);
}
use of edu.stanford.nlp.pipeline.Annotation in project cogcomp-nlp by CogComp.
the class StanfordDepHandler method addView.
@Override
public void addView(TextAnnotation textAnnotation) throws AnnotatorException {
// If the sentence is longer than STFRD_MAX_SENTENCE_LENGTH there is no point in trying to
// parse
StanfordParseHandler.checkLength(textAnnotation, throwExceptionOnSentenceLengthCheck, maxParseSentenceLength);
TreeView treeView = new TreeView(ViewNames.DEPENDENCY_STANFORD, "StanfordDepHandler", textAnnotation, 1d);
// The (tokenized) sentence offset in case we have more than one sentences in the record
List<CoreMap> sentences = StanfordParseHandler.buildStanfordSentences(textAnnotation);
Annotation document = new Annotation(sentences);
posAnnotator.annotate(document);
parseAnnotator.annotate(document);
sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
if (sentences.get(0).get(TreeCoreAnnotations.TreeAnnotation.class).nodeString().equals("X")) {
// This is most like because we ran out of time
throw new AnnotatorException("Unable to parse TextAnnotation " + textAnnotation.getId() + ". " + "This is most likely due to a timeout.");
}
for (int sentenceId = 0; sentenceId < sentences.size(); sentenceId++) {
boolean runtimeExceptionWasThrown = false;
CoreMap sentence = sentences.get(sentenceId);
if (maxParseSentenceLength > 0 && sentence.size() > maxParseSentenceLength) {
logger.warn(HandlerUtils.getSentenceLengthError(textAnnotation.getId(), sentence.toString(), maxParseSentenceLength));
} else {
SemanticGraph depGraph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
IndexedWord root = null;
try {
root = depGraph.getFirstRoot();
} catch (RuntimeException e) {
String msg = "ERROR in getting root of dep graph for sentence. Sentence is:\n" + sentence.toString() + "'\nDependency graph is:\n" + depGraph.toCompactString() + "\nText is:\n" + textAnnotation.getText();
logger.error(msg);
System.err.println(msg);
e.printStackTrace();
if (throwExceptionOnSentenceLengthCheck)
throw e;
else
runtimeExceptionWasThrown = true;
}
if (!runtimeExceptionWasThrown) {
int tokenStart = getNodePosition(textAnnotation, root, sentenceId);
Pair<String, Integer> nodePair = new Pair<>(root.originalText(), tokenStart);
Tree<Pair<String, Integer>> tree = new Tree<>(nodePair);
populateChildren(depGraph, root, tree, textAnnotation, sentenceId);
treeView.setDependencyTree(sentenceId, tree);
}
}
}
textAnnotation.addView(getViewName(), treeView);
}
use of edu.stanford.nlp.pipeline.Annotation in project cogcomp-nlp by CogComp.
the class StanfordTrueCaseHandler method addView.
@Override
public void addView(TextAnnotation ta) throws AnnotatorException {
Annotation document = new Annotation(ta.text);
pipeline.annotate(document);
TokenLabelView vu = new TokenLabelView(viewName, ta);
for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
String trueCase = token.get(CoreAnnotations.TrueCaseTextAnnotation.class);
int beginCharOffsetS = token.beginPosition();
int endCharOffset = token.endPosition() - 1;
List<Constituent> overlappingCons = ta.getView(ViewNames.TOKENS).getConstituentsOverlappingCharSpan(beginCharOffsetS, endCharOffset);
int endIndex = overlappingCons.stream().max(Comparator.comparing(Constituent::getEndSpan)).get().getEndSpan();
Constituent c = new Constituent(trueCase, viewName, ta, endIndex - 1, endIndex);
vu.addConstituent(c);
}
}
ta.addView(viewName, vu);
}
use of edu.stanford.nlp.pipeline.Annotation in project cogcomp-nlp by CogComp.
the class StanfordCorefHandler method addView.
@Override
protected void addView(TextAnnotation ta) throws AnnotatorException {
Annotation document = new Annotation(ta.text);
pipeline.annotate(document);
CoreferenceView vu = new CoreferenceView(viewName, ta);
Map corefChain = document.get(CorefCoreAnnotations.CorefChainAnnotation.class);
for (Object key : corefChain.keySet()) {
CorefChain chain = (CorefChain) corefChain.get(key);
Constituent representative = createConstituentGivenMention(document, chain, chain.getRepresentativeMention(), ta);
List<Constituent> consList = new ArrayList<>();
for (CorefChain.CorefMention m : chain.getMentionsInTextualOrder()) {
consList.add(createConstituentGivenMention(document, chain, m, ta));
}
// remove the representative itself
consList.remove(representative);
vu.addCorefEdges(representative, consList);
}
ta.addView(viewName, vu);
}
Aggregations