首页 文章

斯坦福核心NLP管道

提问于
浏览
0

我正在尝试使用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(); //只运行一次并一次发送,但我不明白我应该怎么做

1 回答

  • 0

    您没有在 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 );
    
    ...
    

    有关更多信息,请参见the official documentation .

    或者,您也可以尝试使用Simple 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'
    

相关问题