in opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java [120:199]
public void run(String format, String[] args) {
super.run(format, args);
mlParams = CmdLineUtil.loadTrainingParameters(params.getParams(), true);
if (mlParams == null) {
mlParams = new TrainingParameters();
}
File modelOutFile = params.getModel();
byte[] featureGeneratorBytes = openFeatureGeneratorBytes(params.getFeaturegen());
// TODO: Support Custom resources:
// Must be loaded into memory, or written to tmp file until descriptor
// is loaded which defines parses when model is loaded
Map<String, Object> resources;
try {
resources = loadResources(params.getResources(), params.getFeaturegen());
}
catch (IOException e) {
throw new TerminateToolException(-1, e.getMessage(), e);
}
CmdLineUtil.checkOutputFile("name finder model", modelOutFile);
if (params.getNameTypes() != null) {
String[] nameTypes = params.getNameTypes().split(",");
sampleStream = new NameSampleTypeFilter(nameTypes, sampleStream);
}
String sequenceCodecImplName = params.getSequenceCodec();
if ("BIO".equals(sequenceCodecImplName)) {
sequenceCodecImplName = BioCodec.class.getName();
}
else if ("BILOU".equals(sequenceCodecImplName)) {
sequenceCodecImplName = BilouCodec.class.getName();
}
SequenceCodec<String> sequenceCodec;
try {
sequenceCodec = TokenNameFinderFactory.instantiateSequenceCodec(sequenceCodecImplName);
} catch (InvalidFormatException e) {
throw new TerminateToolException(-1, e.getMessage(), e);
}
TokenNameFinderFactory nameFinderFactory;
try {
nameFinderFactory = TokenNameFinderFactory.create(params.getFactory(),
featureGeneratorBytes, resources, sequenceCodec);
} catch (InvalidFormatException e) {
throw new TerminateToolException(-1, e.getMessage(), e);
}
NameSampleCountersStream counters = new NameSampleCountersStream(sampleStream);
sampleStream = counters;
TokenNameFinderModel model;
try {
model = opennlp.tools.namefind.NameFinderME.train(
params.getLang(), params.getType(), sampleStream, mlParams,
nameFinderFactory);
}
catch (IOException e) {
throw createTerminationIOException(e);
}
finally {
try {
sampleStream.close();
} catch (IOException e) {
// sorry that this can fail
}
}
counters.printSummary();
CmdLineUtil.writeModel("name finder", modelOutFile, model);
}