也有同样的问题!我得到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) {
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...");
}
茅侃侃
慕容森
相关分类