4

我想对 OpenNLP 中的命名实体识别功能进行培训。我根据 http://opennlp.apache.org/documentation/1.5.2-incubating/manual/opennlp.html#tools.namefind写了一段代码

我从一个尝试训练“数字”的简单示例开始,并在训练文件中标记了所有 \d+,如下所示:

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

代码是:

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

我得到以下异常:

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

我的猜测是“数字”不在默认注释列表中。我应该怎么办?如果我需要“自定义注释”,有人可以给我一个例子。

4

1 回答 1

9

当标签没有被正确识别时,OpenNLP 会抛出这种异常。

尝试在标签之后/之前删除任何特殊字符。

<END>. is invalid.
<END> . is valid. 
于 2014-10-01T03:34:46.130 回答