【问题标题】:How to train Name model in OpenNLP?如何在 OpenNLP 中训练名称模型?
【发布时间】:2015-07-20 14:22:40
【问题描述】:

我正在尝试训练名称查找器模型来检测名称,但它没有给出正确的结果。 这是代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InputStream is=null;
        Resources resources=this.getResources();
        assetManager=resources.getAssets();

        String trainingDataFile = "en-ner-person.train";
        String outputModelFile = "en-ner-person.bin";
        String sentence[] = {"Sunil", "61 years old , will join the board as a nonexecutive director Nov. 29" };

        train(trainingDataFile, outputModelFile, "person");
        try {
            predict(sentence, outputModelFile);
        }
        catch(Exception e)
        {
            System.out.println("Errror Preditct" + e.getMessage());
        }
    }

    private static void train(String trainingDataFile, String outputModelFile, String tagToFind) {

        NameSampleDataStream nss = null;
        try {
            nss = new NameSampleDataStream(new PlainTextByLineStream(new java.io.FileReader(trainingDataFile)));
        } catch (Exception e) {}

        TokenNameFinderModel model = null;

        try {

            model = NameFinderME.train("en", tagToFind, nss, Collections.<String, Object>emptyMap());
        } catch(Exception e) {}

        try {
            File outFile = new File(outputModelFile);
            FileOutputStream outFileStream = new FileOutputStream(outFile);
            model.serialize(outFileStream);
        }
        catch (Exception ex) {}
    }

    private  void predict(String sentence[], String modelFile) throws Exception {
        InputStream is1 ;
        is1 = assetManager.open("en-ner-person.bin",MODE_PRIVATE);

        TokenNameFinderModel model1 = new TokenNameFinderModel(is1);

        String sd;
        NameFinderME nameFinder = new NameFinderME(model1);
        Span sp[] = nameFinder.find(sentence);

        String a[] = Span.spansToStrings(sp, sentence);
        StringBuilder fd = new StringBuilder();
        int l = a.length;

        for (int j = 0; j < l; j++) {
            fd = fd.append(a[j] + "\n");

        }
        sd = fd.toString();
        Log.d("Name Detected:", sd);

    }



}

这是我得到的输出

D:\Name 检测到:[07-20 19:35:47.516 8799:8799 I/Adreno-EGL]

en-ner-person.train的内容是:

<START:person> Sunil <END> , 61 years old , will join the board as a nonexecutive director Nov. 29 .

请帮忙。

【问题讨论】:

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


    【解决方案1】:

    试试这个:

     Span[] sp = nameFinder.find(search);
     nameFinder.clearAdaptiveData();
    
     for (Span span : sp) {
       for (int i=span.getStart(); i<span.getEnd(); i++) {
         fd.append(sentence[i] + "\n");
       }
     }
    

    另外这行好像是错的,不用再把append的内容赋值给fd了。

    fd = fd.append(a[j] + "\n");
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2017-12-01
      相关资源
      最近更新 更多