Hi already have referred to this, this, this and this but still finding it difficult to build a custom name finder model.. Here is the code:
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();
}
}
}
I keep getting an error when trying to execute line:
ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream(trainingDataFilePath), charset);
asking me to cast the argument 1. when I change it to
ObjectStream<String> lineStream = new PlainTextByLineStream((InputStreamFactory) new FileInputStream(trainingDataFilePath), charset);
then I get a runtime error saying you cant cast this. Here is the error when I cast it 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)
The second issue is at line:
try {
model = NameFinderME.train("en", "asian.person", sampleStream, TrainingParameters.defaultParams(),
TokenNameFinderFactory nameFinderFactory);
}
giving a syntax error. Not sure whats wrong here. Any help would be appreciated as I have tried all the code snippets on the above-mentioned links.
Regards,