ProGamer
ProGamer

Reputation: 420

StanfordNLP custom model in java

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.

Upvotes: 1

Views: 140

Answers (1)

ProGamer
ProGamer

Reputation: 420

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

Upvotes: 1

Related Questions