use of edu.stanford.nlp.pipeline.Annotator in project CoreNLP by stanfordnlp.
the class Document method coref.
/**
* Returns the coref chains in the document. This is a map from coref cluster IDs, to the coref chain
* with that ID.
* @param props The properties to use in the {@link edu.stanford.nlp.pipeline.DeterministicCorefAnnotator}.
*/
public Map<Integer, CorefChain> coref(Properties props) {
synchronized (this.impl) {
if (impl.getCorefChainCount() == 0) {
// Run prerequisites
// default is rule mention annotator
this.runLemma(props).runNER(props).runParse(props);
// Run mention
Supplier<Annotator> mention = (props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultMention : getOrCreate(STANFORD_MENTION, props, () -> backend.mention(props));
// Run coref
Supplier<Annotator> coref = (props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultCoref : getOrCreate(STANFORD_COREF, props, () -> backend.coref(props));
Annotation ann = asAnnotation(true);
mention.get().annotate(ann);
coref.get().annotate(ann);
// Convert to proto
synchronized (serializer) {
for (CorefChain chain : ann.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
impl.addCorefChain(serializer.toProto(chain));
}
}
}
Map<Integer, CorefChain> corefs = Generics.newHashMap();
for (CoreNLPProtos.CorefChain chain : impl.getCorefChainList()) {
corefs.put(chain.getChainID(), fromProto(chain));
}
return corefs;
}
}
use of edu.stanford.nlp.pipeline.Annotator in project CoreNLP by stanfordnlp.
the class Document method runParse.
Document runParse(Properties props) {
if (this.sentences != null && this.sentences.size() > 0 && this.sentences.get(0).rawSentence().hasParseTree()) {
return this;
}
// Run annotator
boolean cacheAnnotation = false;
Annotator parse = ((props == EMPTY_PROPS || props == SINGLE_SENTENCE_DOCUMENT) ? defaultParse : getOrCreate(STANFORD_PARSE, props, () -> backend.parse(props))).get();
if (parse.requires().contains(CoreAnnotations.PartOfSpeechAnnotation.class) || System.getenv("CORENLP_HOST") != null) {
// Run the POS tagger if we are (or may be) using the shift reduce parser
runPOS(props);
cacheAnnotation = true;
} else {
sentences();
}
Annotation ann = asAnnotation(cacheAnnotation);
parse.annotate(ann);
// Update data
synchronized (serializer) {
for (int i = 0; i < sentences.size(); ++i) {
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(i);
Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
Tree binaryTree = sentence.get(TreeCoreAnnotations.BinarizedTreeAnnotation.class);
sentences.get(i).updateParse(serializer.toProto(tree), binaryTree == null ? null : serializer.toProto(binaryTree));
sentences.get(i).updateDependencies(ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class)), ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class)), ProtobufAnnotationSerializer.toProto(sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class)));
}
}
return this;
}
use of edu.stanford.nlp.pipeline.Annotator in project CoreNLP by stanfordnlp.
the class RuleBasedCorefMentionFinder method getParser.
private Annotator getParser() {
if (parserProcessor == null) {
Annotator parser = StanfordCoreNLP.getExistingAnnotator("parse");
if (parser == null) {
// TODO: these assertions rule out the possibility of alternately named parse/pos annotators
throw new AssertionError("Failed to get parser - this should not be possible");
}
if (parser.requires().contains(CoreAnnotations.PartOfSpeechAnnotation.class)) {
Annotator tagger = StanfordCoreNLP.getExistingAnnotator("pos");
if (tagger == null) {
throw new AssertionError("Parser required tagger, but failed to find the pos annotator");
}
List<Annotator> annotators = Generics.newArrayList();
annotators.add(tagger);
annotators.add(parser);
parserProcessor = new AnnotationPipeline(annotators);
} else {
parserProcessor = parser;
}
}
return parserProcessor;
}
use of edu.stanford.nlp.pipeline.Annotator in project CoreNLP by stanfordnlp.
the class Document method sentences.
/**
* Get the sentences in this document, as a list.
* @param props The properties to use in the {@link edu.stanford.nlp.pipeline.WordsToSentencesAnnotator}.
* @return A list of Sentence objects representing the sentences in the document.
*/
protected List<Sentence> sentences(Properties props, Annotator tokenizer) {
if (sentences == null) {
Annotator ssplit = props == EMPTY_PROPS ? defaultSSplit : getOrCreate(STANFORD_SSPLIT, props, () -> backend.wordToSentences(props)).get();
// Annotate
Annotation ann = new Annotation(this.impl.getText());
tokenizer.annotate(ann);
ssplit.annotate(ann);
// (docid)
if (ann.containsKey(CoreAnnotations.DocIDAnnotation.class)) {
impl.setDocID(ann.get(CoreAnnotations.DocIDAnnotation.class));
}
// (sentences)
List<CoreMap> sentences = ann.get(CoreAnnotations.SentencesAnnotation.class);
this.sentences = new ArrayList<>(sentences.size());
for (CoreMap sentence : sentences) {
//Sentence sent = new Sentence(this, sentence);
Sentence sent = new Sentence(this, this.serializer.toProtoBuilder(sentence), sentence.get(CoreAnnotations.TextAnnotation.class), defaultProps);
this.sentences.add(sent);
this.impl.addSentence(sent.serialize());
}
}
return sentences;
}
use of edu.stanford.nlp.pipeline.Annotator in project CoreNLP by stanfordnlp.
the class Sentence method openieTriples.
/**
* Get the OpenIE triples associated with this sentence.
* Note that this function may be slower than you would expect, as it has to
* convert the underlying Protobuf representation back into {@link CoreLabel}s.
*
* @param props The properties to use for the OpenIE annotator.
* @return A collection of {@link RelationTriple} objects representing the OpenIE triples in the sentence.
*/
public Collection<RelationTriple> openieTriples(Properties props) {
document.runOpenie(props);
synchronized (impl) {
List<CoreLabel> tokens = asCoreLabels();
Annotation doc = document.asAnnotation();
return impl.getOpenieTripleList().stream().map(x -> ProtobufAnnotationSerializer.fromProto(x, doc, this.sentenceIndex())).collect(Collectors.toList());
}
}
Aggregations