zhangkai-ai / build-kg-from-scratch

181 stars 49 forks source link

ch7: TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. pyltp.Segmentor(model_path: str, lexicon_path: str = None, force_lexicon_path: str = None) #7

Open Taojtyy opened 2 years ago

Taojtyy commented 2 years ago

您好,在运行ch7的main_handler时,遇到了以下问题: Traceback (most recent call last): File "D:/code/build-kg-from-scratch-main/build-kg-from-scratch-main/ch7/main_handler.py", line 8, in from nlu.nlu_handler import nlu_main_handler File "D:\code\build-kg-from-scratch-main\build-kg-from-scratch-main\ch7\nlu\nlu_handler.py", line 6, in from nlu.seg.seg_util import jieba_cut File "D:\code\build-kg-from-scratch-main\build-kg-from-scratch-main\ch7\nlu\seg\seg_util.py", line 6, in from nlu.seg.ltp_util import ltp_seg_handler File "D:\code\build-kg-from-scratch-main\build-kg-from-scratch-main\ch7\nlu\seg\ltp_util.py", line 176, in ltp_seg_handler = LTPHander(LTP_PATH, task_type='seg') File "D:\code\build-kg-from-scratch-main\build-kg-from-scratch-main\ch7\nlu\seg\ltp_util.py", line 42, in init self._segmentor = Segmentor() TypeError: init(): incompatible constructor arguments. The following argument types are supported:

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

Invoked with: Exception ignored in: <function LTPHander.del at 0x000002C0AD6CE3A8> Traceback (most recent call last): File "D:\code\build-kg-from-scratch-main\build-kg-from-scratch-main\ch7\nlu\seg\ltp_util.py", line 63, in del self._segmentor.release() AttributeError: 'LTPHander' object has no attribute '_segmentor'

LTP模型已下载并更改了路径

Ulov888 commented 2 years ago

遇到相同问题,能否帮忙解答下@zhangkai-ai

winnie0 commented 2 years ago

你好,请问你pyltp的版本是多少?

Taojtyy commented 2 years ago

0.4.0, 有尝试改其它更低版本的,但是都无法安装成功。python==3.6.8

winnie0 commented 2 years ago

我查看了一下我的pytlp版本是0.2.1, 具体解决方案: (1)可以先临时安装旧版的适用当前项目里的代码,pip install pyltp==0.2.1,21年的时候主流是这个版本,具体参考文档如下: https://pyltp.readthedocs.io/zh_CN/latest/api.html#id6 (2)pyltp最新版本是0.4.0,我测试了一下新版修改了很多接口的代码,函数传参调用均有变化,后续我更新一套适配新版的代码到项目里。

0.4.0, 有尝试改其它更低版本的,但是都无法安装成功。python==3.6.8

winnie0 commented 2 years ago

针对pyltp==0.4.0,可以使用ltp_util_0_4_0.py适配新版本。

tingshuo521 commented 1 year ago

下面的函数可能存在相同的问题,你没有改 ` def words_mark(array):

# 词性标注模型路径,模型名称为`pos.model`
pos_model_path = os.path.join(LTP_DATA_DIR, 'pos.model')
postagger = pyltp.Postagger(pos_model_path)  # 初始化实例
# postagger.load(pos_model_path)  # 加载模型
postags = postagger.postag(array)  # 词性标注
pos_str=' '.join(postags)
pos_array=pos_str.split(" ")
postagger.release()  # 释放模型
return pos_array `