pyltp安装(pyltp4.0.0)内附各种版本语法bug解决方式(绝对详细)【包含语法改动说明】

本文讲述了在安装PyLTP时遇到的错误,如版本不匹配和`isnotasupportedwheelonthisplatform`问题。作者提供了修改`Segmentor`和加载模型的方法,以及处理`tuple`对象属性错误和lexicon加载方式的更新。
摘要由CSDN通过智能技术生成

可以先参考这位博主的保姆级教程,写得非常好。

pyltp安装教程——保姆级_pyltp wheel 文件-CSDN博客

但是pyltp以及python版本不对版的问题使安装pyltp仍然报了很多bug。

基于上一篇的教程,如果遇到

ERROR: pyltp-0.2.1-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.

可以先参考

【2023新教程】解决is not a supported wheel on this platform-解决pip has no attribute pep425tags-解决网上旧教程不适用的问题_is not a supported wheel on this platform.-CSDN博客

这篇教程主要的意思是:找到最相近的名字,改写whl。

我尝试后,得到的结果是成功安装了,甚至进入anaconda的环境目录搜都能搜到pyltp,但就是import报错,会划红线。

我的解决方法是:

删除在lib的包下所有的pyltp文件,然后直接在python里

pip install pyltp

成功后,不需要把ltp的v3.4.0换掉,继续用它。

创建一个py文件尝试样例:

import os
from pyltp import Segmentor
LTP_DIR='D:\Program Files\myLTP\ltp_data_v3.4.0'  # ltp模型目录的路径
cws_model_path=os.path.join(LTP_DIR,'cws.model') # 分词模型路径,模型名称为`cws.model`
segmentor=Segmentor()            # 初始化实例
segmentor.load(cws_model_path)   # 加载模型
words=segmentor.segment('熊高雄你吃饭了吗')   # 分词
print('\t'.join(words))
segmentor.release()       # 释放模型

会出现报错:

1. pyltp.Segmentor(model_path: str, lexicon_path: str = None, force_lexicon_path: str = None)

这是因为segmentor的语句变了

对于这两行:

segmentor=Segmentor()            # 初始化实例
segmentor.load(cws_model_path)   # 加载模型

要改成:

segmentor=Segmentor(cws_model_path)     

另外,调用Postagger和parser也是一样(但这两个错了是不会报错的,但会运行不出,所以请一定注意)

segmentor = Segmentor(cws_model_path)
postagger = Postagger(pos_model_path)
parser = Parser(par_model_path)

另外,可能还有朋友会遇到:

'tuple' object has no attribute 'head'

的报错

请把

(arc.head, arc.relation) for arc in arcs)

改成

(arc[0], arc[1]) for arc in arcs)

以及:

在使用lexcion时,segmentor.load_with_lexicon也一同被弃用了。需要在定义的时候直接加入file path

segmentor = Segmentor(cws_model_path, 'lexicon.txt')

注意:必须是英文名,否则会报错找不到相应的文件。

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值