如何训练POSTagger OpenNLP并将结果追溯到旧模型?

时间:2012-08-22 21:49:25

标签: java opennlp

所以,我正在尝试使用POSTagger的训练API。但我想将新训练的数据附加到旧模型中。或者,如果我想多次训练,我会有很多模型文件。我怎样才能将结果与现有模型相结合。所以,我只有一个模型有更大的数据。我认为模型文件是一个二进制文件,所以我不确定附加文件是否可以在这种情况下工作。

这是我的代码


public class POSTraining {
    private final String outputModel;
    private InputStream dataIn;

    public POSTraining() throws IOException {
        outputModel = this.getClass().getResource("/model/en-pos-maxent.bin").getPath();
        dataIn = this.getClass().getResourceAsStream("/model/en-pos.train");
    }

    public static void main(String args[]) throws IOException {

        POSTraining posTraining = new POSTraining();
        posTraining.train();
    }

    public void train() {
        try {
            ObjectStream lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
            ObjectStream sampleStream = new WordTagSampleStream(lineStream);

            TrainingParameters trainParams = new TrainingParameters();
            trainParams.put("model", ModelType.MAXENT.name());
            POSModel trainedModel = POSTaggerME.train("en", sampleStream, trainParams, null, null);

            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputModel));
            trainedModel.serialize(bufferedOutputStream);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dataIn != null) {
                try {
                    dataIn.close();
                } catch (IOException e) {
                    // Not an issue, training already finished.
                    // The exception should be logged and investigated
                    // if part of a production system.
                    e.printStackTrace();
                }
            }
        }


    }
}


1 个答案:

答案 0 :(得分:0)

NLP模型通常不可能。你无法逐步调整它们。