StanfordNLP custom model in java

↘锁芯ラ 提交于 2020-07-22 06:12:29

问题


I am using Stanford NLP for the first time.
Here is my code as of now:

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);

I have set property ner.additional.regexner.mapping to include my own rules.

Rule File(additional.rules) looks somewhat like this:

request count   getReq
requestcount    getReq
server details  getSer
serverdetails   getSer

where getReq and getSer are tags for the corresponding words.

When I am running my code, I am not getting the required output.

Required for the sample line - (request count for www.abcd.com):

request count  ->  getReq

Output I am getting :

---
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)

What am I doing wrong?
Please Help.


回答1:


Ok So the problem was in this line :

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

I removed it and added the following lines :

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

Now I am getting the required output



来源:https://stackoverflow.com/questions/62189911/stanfordnlp-custom-model-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!