使用opennlp进行依存句法分析

本文主要研究下如何使用opennlp进行依存句法分析

Parser

opennlp主要使用Parser来进行依存句法分析,其模型为ParserModel

    @Test
    public void testParserTool() throws IOException {
        try (InputStream modelInputStream = this.getClass().getClassLoader().getResourceAsStream("chunker/en-parser-chunking.bin")) {
            ParserModel model = new ParserModel(modelInputStream);
            Parser parser = ParserFactory.create(model);
            String sentence = "The cow jumped over the moon";
            // Used to demonstrate difference between NER and Parser
            // sentence = "He was the last person to see Fred.";

            Parse parses[] = ParserTool.parseLine(sentence, parser, 3);
            for (Parse parse : parses) {
                parse.show();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
这里使用en-parser-chunking.bin这个训练好的模型来进行分析

第一句输出如下

(TOP (PP (S (NP (DT The) (NN cow)) (PP (VP (VBD jumped) (PRT (RP over))))) (NP (DT the) (NN moon))))
(TOP (NP (NP (DT The) (NN cow)) (PP (S (VP (VBN jumped) (PP (IN over) (NP (DT the) (NN moon))))))))
(TOP (NP (NP (DT The) (NN cow)) (SBAR (S (VP (VBN jumped) (PP (IN over) (NP (DT the) (NN moon))))))))

第二句输出如下

(TOP (FRAG (FRAG (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (SBAR (S (VP (TO to) (VP (VB see))))))))) (: Fred.)))
(TOP (S (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (PP (VP (TO to) (VP (VB see))))))) (: Fred.)))
(TOP (S (FRAG (S (NP (PRP He)) (VP (VBD was) (NP (NP (DT the) (JJ last) (NN person)) (SBAR (S (VP (TO to) (VP (VB see))))))))) (: Fred.)))

小结

opennlp也支持依存句法分析,不过根节点的表示,stanford nlp使用的是ROOT,而opennlp使用的是TOP。

doc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 下载Stanford CoreNLP 首先,需要下载Stanford CoreNLP。官网地址为:https://stanfordnlp.github.io/CoreNLP/ 。在下载页面选择适合自己系统的版本,下载后解压。 2. 启动Stanford CoreNLP 进入解压后的Stanford CoreNLP目录,执行以下命令启动Stanford CoreNLP服务: ```bash java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000 ``` 这个命令会启动一个Stanford CoreNLP服务,监听9000端口,并且会在控制台输出一些信息。 3. 使用Stanford CoreNLP进行依存句法分析 Python中可以使用stanfordcorenlp库来连接Stanford CoreNLP服务,进行依存句法分析。 安装stanfordcorenlp库: ```bash pip install stanfordcorenlp ``` 使用代码: ```python import stanfordcorenlp # 连接Stanford CoreNLP服务 nlp = stanfordcorenlp.StanfordCoreNLP('http://localhost:9000') # 输入文本 text = 'I love natural language processing.' # 进行依存句法分析 result = nlp.dependency_parse(text) # 输出结果 print(result) # 关闭连接 nlp.close() ``` 输出结果: ```python [(2, 1, 'nsubj'), (2, 4, 'dobj'), (4, 3, 'amod'), (0, 2, 'root')] ``` 可以看到,结果是一个列表,每个元素代表一个依存关系,其中每个元素又是一个元组,包含三个元素:依存关系的头部、尾部和关系类型。 其他语言的使用方式与Python类似,只需要将连接Stanford CoreNLP服务和调用依存句法分析的代码翻译成相应语言的语法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值