in opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerTrainerTool.java [56:142]
public void run(String format, String[] args) {
super.run(format, args);
mlParams = CmdLineUtil.loadTrainingParameters(params.getParams(), true);
if (mlParams != null && !TrainerFactory.isValid(mlParams)) {
throw new TerminateToolException(1, "Training parameters file '" + params.getParams() +
"' is invalid!");
}
if (mlParams == null) {
mlParams = ModelUtil.createDefaultTrainingParameters();
}
File modelOutFile = params.getModel();
CmdLineUtil.checkOutputFile("pos tagger model", modelOutFile);
Map<String, Object> resources;
try {
resources = TokenNameFinderTrainerTool.loadResources(
params.getResources(), params.getFeaturegen());
}
catch (IOException e) {
throw new TerminateToolException(-1,"IO error while loading resources", e);
}
byte[] featureGeneratorBytes =
TokenNameFinderTrainerTool.openFeatureGeneratorBytes(params.getFeaturegen());
POSTaggerFactory postaggerFactory;
try {
postaggerFactory = POSTaggerFactory.create(params.getFactory(), featureGeneratorBytes,
resources, null);
} catch (InvalidFormatException e) {
throw new TerminateToolException(-1, e.getMessage(), e);
}
if (params.getDict() != null) {
try {
postaggerFactory.setTagDictionary(postaggerFactory
.createTagDictionary(params.getDict()));
} catch (IOException e) {
throw new TerminateToolException(-1,
"IO error while loading POS Dictionary", e);
}
}
if (params.getTagDictCutoff() != null) {
try {
TagDictionary dict = postaggerFactory.getTagDictionary();
if (dict == null) {
dict = postaggerFactory.createEmptyTagDictionary();
postaggerFactory.setTagDictionary(dict);
}
if (dict instanceof MutableTagDictionary) {
POSTaggerME.populatePOSDictionary(sampleStream, (MutableTagDictionary)dict,
params.getTagDictCutoff());
} else {
throw new IllegalArgumentException(
"Can't extend a POSDictionary that does not implement MutableTagDictionary.");
}
sampleStream.reset();
} catch (IOException e) {
throw new TerminateToolException(-1,
"IO error while creating/extending POS Dictionary: "
+ e.getMessage(), e);
}
}
POSModel model;
try {
model = opennlp.tools.postag.POSTaggerME.train(params.getLang(),
sampleStream, mlParams, postaggerFactory);
}
catch (IOException e) {
throw createTerminationIOException(e);
}
finally {
try {
sampleStream.close();
} catch (IOException e) {
// sorry that this can fail
}
}
CmdLineUtil.writeModel("pos tagger", modelOutFile, model);
}