StanfordNLP在java中的自定义模型

问题描述 投票:1回答:1

我是第一次使用斯坦福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.rule)看起来有点像这样。

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)

我做错了什么?请帮助我。

java stanford-nlp
1个回答
1
投票

好的,所以问题出在这一行。

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

我删除了它,并添加了下面的行。

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

现在我得到了所需的输出

© www.soinside.com 2019 - 2024. All rights reserved.