本文介绍了使用OpenNLP获取句子的解析树。卡住了榜样。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenNLP是一个关于自然语言处理的Apache项目。 NLP程序的目标之一是解析一个句子,给出其语法结构的树。例如,句子天空是蓝色的。可能被解析为

OpenNLP is an Apache project on Natural Language Processing. One of the aims of an NLP program is to parse a sentence giving a tree of its grammatical structure. For example, the sentence "The sky is blue." might be parsed as

      S
     / \
   NP   VP
  / \    | \
The sky is blue.

其中 S 是句子, NP 是名词短语, VP 是Verb-phrase。同样地,上面的树可以写成带括号的字符串,如下所示: S(NP(天空)VP(蓝色。))

where S is Sentence, NP is Noun-phrase, and VP is Verb-phrase. Equivalently the above tree can be written down as a parenthesized string like this: S(NP(The sky) VP(is blue.))

我试图能够使用OpenNLP从句子中获取带括号的字符串,但我无法获得示例代码。

I am trying to be able to get the parenthesized strings from sentences using OpenNLP, but I can't get the example code to work.

特别是,我跟随,我的代码在初始化时停滞不前 ParserModel

In particular, I am following along the last part of this tutorial and my code gets stuck at initializing ParserModel.

我已从下载了相应的二进制文件,并添加了 opennlp-tools-1.5.3.jar (包括所有以下对象的类)作为IntelliJ项目的库。另外,我将 en-parser-chunking.bin 移动到我的user.dir。

I have downloaded the appropriate binaries from here and added opennlp-tools-1.5.3.jar (which includes classes for all of the following objects) as a library to my IntelliJ project. Also, I moved en-parser-chunking.bin to my "user.dir."

以下是代码应该给我一个解析树,但它无限期地运行创建 ParserModel 对象。

The following is the code which should give me a parse tree, but it runs indefinitely at creating the ParserModel object.

    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的第一天,但​​我甚至无法让这个简单的例子工作。

It's my first day of working with OpenNLP, but I can't even get this simple example to work.

推荐答案

public static void Parse() throws InvalidFormatException, IOException {
    // http://sourceforge.net/apps/mediawiki/opennlp/index.php?title=Parser#Training_Tool
    InputStream is = new FileInputStream("en-parser-chunking.bin");

    ParserModel model = new ParserModel(is);

    Parser parser = ParserFactory.create(model);

    String sentence = "Programcreek is a very huge and useful website.";
    Parse topParses[] = ParserTool.parseLine(sentence, parser, 1);

    for (Parse p : topParses)
        p.show();

    is.close();

    /*
     * (TOP (S (NP (NN Programcreek) ) (VP (VBZ is) (NP (DT a) (ADJP (RB
     * very) (JJ huge) (CC and) (JJ useful) ) ) ) (. website.) ) )
     */
}

试试这个

这篇关于使用OpenNLP获取句子的解析树。卡住了榜样。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:19