本文整理汇总了Java中edu.stanford.nlp.pipeline.StanfordCoreNLP.annotate方法的典型用法代码示例。如果您正苦于以下问题:Java StanfordCoreNLP.annotate方法的具体用法?Java StanfordCoreNLP.annotate怎么用?Java StanfordCoreNLP.annotate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.stanford.nlp.pipeline.StanfordCoreNLP
的用法示例。
在下文中一共展示了StanfordCoreNLP.annotate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractSentences
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
/**
* Split a document into array of sentences
*
* @param text
* @return
* @throws Exception
*/
public static String[] extractSentences(String text) throws Exception {
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit");
StanfordCoreNLP pipeline = new StanfordCoreNLP();
Annotation document = new Annotation(text);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
String[] sentenceList = new String[sentences.size()];
for (int i = 0; i < sentenceList.length; i++) {
CoreMap sentence = sentences.get(i);
sentenceList[i] = sentence.toString();
}
return sentenceList;
}
示例2: annotate
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
protected void annotate(StanfordCoreNLP pipeline, Annotation ann) {
if (ann.get(CoreAnnotations.SentencesAnnotation.class) == null) {
pipeline.annotate(ann);
}
else {
if (ann.get(CoreAnnotations.SentencesAnnotation.class).size() == 1) {
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
token.remove(NaturalLogicAnnotations.OperatorAnnotation.class);
token.remove(NaturalLogicAnnotations.PolarityAnnotation.class);
}
sentence.remove(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
sentence.remove(NaturalLogicAnnotations.EntailedSentencesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
pipeline.annotate(ann);
}
}
}
示例3: ExtractPosTagsFile
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
@Override
public List<ExtractPosTag> ExtractPosTagsFile(File filePath) throws Exception {
List<String> lstData=ExtractData(filePath);
List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
for(String str:lstData)
{
Annotation annotation = new Annotation(str);
pipeline.annotate(annotation);
List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
for(CoreMap map:senten)
{
map.get(TokensAnnotation.class).stream().forEach((tok) -> {
String PosTagg=tok.get(PartOfSpeechAnnotation.class);
lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),PosTagg));
});
}
}
return lstTaggedSentences;
}
示例4: ExtractPosTags
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
@Override
public List<ExtractPosTag> ExtractPosTags(List<String> inputData)
{
List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
for(String str:inputData)
{
Annotation annotation = new Annotation(str);
pipeline.annotate(annotation);
List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
for(CoreMap map:senten)
{
map.get(TokensAnnotation.class).stream().forEach((tok) -> {
String getPosTag=tok.get(PartOfSpeechAnnotation.class);
lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),getPosTag));
});
}
}
return lstTaggedSentences;
}
示例5: ExtractPosTagsSentence
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
@Override
public List<ExtractPosTag> ExtractPosTagsSentence(String sentence)
{
List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = new Annotation(sentence);
pipeline.annotate(annotation);
List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
for(CoreMap map:senten)
{
map.get(TokensAnnotation.class).stream().forEach((tok) -> {
String getPosTag=tok.get(PartOfSpeechAnnotation.class);
lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),getPosTag));
});
}
return lstTaggedSentences;
}
示例6: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main(String[] args){
try{
Properties props = StringUtils.argsToProperties(args);
// props.setProperty("annotators", "tokenize,ssplit,lemma,pos,parse,ner");
StanfordCoreNLP pipeline = new StanfordCoreNLP();
String sentence = "John Gerspach was named Chief Financial Officer of Citi in July 2009.";
Annotation doc = new Annotation(sentence);
pipeline.annotate(doc);
RelationExtractorAnnotator r = new RelationExtractorAnnotator(props);
r.annotate(doc);
for(CoreMap s: doc.get(CoreAnnotations.SentencesAnnotation.class)){
System.out.println("For sentence " + s.get(CoreAnnotations.TextAnnotation.class));
List<RelationMention> rls = s.get(RelationMentionsAnnotation.class);
for(RelationMention rl: rls){
System.out.println(rl.toString());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
示例7: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main(String[] s) {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// read some text in the text variable
String text = "\"But I do not want to go among mad people,\" Alice remarked.\n" +
"\"Oh, you can not help that,\" said the Cat: \"we are all mad here. I am mad. You are mad.\"\n" +
"\"How do you know I am mad?\" said Alice.\n" +
"\"You must be,\" said the Cat, \"or you would not have come here.\" This is awful, bad, disgusting";
// create an empty Annotation just with the given text
Annotation document = new Annotation(text);
// run all Annotators on this text
pipeline.annotate(document);
List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
System.out.println(sentiment + "\t" + sentence);
}
}
示例8: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, ita_morpho, ita_lemma");
props.setProperty("customAnnotatorClass.ita_lemma", "eu.fbk.dh.digimorph.annotator.DigiLemmaAnnotator");
props.setProperty("customAnnotatorClass.ita_morpho", "eu.fbk.dh.digimorph.annotator.DigiMorphAnnotator");
props.setProperty("tokenize.language", "Spanish");
props.setProperty("ssplit.newlineIsSentenceBreak", "always");
props.setProperty("pos.model", "/Users/alessio/Documents/Resources/ita-models/italian5.tagger");
props.setProperty("ita_morpho.model", "/Users/alessio/Documents/Resources/ita-models/italian.db");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
LOGGER.info("Annotating");
for (int i = 0; i < 50; i++) {
Annotation annotation = new Annotation("Questa è una prova.");
pipeline.annotate(annotation);
}
LOGGER.info("Finishing");
Thread.sleep(60000);
}
示例9: tokenizeFromFile
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
/**
* Reads stdin through a buffer and runs only the tokenize annotator to the
* StanfordCoreNLP pipeline. Tokens are printed to stdout one per line.
*
* @param filePath
* The file to run the tagger on
*/
private static void tokenizeFromFile(String filePath) {
Properties properties = new Properties();
properties.setProperty("annotators", "tokenize");
StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
PrintWriter out = new PrintWriter(System.out);
File dataFile = new File(filePath);
Annotation annotation = null;
try {
annotation = new Annotation(IOUtils.slurpFile(dataFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pipeline.annotate(annotation);
for (CoreMap token : annotation
.get(CoreAnnotations.TokensAnnotation.class)) {
ArrayCoreMap aToken = (ArrayCoreMap) token;
out.println(aToken.toShortString(' ', "Text"));
out.flush();
}
}
示例10: en
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void en(final String[] a) {
/*
* for (Entity e : new NERStanford().retrieve(FoxConst.EXAMPLE_1)) NERStanford.LOG.info(e);
*/
final Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, relation");
final StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
final Annotation ann =
new Annotation("Stanford University is located in California. It is a great university.");
pipeline.annotate(ann);
for (final CoreMap sentence : ann.get(SentencesAnnotation.class)) {
for (final CoreLabel token : sentence.get(TokensAnnotation.class)) {
System.out.println(token.get(NamedEntityTagAnnotation.class));
System.out.println(token.get(CoreAnnotations.AnswerAnnotation.class));
/*
* Tree tree = sentence.get(TreeAnnotation.class); System.out.println(tree);
* System.out.println(tree.score());
*/
}
}
}
示例11: getLemmaCoreNLP
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
static String getLemmaCoreNLP (StanfordCoreNLP pipeline, String form) {
String tokenLemma =form;
//System.out.println("form = " + form);
Annotation tokenAnnotation = new Annotation(form);
if (tokenAnnotation == null) {
//System.out.println("form = " + form);
} else {
try {
pipeline.annotate(tokenAnnotation); // necessary for the LemmaAnnotation to be set.
List<CoreMap> list = tokenAnnotation.get(CoreAnnotations.SentencesAnnotation.class);
tokenLemma = list
.get(0).get(CoreAnnotations.TokensAnnotation.class)
.get(0).get(CoreAnnotations.LemmaAnnotation.class);
} catch (Exception e) {
//e.printStackTrace();
//System.out.println("form = " + form);
}
}
return tokenLemma;
}
示例12: parse
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
/**
* Given a CoreNLP pipeline and an input sentence, generate dependency parse for the sentence and return
* the SemanticGraph object as a result
* @param pipeline - CoreNLP pipeline
* @param snt - input sentence
* @return dependency parse in SemanticGraph object
*/
public static SemanticGraph parse(StanfordCoreNLP pipeline, String snt) {
Annotation document = new Annotation(snt);
pipeline.annotate(document);
//A CoreMap is a sentence with annotations
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
SemanticGraph semanticGraph = null;
for(CoreMap sentence: sentences) {
semanticGraph = sentence.get(BasicDependenciesAnnotation.class);
}
return semanticGraphUniversalEnglishToEnglish(semanticGraph);
}
示例13: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main (String[] args) {
// Create the Stanford CoreNLP pipeline
Properties props = PropertiesUtils.asProperties("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie",
"parse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz",
"depparse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// Annotate an example document.
String text = "I went into my bedroom and flipped the light switch. Oh, I see that the ceiling lamp is not turning on." +
" It must be that the light bulb needs replacement. I go through my closet and find a new light bulb that will fit" +
" this lamp and place it in my pocket. I also get my stepladder and place it under the lamp. I make sure the light" +
" switch is in the off position. I climb up the ladder and unscrew the old light bulb. I place the old bulb in my " +
"pocket and take out the new one. I then screw in the new bulb. I climb down the stepladder and place it back into " +
"the closet. I then throw out the old bulb into the recycling bin. I go back to my bedroom and turn on the light switch." +
" I am happy to see that there is again light in my room.";
Annotation doc = new Annotation(text);
pipeline.annotate(doc);
// Loop over sentences in the document
int sentNo = 0;
for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
System.out.println("Sentence #" + ++sentNo + ": " + sentence.get(CoreAnnotations.TextAnnotation.class));
// Get the OpenIE triples for the sentence
Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
// Print the triples
for (RelationTriple triple : triples) {
System.out.println(triple.confidence + "\t" + triple.subjectLemmaGloss() + "\t" + triple.relationLemmaGloss() + "\t" + triple.objectLemmaGloss());
}
System.out.println("\n");
}
}
示例14: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main (String[] args) {
String string = "I went into my bedroom and flipped the light switch. Oh, I see that the ceiling lamp is not turning on." +
" It must be that the light bulb needs replacement. I go through my closet and find a new light bulb that will fit" +
" this lamp and place it in my pocket. I also get my stepladder and place it under the lamp. I make sure the light" +
" switch is in the off position. I climb up the ladder and unscrew the old light bulb. I place the old bulb in my " +
"pocket and take out the new one. I then screw in the new bulb. I climb down the stepladder and place it back into " +
"the closet. I then throw out the old bulb into the recycling bin. I go back to my bedroom and turn on the light switch." +
" I am happy to see that there is again light in my room.";
Properties prop = new Properties();
prop.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
//prop.setProperty("parse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz");
StanfordCoreNLP pipeline = new StanfordCoreNLP(prop);
Annotation annotation = new Annotation(string);
pipeline.annotate(annotation); // add annotation to pipeline
// a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for(CoreMap sentence: sentences) {
for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) {
// this is the text of the token
String word = token.get(CoreAnnotations.TextAnnotation.class);
// this is the POS tag of the token
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
// this is the NER label of the token
//String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
System.out.print(word + "/" + pos);
}
System.out.println("\n");
// this is the parse tree of the current sentence
Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
System.out.println("parse tree:\n" + tree);
SemanticGraph dependencies = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
System.out.println("dependency graph:\n" + dependencies);
}
}
示例15: main
import edu.stanford.nlp.pipeline.StanfordCoreNLP; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
Properties properties = new Properties();
properties.setProperty("annotators", "ita_toksent, udpipe, ita_verb");
properties.setProperty("customAnnotatorClass.udpipe", "eu.fbk.fcw.udpipe.api.UDPipeAnnotator");
properties.setProperty("customAnnotatorClass.ita_toksent",
"eu.fbk.dh.tint.tokenizer.annotators.ItalianTokenizerAnnotator");
properties.setProperty("customAnnotatorClass.ita_verb",
"eu.fbk.dh.tint.verb.VerbAnnotator");
properties.setProperty("udpipe.server", "gardner");
properties.setProperty("udpipe.port", "50020");
properties.setProperty("udpipe.keepOriginal", "1");
// properties.setProperty("udpipe.model", "/Users/alessio/Desktop/model");
StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
Annotation annotation = new Annotation("Il caporale alpino Giampietro Civati caduto in combattimento il 5 dicembre 1944, come racconta Silvestri, ha scritto questo mirabile testamento: «sono figlio d’Italia, d’anni 21, non di Graziani e nemmeno di Badoglio, ma sono italiano e seguo la via che salverà l’onore d’Italia».");
pipeline.annotate(annotation);
String out = JSONOutputter.jsonPrint(annotation);
System.out.println(out);
// for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
// System.out.println(sentence.get(VerbAnnotations.VerbsAnnotation.class));
// }
} catch (Exception e) {
e.printStackTrace();
}
}