【问题标题】:OpenNLP MaxEnt - ContextGenerator and EventStreamOpenNLP MaxEnt - ContextGenerator 和 EventStream
【发布时间】:2014-09-17 10:44:52
【问题描述】:

我想使用 openNLP MaxEnt 编写自己的模型,为此我想实现 ContextGenerator 和 EventStream 接口(如文档中所述)。我查看了 openNLP Chuncker、POSTagger 和 NameFinder 的这些实现,但所有这些实现都使用了已弃用的“Pair”,并且仅查看代码我不明白它们各自的 ContextGenerators 在做什么。我将创建的模型将通过查看每个令牌的 POS 标签将每个令牌分类为 RoomNumber 或非 RoomNumber。我应该如何开始为这个模型编写 ContextGenerator 和 EventStream。我知道上下文是什么以及功能是什么,但我不知道 ContextGenerator 做什么以及 EvenStream 做什么。我确实看过 openNLP maxent 页面,但没有帮助。请帮助我理解这一点,谢谢。

【问题讨论】:

  • 您是否对使用来自 OpenNLP 的纯 Maxent 分类器或使用 maxent 的更高级别的 API 感兴趣?

标签: opennlp maxent


【解决方案1】:

以下代码可能会有所帮助,尽管它没有明确使用ContextGenerator。 实际上,BasicContextGeneratorBasicEventStream 中使用,它只是将每个输入字符串拆分为一个特征列表。

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

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

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
    }

}

【讨论】:

    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多