有同样的问题!我得到 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
为什么线程无法打开并引发异常?