OpenNLP 是一个关于自然语言处理的 Apache 项目。 NLP 程序的目标之一是解析一个句子,并给出其语法结构的树。例如,“天空是蓝色的”这句话。可能会被解析为
S
/ \
NP VP
/ \ | \
The sky is blue.
where S
是句子,NP
是名词短语,并且VP
是动词短语。同样,上面的树可以写成带括号的字符串,如下所示:S(NP(The sky) VP(is blue.))
我试图使用 OpenNLP 从句子中获取带括号的字符串,但我无法使示例代码正常工作。
特别是,我正在跟随本教程的最后一部分 http://www.programcreek.com/2012/05/opennlp-tutorial/#parser我的代码在初始化时卡住了ParserModel
.
我已经从以下位置下载了适当的二进制文件here http://opennlp.apache.org/cgi-bin/download.cgi并添加了opennlp-tools-1.5.3.jar
(其中包括以下所有对象的类)作为我的 IntelliJ 项目的库。还有,我搬家了en-parser-chunking.bin
到我的“user.dir”。
以下是应该给我一个解析树的代码,但它在创建时无限期地运行ParserModel
目的。
InputStream is = new FileInputStream("en-parser-chunking.bin");
ParserModel model = new ParserModel(is);
Parser parser = ParserFactory.create(model);
String sentence = "The sky is blue.";
Parse topParses[] = ParserTool.parseLine(sentence, parser, 1);
for (Parse p : topParses)
p.show();
is.close();
这是我使用 OpenNLP 的第一天,但我什至无法让这个简单的示例运行起来。