Training own model in opennlp

后端 未结 4 1566
灰色年华
灰色年华 2020-12-29 06:00

I am finding it difficult to create my own model openNLP. Can any one tell me, how to own model. How the training shouls be done.

What should be the input and where

4条回答
  •  情书的邮戳
    2020-12-29 06:25

    Copy the data in data and run below code to get your own mymodel.bin .

    Can refer for data=https://github.com/mccraigmccraig/opennlp/blob/master/src/test/resources/opennlp/tools/namefind/AnnotatedSentencesWithTypes.txt

    public class Training {
           static String onlpModelPath = "mymodel.bin";
           // training data set
           static String trainingDataFilePath = "data.txt";
    
           public static void main(String[] args) throws IOException {
                           Charset charset = Charset.forName("UTF-8");
                           ObjectStream lineStream = new PlainTextByLineStream(
                                                           new FileInputStream(trainingDataFilePath), charset);
                           ObjectStream sampleStream = new NameSampleDataStream(
                                                           lineStream);
                           TokenNameFinderModel model = null;
                           HashMap mp = new HashMap();
                           try {
                                  //         model = NameFinderME.train("en","drugs", sampleStream, Collections.emptyMap(),100,4) ;
                                           model=  NameFinderME.train("en", "drugs", sampleStream, Collections. emptyMap());
                           } finally {
                                           sampleStream.close();
                           }
                           BufferedOutputStream modelOut = null;
                           try {
                                           modelOut = new BufferedOutputStream(new FileOutputStream(onlpModelPath));
                                           model.serialize(modelOut);
                           } finally {
                                           if (modelOut != null)
                                                           modelOut.close();
                           }
           }
    }
    

提交回复
热议问题