当前位置:   article > 正文

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

pyltp安装

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

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文件尝试样例:

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

会出现报错:

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

这是因为segmentor的语句变了

对于这两行:

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

要改成:

segmentor=Segmentor(cws_model_path)     

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

  1. segmentor = Segmentor(cws_model_path)
  2. postagger = Postagger(pos_model_path)
  3. 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')

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/637602
推荐阅读
  

闽ICP备14008679号