【发布时间】: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