2017-05-30 56 views
1

我在尝试创建自定义模型时遇到了一个问题。问题是我创建了一个基于培训文件的自定义模型,培训成功。 但是,当我使用样本输入测试模型(实际上是从训练文件本身获取它)时,这不会给出任何输出。 我甚至尝试了超过15000个句子,但它从来没有给我一个输出。 1.代码尝试: -使用Apache OpenNLP创建自定义模型时出现问题。测试模型时没有输出

package com.tcs.ai.opennlp.anothercustommodel.anothercustommodel; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
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.util.ObjectStream; 
import opennlp.tools.util.PlainTextByLineStream; 
import opennlp.tools.util.Span; 
public class AnotherCustomModel { 
public static void main(String args[]) 
    { 
     InputStream is=null; 
     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 static void predict(String sentence[], String modelFile) throws Exception { 
     InputStream is1 ; 
     is1 = new FileInputStream("en-ner-person.bin"); 
     TokenNameFinderModel model1 = new TokenNameFinderModel(is1); 
     String sd; 
     NameFinderME nameFinder = new NameFinderME(model1); 
     StringBuilder fd = new StringBuilder(); 
     Span[] sp = nameFinder.find(sentence); 
     nameFinder.clearAdaptiveData(); 
     for (Span span : sp) { 
      for (int i=span.getStart(); i<span.getEnd(); i++) { 
      fd.append(sentence[i] + "\n"); 
      } 
     } 
     sd = fd.toString();   
     System.out.println("Name Detected:[" + sd + "]"); 
    } 
} 
  • 烯NER-person.train: -

    <START:person> Sunil <END> , 61 years old , will join the board as a 
    

    非执行董事11月29日。

  • 我甚至尝试了超过15000个句子。不幸的是,没有任何错误,也没有输出也当我测试我的模型(模型的创建也成功

    +0

    你确定你的列车方法的catch块没有被击中?这是空的,所以只是问。 – markg

    +0

    是的。你可以请一个工作样本a)培训模式b)培训文件c)测试模型。这将有很大的帮助。 –

    回答

    0

    首先,你是给你的输入为:
    String sentence[] = {"Sunil", "61 years old , will join the board as a nonexecutive director Nov. 29" };

    这是不必要的,因为一个OpenNLP模型是使用maxent modeling训练。

    你应该尝试给一个句子或只是一个文本文件,并让opennlp用它模拟的话那袋提取的实体。

    希望这有助于。

    更新

    这里是link我与我在那里创建的模型,并利用它们opennlp工作。代码不言自明。如果你有任何疑问,请回到我身边。

    +0

    我尝试了所有选项,例如将训练数据放入文本文件并创建模型。甚至尝试给出15000句话。没有任何选项奏效。在测试模型时出现主要问题,虽然创建模型文件时没有错误,但输出为空白。你能否分享一个包含测试类(测试模型)的工作示例以及生成模型的代码。谢谢 –

    +0

    在我的答案更新中找到我的存储库的链接。如有任何疑问,请回复我。 – Nuwanda

    相关问题