首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenNLP MaxEnt - ContextGenerator和EventStream

OpenNLP MaxEnt - ContextGenerator和EventStream
EN

Stack Overflow用户
提问于 2014-07-25 22:28:42
回答 1查看 267关注 0票数 1

我想使用openNLP MaxEnt编写自己的模型,因此我想实现ContextGenerator和EventStream接口(如文档中所述)。我研究了openNLP Chuncker,POSTagger和NameFinder的这些实现,但所有这些实现都使用了“Pair”,这是不推荐的,仅仅通过查看代码,我不明白他们各自的ContextGenerators在做什么。我将创建的模型将通过查看每个令牌的RoomNumber标记来将每个令牌分类为RoomNumber。我应该如何开始为这个模型编写ContextGenerator和EventStream。我知道上下文是什么,特性是什么,但我不知道ContextGenerator做什么,EvenStream做什么。我确实查看了openNLP maxent页面,但它没有任何帮助。请帮我理解一下,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-11-06 17:02:10

下面的代码可能会有所帮助,尽管它没有显式使用ContextGenerator。实际上,BasicContextGenerator是在BasicEventStream中使用的,它只是将每个输入字符串拆分成一个功能列表。

例如,字符串"a=1 b=2 c=1"分为3个特征:"a=1""b=2""c=1"

如果您只想使用Maxent API训练模型,然后将其用于分类,则可以使用以下适用于我的方法:

代码语言:javascript
复制
package opennlptest;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import opennlp.maxent.GIS;
import opennlp.model.Event;
import opennlp.model.EventStream;
import opennlp.model.ListEventStream;
import opennlp.model.MaxentModel;

public class TestMaxentEvents {

    static Event createEvent(String outcome, String... context) {
        return new Event(outcome, context);
    }

    public static void main(String[] args) throws IOException {

        // here are the input training samples
        List<Event> samples =  Arrays.asList(new Event[] {
                //           outcome + context
                createEvent("c=1", "a=1", "b=1"),
                createEvent("c=1", "a=1", "b=0"),
                createEvent("c=0", "a=0", "b=1"),
                createEvent("c=0", "a=0", "b=0")
        });

        // training the model
        EventStream stream = new ListEventStream(samples);
        MaxentModel model = GIS.trainModel(stream);

        // using the trained model to predict the outcome given the context
        String[] context = {"a=1", "b=0"};
        double[] outcomeProbs = model.eval(context);
        String outcome = model.getBestOutcome(outcomeProbs);

        System.out.println(outcome); // output: c=1
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24958095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档