有同样的问题!我得到 InputSteram = null ,我使用了IntelliJ IDEA,OpenNLP 1.9.1。在Ubuntu 18.04上
public void makeDataTrainingModel() {
model = null;
System.out.println("POS model started");
//InputStream dataIn = null;
InputStreamFactory dataIn = null;
try {
dataIn = new InputStreamFactory() {
public InputStream createInputStream() throws IOException {
return NLPClassifier.class.getResourceAsStream("/home/int/src
/main/resources/en-pos.txt");
}
};
//I get null pointer here in dataIn
ObjectStream<String> lineStream = new PlainTextByLineStream((InputStreamFactory) , "UTF-8");
ObjectStream<POSSample> sampleStream = new WordTagSampleStream(lineStream);
//This train part IS NOT WORK ?
model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);
} catch (IOException e) {
// Failed to read or parse training data, training failed
e.printStackTrace();
} finally {
if (dataIn != null) {
// dataIn.close();
System.out.println("InputStreamFactory was not created!");
}
}
System.out.println("POS model done...");
System.out.println("Success generate model...");
//write Data model
OutputStream modelOut = null;
try {
String currentDir = new File("").getAbsolutePath();
modelOut = new BufferedOutputStream(new FileOutputStream(currentDir + "//src//main//resources//example-bad-model.dat"));
model.serialize(modelOut);
} catch (IOException e) {
// Failed to save model
e.printStackTrace();
} finally {
if (modelOut != null) {
try {
modelOut.close();
} catch (IOException e) {
// Failed to correctly save model.
// Written model might be invalid.
e.printStackTrace();
}
}
}
System.out.println("Model generated and treated successfully...");
}
我在inputStream中得到了空指针,并且出现了错误... 未创建InputStreamFactory!
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at
opennlp.tools.util.PlainTextByLineStream.reset(PlainTextByLineStream.java:57)
at opennlp.tools.util.PlainTextByLineStream.<init>
(PlainTextByLineStream.java:48)
at opennlp.tools.util.PlainTextByLineStream.<init>
(PlainTextByLineStream.java:39)
at NLPClassifier.makeDataTrainingModel(NLPClassifier.java:98)
at NlpProductClassifier.main(NlpProductClassifier.java:39)
数据如下:
profit_profit shell_environment 384912_CD bucks_currency
工资_利润finger_body 913964_CD美元_货币
profit_profit faith_law 3726_CD rur_currency
gain_profit game_entertainment 897444_CD dollar_currency
got_buy gift_jewelery 534841_CD rub_currency
为什么线程无法打开并引发异常?
答案 0 :(得分:0)
如果getResourceAsStream
返回null
,则表示找不到资源。
您应该检查null
并执行其他操作,例如抛出异常(在这种情况下,抛出异常(IOException
或FileNotFoundException
,因为IOException
和子类被throws
声明)-您不应让其将null
传递给其余代码。
NLPClassifier.class.getResourceAsStream("/home/int/src/main/resources/en-pos.txt")
不起作用,因为资源与Java包具有相同的结构,只是用斜杠代替了点。它不是文件系统中的路径。
将其更改为:getResourceAsStream("/en-pos.txt")
(因为您的文件位于包层次结构的根目录中)
答案 1 :(得分:0)
我按照Erwin Bolwidt的说法更改了代码,
/** I commented this part
return NLPClassifier.class.getResourceAsStream("/home/interceptor/src/main/resources/en-pos.txt");
*/
/**
Add this location of my resoures:
/Project/src/main/resources
*/
return getClass().getClassLoader().getResourceAsStream("en-pos.txt");
此后,我发现Apache OpenNLP: java.io.FileInputStream cannot be cast to opennlp.tools.util.InputStreamFactory遇到了类似的问题,但是使用了其他方法。 @schrieveslaach说
您需要一个InputStreamFactory实例,该实例将检索您的InputStream。此外, TokenNameFinderFactory不能为空!,例如 posFactory-不能为空!
/**
* Factory must not be a null. Add posModel.getFactory()
* model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);
*/
model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), posModel.getFactory());