首页 > 解决方案 > java中的StanfordNLP自定义模型

问题描述

我是第一次使用斯坦福 NLP。
这是我现在的代码:

Properties props = new Properties();
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner");
    props.setProperty("ner.additional.regexner.mapping", "additional.rules");
    //props.setProperty("ner.applyFineGrained", "false");

    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    String content = "request count for www.abcd.com";
    CoreDocument doc = new CoreDocument(content);
    // annotate the document
    pipeline.annotate(doc);
    // view results
    System.out.println("---");
    System.out.println("entities found");
    for (CoreEntityMention em : doc.entityMentions())
      System.out.println("\tdetected entity: \t" + em.text() + "\t" + em.entityType());
    System.out.println("---");
    System.out.println("tokens and ner tags");
    String tokensAndNERTags =
        doc.tokens().stream().map(token -> "(" + token.word() + "," + token.ner() + ")")
            .collect(Collectors.joining(" "));
    System.out.println(tokensAndNERTags);

我已设置属性ner.additional.regexner.mapping以包含我自己的规则。

规则文件(additional.rules)看起来有点像这样:

request count   getReq
requestcount    getReq
server details  getSer
serverdetails   getSer

其中 getReq 和 getSer 是对应单词的标签。

当我运行我的代码时,我没有得到所需的输出。

样品行所需 - (www.abcd.com 的请求计数):

request count  ->  getReq

我得到的输出:

---
entities found
    detected entity:    count   TITLE
    detected entity:    www.abcd.com    URL
---
tokens and ner tags
(request,O) (count,TITLE) (for,O) (www.abcd.com,URL)

我究竟做错了什么?
请帮忙。

标签: javastanford-nlp

解决方案


好的所以问题出在这一行:

props.setProperty("ner.additional.regexner.mapping", "additional.rules");

我删除了它并添加了以下几行:

pipeline.addAnnotator(new TokensRegexNERAnnotator("additional.rules", true));

现在我得到了所需的输出


推荐阅读