【问题标题】:Training own model in opennlp在 opennlp 中训练自己的模型
【发布时间】:2012-06-26 09:27:49
【问题描述】:

我发现创建自己的模型 openNLP 很困难。 谁能告诉我,如何拥有模型。 应该如何进行培训。

输入应该是什么,输出模型文件将存储在哪里。

【问题讨论】:

  • 您要为哪个工具创建模型?

标签: file model opennlp


【解决方案1】:

https://opennlp.apache.org/docs/1.5.3/manual/opennlp.html

这个网站非常有用,既显示代码,又使用 OpenNLP 应用程序训练所有不同类型的模型,如实体提取和词性等。

我可以在这里给你一些代码示例,但是页面使用起来非常清晰。

理论上:

基本上你创建一个文件来列出你想要训练的东西

例如。

Sport [whitespace] 这是一个关于足球、橄榄球和其他东西的页面

政治 [空白] 这是关于托尼·布莱尔担任首相的页面。

格式在上面的页面中描述(每个模型都需要不同的格式)。创建此文件后,您可以通过 API 或 opennlp 应用程序(通过命令行)运行它,它会生成一个 .bin 文件。一旦你有了这个 .bin 文件,你就可以将它加载到模型中,然后开始使用它(根据上面网站中的 api)。

【讨论】:

【解决方案2】:

首先您需要使用所需的实体来训练数据。

句子应该用换行符 (\n) 分隔。值应与空格字符分隔并标记。
假设您要创建医学实体模型,那么数据应该是这样的:

<START:medicine> Augmentin-Duo <END> is a penicillin antibiotic that contains two medicines - <START:medicine> amoxicillin trihydrate <END> and 
<START:medicine> potassium clavulanate <END>. They work together to kill certain types of bacteria and are used to treat certain types of bacterial infections.

例如,您可以参考示例dataset。训练数据至少要有 15000 句才能得到更好的结果。

您还可以使用 Opennlp TokenNameFinderTrainer。 输出文件将采用 .bin 格式。

示例如下:Writing a custom NameFinder model in OpenNLP

更多详情,请参考Opennlp documentation

【讨论】:

    【解决方案3】:

    也许这篇文章会对你有所帮助。它描述了如何从维基百科提取的数据中进行 TokenNameFinder 训练...

    【讨论】:

      【解决方案4】:

      复制data中的数据,运行下面的代码,得到你自己的mymodel.bin。

      可以参考资料=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<String> lineStream = new PlainTextByLineStream(
                                                             new FileInputStream(trainingDataFilePath), charset);
                             ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
                                                             lineStream);
                             TokenNameFinderModel model = null;
                             HashMap<String, Object> mp = new HashMap<String, Object>();
                             try {
                                    //         model = NameFinderME.train("en","drugs", sampleStream, Collections.<String,Object>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();
                             }
             }
      }
      

      【讨论】:

      • 欢迎来到 Stack Overflow!尽管此代码可能有助于解决问题,但它并没有解释 为什么 和/或 如何 回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的回答添加解释,包括适用的限制和假设。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多