我想使用openNLP MaxEnt编写自己的模型,因此我想实现ContextGenerator和EventStream接口(如文档中所述)。我研究了openNLP Chuncker,POSTagger和NameFinder的这些实现,但所有这些实现都使用了“Pair”,这是不推荐的,仅仅通过查看代码,我不明白他们各自的ContextGenerators在做什么。我将创建的模型将通过查看每个令牌的RoomNumber标记来将每个令牌分类为RoomNumber。我应该如何开始为这个模型编写ContextGenerator和EventStream。我知道上下文是什么,特性是什么,但我不知道ContextGenerator做什么,EvenStream做什么。我确实查看了openNLP maxent页面,但它没有任何帮助。请帮我理解一下,谢谢。
发布于 2014-11-06 17:02:10
下面的代码可能会有所帮助,尽管它没有显式使用ContextGenerator。实际上,BasicContextGenerator是在BasicEventStream中使用的,它只是将每个输入字符串拆分成一个功能列表。
例如,字符串"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
}
}https://stackoverflow.com/questions/24958095
复制相似问题