【问题标题】:OpenNLP model builder addon doesnt continueOpenNLP 模型构建器插件不继续
【发布时间】:2018-04-07 13:59:51
【问题描述】:

我正在使用model builder addonOpenNLP 来创建更好的NER 模型。 根据这个post,我使用了markg发布的代码:

public class ModelBuilderAddonUse {

  private static List<String> getSentencesFromSomewhere() throws Exception 
  {
      List<String> list = new ArrayList<String>();
      BufferedReader reader = new BufferedReader(new FileReader("D:\\Work\\workspaces\\default\\UpdateModel\\documentrequirements.docx"));
      String line;
      while ((line = reader.readLine()) != null) 
      {
          list.add(line);
      }
      reader.close();
      return list;

    }

  public static void main(String[] args) throws Exception {
    /**
     * establish a file to put sentences in
     */
    File sentences = new File("D:\\Work\\workspaces\\default\\UpdateModel\\sentences.text");

    /**
     * establish a file to put your NER hits in (the ones you want to keep based
     * on prob)
     */
    File knownEntities = new File("D:\\Work\\workspaces\\default\\UpdateModel\\knownentities.txt");

    /**
     * establish a BLACKLIST file to put your bad NER hits in (also can be based
     * on prob)
     */
    File blacklistedentities = new File("D:\\Work\\workspaces\\default\\UpdateModel\\blentities.txt");

    /**
     * establish a file to write your annotated sentences to
     */
    File annotatedSentences = new File("D:\\Work\\workspaces\\default\\UpdateModel\\annotatedSentences.txt");

    /**
     * establish a file to write your model to
     */
    File theModel = new File("D:\\Work\\workspaces\\default\\UpdateModel\\nl-ner-person.bin");


//------------create a bunch of file writers to write your results and sentences to a file

    FileWriter sentenceWriter = new FileWriter(sentences, true);
    FileWriter blacklistWriter = new FileWriter(blacklistedentities, true);
    FileWriter knownEntityWriter = new FileWriter(knownEntities, true);

//set some thresholds to decide where to write hits, you don't have to use these at all...
    double keeperThresh = .95;
    double blacklistThresh = .7;


    /**
     * Load your model as normal
     */
    TokenNameFinderModel personModel = new TokenNameFinderModel(new File("D:\\Work\\workspaces\\default\\UpdateModel\\nl-ner-person.bin"));
    NameFinderME personFinder = new NameFinderME(personModel);
    /**
     * do your normal NER on the sentences you have
     */
   for (String s : getSentencesFromSomewhere()) {
      sentenceWriter.write(s.trim() + "\n");
      sentenceWriter.flush();

      String[] tokens = s.split(" ");//better to use a tokenizer really
      Span[] find = personFinder.find(tokens);
      double[] probs = personFinder.probs();
      String[] names = Span.spansToStrings(find, tokens);
      for (int i = 0; i < names.length; i++) {
        //YOU PROBABLY HAVE BETTER HEURISTICS THAN THIS TO MAKE SURE YOU GET GOOD HITS OUT OF THE DEFAULT MODEL
        if (probs[i] > keeperThresh) {
          knownEntityWriter.write(names[i].trim() + "\n");
        }
        if (probs[i] < blacklistThresh) {
          blacklistWriter.write(names[i].trim() + "\n");
        }
      }
      personFinder.clearAdaptiveData();
      blacklistWriter.flush();
      knownEntityWriter.flush();
    }
    //flush and close all the writers
    knownEntityWriter.flush();
    knownEntityWriter.close();
    sentenceWriter.flush();
    sentenceWriter.close();
    blacklistWriter.flush();
    blacklistWriter.close();

    /**
     * THIS IS WHERE THE ADDON IS GOING TO USE THE FILES (AS IS) TO CREATE A NEW MODEL. YOU SHOULD NOT HAVE TO RUN THE FIRST PART AGAIN AFTER THIS RUNS, JUST NOW PLAY WITH THE
     * KNOWN ENTITIES AND BLACKLIST FILES AND RUN THE METHOD BELOW AGAIN UNTIL YOU GET SOME DECENT RESULTS (A DECENT MODEL OUT OF IT).
     */
    DefaultModelBuilderUtil.generateModel(sentences, knownEntities, blacklistedentities, theModel, annotatedSentences, "person", 3);


  }
}

它也可以运行,但我的输出在以下位置退出:

    annotated sentences: 1862
    knowns: 58
    Building Model using 1862 annotations
    reading training data...

但在post 的示例中,它应该更进一步:

Indexing events using cutoff of 5

    Computing event counts...  done. 561755 events
    Indexing...  done.
Sorting and merging events... done. Reduced 561755 events to 127362.
Done indexing.
Incorporating indexed data for training...  
done.
    Number of Event Tokens: 127362
        Number of Outcomes: 3
      Number of Predicates: 106490
...done.

谁能帮我解决这个问题,以便我生成一个模型? 我已经搜索了很多,但找不到任何关于它的好的文档。 真的很感激,谢谢。

【问题讨论】:

  • 你的编译终止了吗?看起来它在读取训练数据这么久。
  • @caffeinator13 是的,它正在终止,它只是退出。有什么想法吗?

标签: java machine-learning opennlp named-entity-recognition


【解决方案1】:

更正您的训练数据文件的路径,如下所示:

File sentences = new File("D:/Work/workspaces/default/UpdateModel/sentences.text");

而不是

File sentences = new File("D:\\Work\\workspaces\\default\\UpdateModel\\sentences.text");

更新

这是如何使用的,通过将文件添加到项目文件夹中。试试这样 -

File sentences = new File("src/training/resources/CreateModel/sentences.txt");

Check my respository for reference on Github

这应该会有所帮助。

【讨论】:

  • 这不起作用,得到以下错误:Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  • 路径现在是正确的,但是构建器仍然在Building Model using 1358 annotations reading training data...退出
  • 你有 1862 个带注释的句子,对吧?检查句子 1358 中的任何错误
  • 我不知道1862 是如何出现的,但如果我运行它,它会以Building Model using 1358 annotations 结束。当我转到sentence 1358 时,它是来自annotated sentences 的最后一行,与其他行没有什么不同。有什么想法吗?
  • 这可能与您的训练集有关。我想我的建议为时已晚@Patrick,我只是想知道问题是否已解决!进展如何?
猜你喜欢
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2015-10-04
  • 1970-01-01
  • 2020-09-30
  • 2019-02-04
  • 1970-01-01
相关资源
最近更新 更多