当前位置: 首页 > 知识库问答 >
问题:

OpenNLP model builder加载项无法继续

松雅健
2023-03-14

我正在使用OpenNLP模型构建器插件来创建更好的NER模型。根据这篇文章,我使用了mark g发布的代码:

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...

但在帖子中的例子中,应该进一步如下:

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.

有人能帮我解决这个问题,这样我就可以生成一个模型了吗?我找了很多,但找不到任何关于它的好文件。非常感谢,谢谢。

共有1个答案

戚繁
2023-03-14

更正训练数据文件的路径如下:

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");

查看我的回复以获取Github上的参考

这应该会有帮助。

 类似资料:
  • 我试图建立一个本地网站,使用虚拟主机。我正在研究Mac Big sur 11.6-Mamp 6.3 /apache.下面是文件的配置: /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf 下面是文件/etc/hosts:127.0的配置。0.1本地主机255.255。255.255广播主机::1本地主机127.0。0.1 symfart。地

  • 这是密码。没有警告,文件正常。

  • 我的JavaFX项目可以在一台机器中加载图像,但是相同的代码不能在另一台机器中加载图像。 我有包结构(在src中)-/com/mypackagestructure/view/images/,它保存图像文件。 我的CSS文件位于-/com/mypackagestructure/view/Login。css 我在另一台机器上有完全相同的代码(安装了相同的操作系统、相同的Eclipse IDE和JDK/

  • 问题内容: 我对SLF4J有依赖性。我收到此错误: 这是我的Maven条目: 问题出在哪里? 编辑: 没有log4j依赖性我得到以下异常 EDIT2 :这是我得到的示例树 问题答案: 我假设您正在使用Eclipse作为开发环境。 使用捆绑的Maven版本(m2e)时,Eclipse Juno,Indigo和Kepler不会抑制消息SLF4J:无法加载类“ org.slf4j.impl.Static

  • 我刚在macbook pro(10.9)上安装了用于开发android应用程序的eclipse。我为eclipse安装了JDK1.7、android sdk和adt插件,然而,当我创建我的第一个android应用程序时,我得到了错误“未能加载project的属性文件...” 错误日志:!会话2015-03-10 09:08:33.363-------------------------------