【问题标题】:Writing our own models in openNLP在 openNLP 中编写我们自己的模型
【发布时间】:2014-08-14 09:19:52
【问题描述】:

如果我在命令行中使用这样的查询

./opennlp TokenNameFinder en-ner-person.bin "input.txt" "output.txt"

我将在 output.txt 中打印人名,但我想编写自己的模型以便打印自己的实体。

例如

  1. icm2500 的风险值是多少。
  2. prd_234 的交付将延迟到达。
  3. Watson 正在处理 router_34。

如果我通过这些行,它应该解析并提取 product_entities。 icm2500、prd_234、router_34...等这些都是产品(我们可以将这些信息保存在一个文件中,我们可以将其用作模型或openNLP的查找类型)。

谁能告诉我怎么做?

【问题讨论】:

    标签: java nlp opennlp named-entity-recognition


    【解决方案1】:

    您需要通过注释一些 opennlp 格式的句子来训练自己的模型。对于您发布的例句,格式如下所示:

    what is the risk value on <START:product> icm2500 <END>.
    Delivery of <START:product> prd_234 <END> will be arrived late.
    Watson is handling <START:product> router_34 <END>.
    

    确保每个句子都以换行符结尾,以及句子中是否有换行符以某种方式转义。 一旦你从你的数据中创建了一个这样的文件,那么你就可以使用 Java API 来训练这样的模型

    public static void main(String[] args){
    
    Charset charset = Charset.forName("UTF-8");
    ObjectStream<String> lineStream =
            new PlainTextByLineStream(new FileInputStream("your file in the above format"), charset);
    ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);
    
    TokenNameFinderModel model;
    
    try {
      model = NameFinderME.train("en", "person", sampleStream, TrainingParameters.defaultParams(),
                null, Collections.<String, Object>emptyMap());
    }
    finally {
      sampleStream.close();
    }
    
    try {
      modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
      model.serialize(modelOut);
    } finally {
      if (modelOut != null) 
         modelOut.close();      
    }
    
    }
    

    现在您可以将模型与名称查找器一起使用。

    因为您可能有一个明确的并且可能很短的产品名称列表,您可以考虑使用简单的正则表达式方法。

    这里有一些涉及 NameFinder 的 opennlp 文档:

    http://opennlp.apache.org/documentation/1.5.3/manual/opennlp.html#tools.namefind.training.tool
    

    【讨论】:

    • 我按照你说的做了,我的意思是我创建了自己的模型,并使用我自己的模型将这个字符串“icm2500 位于哪里”作为输入传递给代码gist.github.com/johnmiedema/7e7330e1b9263267bdfc,但没有成功。而且我也不知道正则表达式方法??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    相关资源
    最近更新 更多