您好,他们已经提到过this,this,this和this,但仍然发现很难建立自定义名称查找器模型。 这是代码:
public class CustomClassifierTrainer {
private static final TokenNameFinderFactory TokenNameFinderFactory = null;
static String onlpModelPath = "/Users/user/eclipse-workspace/openNLP/OpenNLP_models/en-ner-asiannames.bin";
// training data set
static String trainingDataFilePath = "/Users/user/eclipse-workspace/openNLP/trainingData/asiannames.txt";
public static void main(String[] args) throws IOException {
Charset charset = Charset.forName("UTF-8");
ObjectStream<String> lineStream =
new PlainTextByLineStream(new FileInputStream(trainingDataFilePath), charset);
ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);
TokenNameFinderModel model;
try {
model = NameFinderME.train("en", "asian.person", sampleStream, TrainingParameters.defaultParams(),
TokenNameFinderFactory nameFinderFactory);
}
finally {
sampleStream.close();
}
BufferedOutputStream modelOut = null;
try {
modelOut = new BufferedOutputStream(new FileOutputStream(onlpModelPath));
model.serialize(modelOut);
} finally {
if (modelOut != null)
modelOut.close();
}
}
}
尝试执行行时,我不断收到错误消息:
ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream(trainingDataFilePath), charset);
要求我转换参数1.当我将其更改为
时ObjectStream<String> lineStream = new PlainTextByLineStream((InputStreamFactory) new FileInputStream(trainingDataFilePath), charset);
然后我收到一个运行时错误消息,提示您无法强制转换。这是我Exception in thread "main" java.lang.ClassCastException: class java.io.FileInputStream cannot be cast to class opennlp.tools.util.InputStreamFactory (java.io.FileInputStream is in module java.base of loader 'bootstrap'; opennlp.tools.util.InputStreamFactory is in unnamed module of loader 'app')
at openNLP.CustomClassifierTrainer.main(CustomClassifierTrainer.java:35)
第二个问题所在的行:
try {
model = NameFinderME.train("en", "asian.person", sampleStream, TrainingParameters.defaultParams(),
TokenNameFinderFactory nameFinderFactory);
}
给出语法错误。不知道这是怎么了。由于我已经尝试了上述链接上的所有代码段,因此将不胜感激。
此致
答案 0 :(得分:1)
第一个错误:您的方法需要一个InputStreamFactory。您正在尝试传递InputStream。 InputStream不是InputStreamFactory。就像比萨饼不是汽车一样。
如果某人(编译器)向您要车,而您给他披萨,他将无法开车。通过告诉他“相信我,这匹披萨是汽车”来假装匹萨是一辆汽车(铸造就是这样做的)无法解决问题。
因此,您实际上需要传递一个InputStreamFactory。查看此接口的javadoc,您将看到它只有一个方法createInputStream()
,该方法不带任何参数,应该创建并返回InputStream。
因此,有效值为
() -> new FileInputStream(trainingDataFilePath)
即一个不需要输入并创建新输入流的lambda,因此可以将其推断为InputStreamFactory。
第二个错误甚至更简单:调用方法时,您不应该指定参数的类型。仅在定义方法时。所以
NameFinderME.train("en",
"asian.person",
sampleStream,
TrainingParameters.defaultParams(),
TokenNameFinderFactory nameFinderFactory);
应该是
NameFinderME.train("en",
"asian.person",
sampleStream,
TrainingParameters.defaultParams(),
nameFinderFactory);
练习一些简单的知识来学习Java语法。学习阅读错误消息,而不是忽略它们,以及阅读所使用类的javadoc。这很关键。