分析器 OpenNLP

7 分析器

功能介绍:尝试解析器最简单的方法是在命令行工具。该工具仅用于演示和测试。请从我们网站上的英文分块解析器模型,并用以下命令启动解析工具。

代码实现:

package package01;

import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
import opennlp.tools.util.InvalidFormatException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Test07 {

    public static void main(String[] args) throws IOException {
        Test07.Parse();
    }

    /**
     * 6.分析器: Parser
     * @deprecated Given this sentence: "Programcreek is a very huge and useful website.", parser can return the following:
     * (TOP (S (NP (NN Programcreek) ) (VP (VBZ is) (NP (DT a) (ADJP (RB very) (JJ huge) (CC and) (JJ useful) ) ) ) (. website.) ) )
     * (TOP
     *     (S
     *        (NP
     *           (NN Programcreek)
     *        )
     *        (VP
     *           (VBZ is)
     *           (NP
     *              (DT a)
     *              (ADJP
     *                   (RB very)
     *                   (JJ huge)
     *                   (CC and)
     *                   (JJ userful)
     *              )
     *           )
     *        )
     *        (. website.)
     *     )
     * )
     * @param str
     */
    public static void Parse() throws InvalidFormatException, IOException {
        // http://sourceforge.net/apps/mediawiki/opennlp/index.php?title=Parser#Training_Tool
        InputStream is = new FileInputStream("E:\\NLP_Practics\\models\\en-parser-chunking.bin");
        ParserModel model = new ParserModel(is);
        Parser parser = ParserFactory.create(model);
        String sentence = "Programcreek is a very huge and useful website.";
        opennlp.tools.parser.Parse topParses[] = ParserTool.parseLine(sentence, parser, 1);
        for (opennlp.tools.parser.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.)))

  

posted @ 2021-07-19 13:50  尐鱼儿  阅读(198)  评论(0编辑  收藏  举报