MacOS M1关于ltp(python)的使用

官方给的代码部分行不通了,不知道是由于系统还是用法更新,下面是我的可行代码

import os
LTP_DIR = "./ltp_data_v3.4.0"
cws_path = os.path.join(LTP_DIR, "cws.model")
pos_path = os.path.join(LTP_DIR, "pos.model")
ner_path = os.path.join(LTP_DIR, "ner.model")
par_path = os.path.join(LTP_DIR, "parser.model")
srl_path = os.path.join(LTP_DIR, 'pisrl.model')

from pyltp import Segmentor, Postagger, NamedEntityRecognizer, Parser, SementicRoleLabeller
seg = Segmentor(cws_path)#分词
pos = Postagger(pos_path)#词性标注
rec = NamedEntityRecognizer(ner_path)#命名实体识别
parser = Parser(par_path)#依句法分析,与示例代码不同
labeller =SementicRoleLabeller(srl_path)#语义角色标注, 与示例代码不同

words = seg.segment("鲍二蕾,你可长点心吧!")
postags = pos.postag(words)
nertags = rec.recognize(words, postags)
arcs = parser.parse(words, postags)
roles = labeller.label(words, postags, arcs)

print(words)
print(postags)
print(nertags)
print(arcs)
print(roles)

seg.release()
pos.release()
rec.release()
parser.release()
labeller.release()

运行结果是

[‘鲍二蕾’, ‘,’, ‘你’, ‘可’, ‘长’, ‘点心’, ‘吧’, ‘!’]
[‘nh’, ‘wp’, ‘r’, ‘v’, ‘a’, ‘n’, ‘u’, ‘wp’]
[‘S-Nh’, ‘O’, ‘O’, ‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[(0, ‘HED’), (1, ‘WP’), (6, ‘ATT’), (6, ‘ADV’), (6, ‘ATT’), (1, ‘COO’), (6, ‘RAD’), (1, ‘WP’)]
[(4, [(‘A0’, (2, 2))])]

且都是list类型

输出结果的含义参考https://pyltp.readthedocs.io/zh_CN/latest/api.html

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页