代码
import os
from pprint import pprint
from pyltp import Segmentor, Postagger, Parser, NamedEntityRecognizer, SementicRoleLabeller
class LtpParser:
def __init__(self):
LTP_DIR = "../model/ltp_data_v3.4.0/"
self.segmentor = Segmentor()
# load_with_lexicon用于加载自定义的词典
self.segmentor.load_with_lexicon(os.path.join(LTP_DIR, "cws.model"),os.path.join(LTP_DIR, "user_dict.txt"))
self.postagger = Postagger()
self.postagger.load_with_lexicon(os.path.join(LTP_DIR, "pos.model"),os.path.join(LTP_DIR, "user_dict.txt"))
self.parser = Parser()
self.parser.load(os.path.join(LTP_DIR, "parser.model"))
self.recognizer = NamedEntityRecognizer()
self.recognizer.load(os.path.join(LTP_DIR, "ner.model"))
self.labeller = SementicRoleLabeller()
self.labeller.load(os.path.join(LTP_DIR, 'pisrl.model'))
def analyse(self, text):
# 分词
segmentor_res = self.segmentor.segment(text)
print(list(segmentor_res))
# 词性标注,传入的是分词的结果
postagger_res = self.postagger.postag(segmentor_res)
print(list(postagger_res))
# 命名实体识别,传入的是分词、词性标注的结果
# 依存句法分析,传入的是分词、词性标注的结果
arcs = self.parser.parse(segmentor_res, postagger_res)
# print("\t".join("%d:%s" % (arc.head, arc.relation) for arc in arcs))
arcs_res = []
for word, arc in zip(list(segmentor_res), arcs):
tmp = {}
if arc.head == 0:
tmp['dep'] = word
tmp['gov'] = 'ROOT'
tmp['pos'] = arc.relation
else:
tmp['dep'] = word
tmp['gov'] = segmentor_res[arc.head-1]
tmp['pos'] = arc.relation
arcs_res.append(tmp)
pprint(arcs_res)
# 语义角色标注,传入的是分词、词性标注、句法分析结果
labeller_res = self.labeller.label(segmentor_res, postagger_res, arcs)
for role in labeller_res:
print (role.index, "\t".join(["%s:(%d,%d)-(%s)" % (arg.name, arg.range.start, arg.range.end, "".join(list(segmentor_res)[arg.range.start:arg.range.end+1])) for arg in role.arguments]))
if __name__ == '__main__':
ltpParser = LtpParser()
text = "中国是一个自由、和平的国家"
ltpParser.analyse(text)
结果
['中国', '是', '一个', '自由', '、', '和平', '的', '国家']
['ns', 'v', 'm', 'a', 'wp', 'a', 'u', 'n']
['S-Ns', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
[{'dep': '中国', 'gov': '是', 'pos': 'SBV'},
{'dep': '是', 'gov': 'ROOT', 'pos': 'HED'},
{'dep': '一个', 'gov': '国家', 'pos': 'ATT'},
{'dep': '自由', 'gov': '国家', 'pos': 'ATT'},
{'dep': '、', 'gov': '和平', 'pos': 'WP'},
{'dep': '和平', 'gov': '自由', 'pos': 'COO'},
{'dep': '的', 'gov': '自由', 'pos': 'RAD'},
{'dep': '国家', 'gov': '是', 'pos': 'VOB'}]
1 A0:(0,0)-(中国) A1:(2,7)-(一个自由、和平的国家)