pyltp
记录使用哈工大Ltp工具进行自然语言处理中遇见的问题,后续会继续往下写。
Ltp model加载路径
pyltp中segmentor,Postagger,Parser,NamedEntityRecognizer加载模型时,模型的路径要以‘/’分割,不能以‘\’分割,Linux文件路径是以第一种方式,而Windows是第二方式,因此在Windows下使用Ltp模型需要对路径进行处理,否则在命令行中使用会报“*: Model not loaded!”,例如:
real_path = os.path.realpath(__file__).replace('\\', '/')
real_path = real_path[0: real_path.rindex('/')] + '/ltp_dat'
# D:\PycharmProjects\GovDataTools\GovTools\GovNLP\ltp_data\cws.model
print("ltp_data磁盘路径:%s" % real_path)
# LTP_DIR = "./ltp_data"
self.segmentor = Segmentor()
self.segmentor.load(real_path + "/cws.model")
Ltp 语义角色pisrl模型的加载
Pyltp在Windows环境下使用要加载pisrl_win.model,否则加载pisrl.model,具体的原因和解释看Ltp官网。
self.labeller = SementicRoleLabeller()
sys = platform.system()
if sys == "Windows":
print("加载pisrl_win模型")
self.labeller.load(real_path + "/pisrl_win.model")
else:
print("加载pisrl模型")
self.labeller.load(real_path + "/pisrl.model")