Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Found unexpected annotation while handling a name sequence"

Tags:

java

opennlp

I wanted to do my training for Named Entity Recognition functionality in OpenNLP. I wrote a piece of code according to http://opennlp.apache.org/documentation/1.5.2-incubating/manual/opennlp.html#tools.namefind

I started with a trivial example trying to train for “number” and marked all \d+ in a training file like this:

In <START:number> 1941 <END>, Paramount Pictures produced a movie version of the play.

The code is:

static String markedFile    = "C:/MyStuff/eclipse_workspace/OpenNlpTest/src/NameFinderTraining/en-ner-number-marked.train";
    static String modelFile     = "C:/MyStuff/eclipse_workspace/OpenNlpTest/src/NameFinderTraining/en-ner-number-marked.bin";

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception 
    {
        Charset charset = Charset.forName("UTF-8");
        ObjectStream<String> lineStream =
                new PlainTextByLineStream(new FileInputStream( markedFile), charset);
        ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);

        TokenNameFinderModel model;

        try 
        {
            model = NameFinderME.train("en", "person", sampleStream,
                    Collections.<String, Object>emptyMap(), 100, 5);
        }
        finally 
        {
            sampleStream.close();
        }

        BufferedOutputStream modelOut = null;
        try 
        {
            modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
            model.serialize(modelOut);
        } 
        finally 
        {
            if (modelOut != null) 
                    modelOut.close();      
        }   
    }

I got the following exception:

Computing event counts...  java.io.IOException: Found unexpected annotation while handling a name sequence: until the ###<START:number>### 1950 <END>s

My guess is “number” is not in a default annotation list. What should I do? If I need a “custom annotation” could someone give me an example.

like image 349
user1623058 Avatar asked Jan 14 '23 23:01

user1623058


1 Answers

OpenNLP throws this kind of exception when tag is not recognized properly.

Try removing any special characters after/before tag.

<END>. is invalid.
<END> . is valid. 
like image 148
Jai Bhatt Avatar answered Jan 31 '23 10:01

Jai Bhatt