[英]Stanford Core NLP pipeline
我正在尝试使用NER标签创建管道。
如何以这种方式获得NER标签?
触发错误的行: String nerrr = token.ner();
码:
public class NLPpipeline {
public AnnotationPipeline buildPipeline() {
Properties props = new Properties();
AnnotationPipeline pl = new AnnotationPipeline();
pl.addAnnotator( new TokenizerAnnotator( false ) );
pl.addAnnotator( new WordsToSentencesAnnotator( false ) );
pl.addAnnotator( new POSTaggerAnnotator( false ) );
pl.addAnnotator( new MorphaAnnotator( false ) );
pl.addAnnotator(new TimeAnnotator("sutime", props));
return pl;
}
public static void main(String[] args) {
NLPpipeline nlp = new NLPpipeline();
AnnotationPipeline pipeline = nlp.buildPipeline();
Annotation annotation = new Annotation( "Last summer, Sali and Nadav met every Tuesday afternoon, from 1:00 pm to 3:00 pm." );
pipeline.annotate( annotation );
for (CoreMap sentence : sentences) {
for (CoreLabel token : sentence.get( CoreAnnotations.TokensAnnotation.class )) {
String word = token.word();
String pos = token.tag();
String nerrr = token.ner();
String role = token.lemma();
System.out.println( "=====\n" + word );
System.out.println( pos );
System.out.println( nerrr );
System.out.println( role );
}
}
}
非常感谢您的回答。 我试图创建一个如您所描述的管道,但是它非常慢,因为我的文本很长,必须将其划分为句子,每次加载NER文件时,每个句子大约需要45秒。 我的项目是将用户案例转换为测试用例,我需要确定用户案例中的实体。 我意识到我曾经有机会创建部门:SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer(); sentimentAnalyzer.initializeCoreNLP(); //仅运行一次并一次发送,但是我不知道该怎么做
您尚未在AnnotationPipeline
包含NERAnnotator。 通常,我建议使用“ Properties
而不是通过注释器类显式创建管道。 这具有许多优点:
对于您的用例,您可以运行:
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = new Annotation( "Last summer, Sali and Nadav met every Tuesday afternoon, from 1:00 pm to 3:00 pm." );
pipeline.annotate( annotation );
...
有关更多信息,请参见官方文档 。
另外,您也可以尝试使用简单API :
Document doc = new Document("Last summer, Sali and Nadav met every Tuesday afternoon, from 1:00 pm to 3:00 pm.")
doc.sentence(0).ner(3); // returns 'PERSON'
doc.sentence(0).nerTags(); // returns [O, O, O, PERSON, O, PERSON, ...]
或者,如果您知道您的输入只是一个句子,则只需:
Sentence sent = new Document("Last summer, Sali and Nadav met every Tuesday afternoon, from 1:00 pm to 3:00 pm.")
sent.ner(3); // returns 'PERSON'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.