StanfordCoreNLP中的注意事项


本文使用的是StanfordCoreNLP 4.4.0,可以从下面网站中download

https://stanfordnlp.github.io/CoreNLP/history.html

1. annotate中的wordoriginalText并不一致

originalText才是我们需要的token

import json
from stanfordcorenlp import StanfordCoreNLP
CORENLP_HOME = r"D:\Software\StanfordCoreNLP\stanford-corenlp-4.4.0"
client = StanfordCoreNLP(CORENLP_HOME, lang="en", memory='8g')
sentence = "Minicomputers, supercomputers, disk drives, CRT, CRU, etc., etc."
p = json.loads(client.annotate(sentence, properties={"annotators":"pos","outputFormat": "json"}))
original_text = [tok['originalText'] for tok in p['sentences'][0]['tokens']]
word = [tok['word'] for tok in p['sentences'][0]['tokens']]
p['sentences'][0]['tokens']
print("originalText:",  original_text)
print("word:", word)
print("word_tokenize: ", client.word_tokenize(sentence))

在这里插入图片描述
在这里插入图片描述

2. annotate使用parse一定要添加pos,否则parse tree是错误的

note:stanfordcorenlp使用annotate时,如果需要parse tree,则一定要添加pos,否则得到的parse tree是错误的。同时,也可以使用client.parse(text)

from stanfordcorenlp import StanfordCoreNLP
from nltk.tree import ParentedTree
import json

text = "Fishes swim in the sea and butterflies fly in the sky."
parser1 = json.loads(client.annotate(text, properties={"annotators":"pos,parse","outputFormat": "json"}))
tree1 = ParentedTree.fromstring(ParentedTree.convert(parser1 ["sentences"][0]["parse"]))
tree1 

parser2 = json.loads(client.annotate(text, properties={"annotators":"parse","outputFormat": "json"}))
tree2 = ParentedTree.fromstring(ParentedTree.convert(parser2["sentences"][0]["parse"]))
tree2

p_tree3 = client.parse(text)
tree3 = ParentedTree.fromstring(ParentedTree.convert(p_tree3))
tree3

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值