【问题标题】:I keep getting this error : TypeError: lemmatize() missing 1 required positional argument: 'word'我不断收到此错误:TypeError: lemmatize() missing 1 required positional argument: 'word'
【发布时间】:2021-01-13 12:54:13
【问题描述】:
lemmatizer = WordNetLemmatizer

intents = json.loads(open('intents.json').read())

words = []
classes = []
documents = []
ignore_letters = ['?', '!', '.', ',']

for intent in intents['intents']:
    for pattern in intent['patterns']:
        word_list = nltk.word_tokenize(pattern)
        words.extend(word_list)
        documents.append((word_list, intent['tag']))
        if intent['tag'] not in classes:
            classes.append(intent['tag'])

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

lemmatizer.lemmatize(word) 中有一个错误,它一直在说-

“参数‘字’未填写”。 确切的错误是-

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

【问题讨论】:

  • 请准确粘贴错误信息。我不知道有任何异常说unfilled
  • 这是一直显示的错误:TypeError: lemmatize() missing 1 required positional argument: 'word'

标签: python python-3.x tensorflow


【解决方案1】:

word 的可疑值在某些情况下可能是 None,因此您可以在 if 子句中添加这种情况:

words = [lemmatizer.lemmatize(word) for word in words if word and word not in ignore_letters]

希望对你有帮助:)


我尝试了以下代码,看起来您的代码中的上述行是正确的。但由于有些方法的定义(WordNetLemmatizer.lemmatizenltk.word_tokenize)是未知,所以也许你需要检查那些定义

>>> ignore_letters = ['?', '!', '.', ',']
>>> words = ['asd']
>>> def f(x):
      pass
>>> words = [f(word) for word in words if word not in ignore_letters]
>>> words
[None]

如果我将f 更改为

>>> def f(x, word):
      pass

会有你遇到的同样的错误信息。

【讨论】:

  • 我在回答中添加了更多内容,请查看。
【解决方案2】:

同样的事情发生在我身上。您只需要在 'WordNetLemmatizer' 之后添加括号 '()' 即可正确实例化它。

应阅读:

lemmatizer = WordNetLemmatizer()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2022-01-06
    • 2016-05-09
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多