【问题标题】:unable to train location.bin using opennlp with java无法使用带有 java 的 opennlp 训练 location.bin
【发布时间】:2023-03-13 12:11:01
【问题描述】:

我正在尝试在 java 中使用 opennlp 训练 en-ner-location.bin 文件问题是我得到了以下格式的训练文本文件 <START:location> Fontana <END> <START:location> Palo Verde <END> <START:location> Picacho <END>

我使用以下代码训练了文件

import java.io.BufferedOutputStream;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.nio.charset.Charset;
  import java.util.Collections;
  import opennlp.tools.namefind.NameFinderME;
  import opennlp.tools.namefind.NameSample;
  import opennlp.tools.namefind.NameSampleDataStream;
  import opennlp.tools.namefind.TokenNameFinderModel;
  import opennlp.tools.tokenize.Tokenizer;
  import opennlp.tools.tokenize.TokenizerME;
  import opennlp.tools.tokenize.TokenizerModel;
  import opennlp.tools.util.ObjectStream;
  import opennlp.tools.util.PlainTextByLineStream;
  import opennlp.tools.util.Span;

  public class TrainNames {   
@SuppressWarnings("deprecation")
public void TrainNames() throws IOException{
    File fileTrainer=new File("citytrain.txt");
    File output=new File("en-ner-location.bin");
    ObjectStream<String> lineStream = new PlainTextByLineStream(new    FileInputStream(fileTrainer), "UTF-8");
    ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);
    System.out.println("lineStream = " + lineStream);
    TokenNameFinderModel model = NameFinderME.train("en", "location", sampleStream, Collections.<String, Object>emptyMap(), 1, 0);

    BufferedOutputStream modelOut = null;
    try {
        modelOut = new BufferedOutputStream(new FileOutputStream(output));
        model.serialize(modelOut);
    } finally {
        if (modelOut != null)
            modelOut.close();
    }
}
  }

我没有收到任何错误或警告,但是当我尝试从这样的字符串中获取城市名称时,cnt="John 计划在 UC Fontana 专攻电气工程并在 IBM 寻求职业。";它返回整个字符串 谁能告诉我为什么...??

【问题讨论】:

    标签: java eclipse opennlp


    【解决方案1】:

    欢迎来到 SO!看起来您需要更多关于每个位置注释的上下文。我相信现在 openNLP 认为您正在训练它来查找单词(任何单词),因为您的训练数据只有一个单词。您需要在整个句子中标注位置,并且至少需要几百个样本才能开始看到良好的结果。

    也请参阅此答案: How I train an Named Entity Recognizer identifier in OpenNLP?

    【讨论】:

    • 嗨,我按照您的建议更改了培训文件,我包含了 100 个包含城市名称的句子并标记了它们,但它也没有用......你认为我在哪里做错了
    • 尝试将上面对 .train 的调用更改为:TokenNameFinderModel model = NameFinderME.train("en", "location", sampleStream,null);
    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多