// TODO: Add param to train tree insert parser
public void run(String[] args) {
if (!ArgumentParser.validateArguments(args, TrainerToolParams.class)) {
System.err.println(getHelp());
throw new TerminateToolException(1);
}
TrainerToolParams params = ArgumentParser.parse(args,
TrainerToolParams.class);
opennlp.tools.util.TrainingParameters mlParams =
CmdLineUtil.loadTrainingParameters(params.getParams(), true);
if (mlParams != null) {
if (!TrainUtil.isValid(mlParams.getSettings("build"))) {
System.err.println("Build training parameters are invalid!");
throw new TerminateToolException(-1);
}
if (!TrainUtil.isValid(mlParams.getSettings("check"))) {
System.err.println("Check training parameters are invalid!");
throw new TerminateToolException(-1);
}
if (!TrainUtil.isValid(mlParams.getSettings("attach"))) {
System.err.println("Attach training parameters are invalid!");
throw new TerminateToolException(-1);
}
if (!TrainUtil.isValid(mlParams.getSettings("tagger"))) {
System.err.println("Tagger training parameters are invalid!");
throw new TerminateToolException(-1);
}
if (!TrainUtil.isValid(mlParams.getSettings("chunker"))) {
System.err.println("Chunker training parameters are invalid!");
throw new TerminateToolException(-1);
}
}
ObjectStream<Parse> sampleStream = openTrainingData(params.getData(), params.getEncoding());
File modelOutFile = params.getModel();
CmdLineUtil.checkOutputFile("parser model", modelOutFile);
ParserModel model;
try {
HeadRules rules = new opennlp.tools.parser.lang.en.HeadRules(
new InputStreamReader(new FileInputStream(params.getHeadRules()),
params.getEncoding()));
ParserType type = parseParserType(params.getParserType());
if (mlParams == null) {
if (ParserType.CHUNKING.equals(type)) {
model = opennlp.tools.parser.chunking.Parser.train(
params.getLang(), sampleStream, rules,
params.getIterations(), params.getCutoff());
}
else if (ParserType.TREEINSERT.equals(type)) {
model = opennlp.tools.parser.treeinsert.Parser.train(params.getLang(), sampleStream, rules, params.getIterations(),
params.getCutoff());
}
else {
throw new IllegalStateException();
}
}
else {
if (ParserType.CHUNKING.equals(type)) {
model = opennlp.tools.parser.chunking.Parser.train(
params.getLang(), sampleStream, rules,
mlParams);
}
else if (ParserType.TREEINSERT.equals(type)) {
model = opennlp.tools.parser.treeinsert.Parser.train(params.getLang(), sampleStream, rules,
mlParams);
}
else {
throw new IllegalStateException();
}
}
}
catch (IOException e) {
CmdLineUtil.printTrainingIoError(e);
throw new TerminateToolException(-1);
}
finally {
try {
sampleStream.close();
} catch (IOException e) {