TypeError: lemmatize() missing 1 required positional argument: 'word

TypeError: lemmatize() missing 1 required positional argument: 'word

我有一个 csv 文件中每一行的数组,如下所示:

[['thxx'], ['too', 'late', 'now', 'dumbass'], ['you', '‘', 're', 'so', 'dumb', '?', '?'], ['thxxx'], ['i', '‘', 'd', 'be', 'fucked']]

当我尝试像这样将其传递给词形还原器时:

from nltk.stem import WordNetLemmatizer
lemmatized_words = [WordNetLemmatizer.lemmatize(word) for word in tokened_text]
print(lemmatized_words)

我收到以下错误:

TypeError: lemmatize() missing 1 required positional argument: 'word'

这是为什么?

作为附带问题:我需要在将其传递给矢量化之前执行此操作吗?我正在构建一个机器学习模型,并在 sci kit learn 中看到了 CountVectorizer 函数,但找不到任何关于它预先进行词形还原等的信息。

您的代码中有一些错误:

  • WordNetLemmatizer是一个class,你需要先实例化它
  • tokened_text 是嵌套列表,因此您需要嵌套列表理解来保留结构。另外 lemmatize 需要一个字符串。

以下是您可以执行此操作的方法:

from nltk.stem import WordNetLemmatizer

wnl = WordNetLemmatizer()

lemmatized_words = [[wnl.lemmatize(word) for word in l] for l in tokened_text]
Traceback (most recent call last):
  File "C:/Users/tarun/PycharmProjects/Yumnam_jr_ChatBot/training.py", line 31, in <module>
    words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letter]
  File "C:/Users/tarun/PycharmProjects/Yumnam_jr_ChatBot/training.py", line 31, in <listcomp>
    words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letter]
TypeError: lemmatize() missing 1 required positional argument: 'word'

错误是“参数 'self' 未填充”。所以,你需要做的是安装“package 'self' Version 2020.12.3”并像下面这样写:-

lemmatizer = WordNetLemmatizer

words = [lemmatizer.lemmatize(self, word) for word in words if word not in ignore_letter]
words = sorted(set(words))

对我来说效果很好。

出现错误的原因是你在将函数名赋值给变量时在函数名后面缺少 ()

xyz = WordNetLemmatizer() --> this bracket is missing leading to the error

您只需编写此代码即可使您的代码正常工作

your_variable_here = WordNetLemmatizer()

在您的代码中,您没有在 WordNetLemmatizer 之后添加括号 ()
添加那个,你就可以开始了。