我想使用 C# 在 OpenNLP 中训练一个新模型。我在java部分使用了IKVM。这是我的火车的方法: (我在jv中引用了java.io,在op中引用了open.tools)
public string train(string lang, string entity, jv.FileInputStream taggedCorpusStream, jv.FileOutputStream modelStream)
{
//for encoding
java.nio.charset.Charset charset = java.nio.charset.Charset.forName("UTF-8");
try
{
op.util.ObjectStream lineStream = new op.util.PlainTextByLineStream(taggedCorpusStream, charset);
op.util.ObjectStream sampleStream = new op.namefind.NameSampleDataStream(lineStream);
op.namefind.TokenNameFinderModel model;
jv.OutputStream modelOut = null;
try
{
model = op.namefind.NameFinderME.train(lang, entity, sampleStream, op.util.TrainingParameters.defaultParams(), new op.namefind.TokenNameFinderFactory());
modelOut = new jv.BufferedOutputStream(modelStream);
if (model != null)
{
model.serialize(modelOut);
}
return entity + " model trained successfully";
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
sampleStream.close();
if (modelOut != null)
{
modelOut.close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return "Something goes wrong with training module.";
}
我在调用第五个参数(即 TokenNameFinderFactor)中的 NameFinder.train 时遇到 NullReference 异常。现在我的问题是它的主要用途是什么以及我可以使用哪些替代方案或方法来解决这个问题?我需要创建自己的 TokenNameFinderFactor 吗?我没有得到或理解它的文档 here关于如何实现它。 任何帮助表示赞赏。谢谢。
最佳答案
自从您发布问题以来已经有一段时间了。不确定您是否找到了解决方案。我遇到了类似的问题(更确切地说是此处描述的问题 http://permalink.gmane.org/gmane.comp.apache.opennlp.user/937 )。解决方案很简单。
将所有 IKVM dll 复制到您的项目文件夹中(调试/发布)。我只引用了一些,因此出现了错误。很可能是为此目的需要一两个 dll,但我没有费心去确定具体的 dll。
现在,我可以训练新模型了。
问候, 公羊
关于java - C# 上的 OpenNLP NameFinder 培训,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119351/