我正在尝试使用OpenNLP Java API从文档中提取名称、技能等实体。但它没有提取正确的 Names。我正在使用opennlp sourceforge 链接上提供的模型
这是一段java代码-
public class tikaOpenIntro {
public static void main(String[] args) throws IOException, SAXException,
TikaException {
tikaOpenIntro toi = new tikaOpenIntro();
toi.filest("");
String cnt = toi.contentEx();
toi.sentenceD(cnt);
toi.tokenization(cnt);
String names = toi.namefind(toi.Tokens);
toi.files(names);
}
public String Tokens[];
public String contentEx() throws IOException, SAXException, TikaException {
InputStream is = new BufferedInputStream(new FileInputStream(new File(
"/home/rahul/Downloads/rahul.pdf")));
// URL url=new URL("http://in.linkedin.com/in/rahulkulhari");
// InputStream is=url.openStream();
Parser ps = new AutoDetectParser(); // for detect parser related to
BodyContentHandler bch = new BodyContentHandler();
ps.parse(is, bch, new Metadata(), new ParseContext());
return bch.toString();
}
public void files(String st) throws IOException {
FileWriter fw = new FileWriter("/home/rahul/Documents/extrdata.txt",
true);
BufferedWriter bufferWritter = new BufferedWriter(fw);
bufferWritter.write(st + "\n");
bufferWritter.close();
}
public void filest(String st) throws IOException {
FileWriter fw = new FileWriter("/home/rahul/Documents/extrdata.txt",
false);
BufferedWriter bufferWritter = new BufferedWriter(fw);
bufferWritter.write(st);
bufferWritter.close();
}
public String namefind(String cnt[]) {
InputStream is;
TokenNameFinderModel tnf;
NameFinderME nf;
String sd = "";
try {
is = new FileInputStream(
"/home/rahul/opennlp/model/en-ner-person.bin");
tnf = new TokenNameFinderModel(is);
nf = new NameFinderME(tnf);
Span sp[] = nf.find(cnt);
String a[] = Span.spansToStrings(sp, cnt);
StringBuilder fd = new StringBuilder();
int l = a.length;
for (int j = 0; j < l; j++) {
fd = fd.append(a[j] + "\n");
}
sd = fd.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sd;
}
public void sentenceD(String content) {
String cnt[] = null;
InputStream om;
SentenceModel sm;
SentenceDetectorME sdm;
try {
om = new FileInputStream("/home/rahul/opennlp/model/en-sent.bin");
sm = new SentenceModel(om);
sdm = new SentenceDetectorME(sm);
cnt = sdm.sentDetect(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public void tokenization(String tokens) {
InputStream is;
TokenizerModel tm;
try {
is = new FileInputStream("/home/rahul/opennlp/model/en-token.bin");
tm = new TokenizerModel(is);
Tokenizer tz = new TokenizerME(tm);
Tokens = tz.tokenize(tokens);
// System.out.println(Tokens[1]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我想做的是:
- 我正在使用Apache Tika 将 PDF 文档转换为纯文本文档。
- 我正在传递纯文本文档以进行句子边界检测。
- 在这个标记化之后
- 在此名称实体提取之后
但它正在提取名称和其他单词。 它不是提取专有名称。以及如何创建自定义模型以从游泳、编程等文档中提取技能?
给我一些想法!
任何帮助将不胜感激!?