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)
中有一个错误,它一直在说-
";参数"word"未填充">。准确的错误是-
TypeError: lemmatize() missing 1 required positional argument: 'word'
在某些情况下,word
的可疑值可能是None
,因此您可以在if
子句中添加此情况:
words = [lemmatizer.lemmatize(word) for word in words if word and word not in ignore_letters]
希望有帮助:(
我尝试了下面的代码,在您的代码中查看上面的行是正确的。但由于有些方法的定义(WordNetLemmatizer.lemmatize
、nltk.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
将会出现与您遇到的错误消息相同的错误消息。
同样的事情也发生在我身上。你只需要在"WordNetLemmatizer"后面添加括号"(("就可以正确地实例化它。
应为:
lemmatizer = WordNetLemmatizer()