AttributeError:“tuple”属性没有属性“endswith”Python NLTK Lemmatiz

2024-04-19 18:53:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个NLP项目创建一个预处理器,而lemmatizer没有按预期工作。我希望代码对每个单词进行词法化,但是我看到了错误AttributeError: 'tuple' object has no attribute 'endswith'。对不起,如果这是个愚蠢的错误,但我做错了什么?我用的是Python。这是我的代码:

from pymongo import MongoClient
from nltk import *
import nltk
lemma = WordNetLemmatizer()
client = MongoClient()
db = client.qa
main = db.main

while True:
    question = input('Ask a question: ').upper()
    question = re.sub('[^0-9A-Z\s]', '', question)
    question = word_tokenize(question)
    question = nltk.pos_tag(question)
    for each in question:
        lemma.lemmatize(each)
    print(question)

更新:

我已经更新了代码以便它可以编译,但它现在并没有实际地对单词进行柠檬化。以下是更新后的代码:

^{pr2}$

Tags: 项目代码fromimportclientdbnlpmain
1条回答
网友
1楼 · 发布于 2024-04-19 18:53:10

TL;DR

from pymongo import MongoClient
from nltk import word_tokenize, pos_tag, WordNetLemmatizer

wnl = WordNetLemmatizer()
client = MongoClient()
db = client.qa
main = db.main

while True:
    question = input('Ask a question: ').upper()
    question = re.sub('[^0-9A-Z\s]', '', question)
    question = word_tokenize(question)
    question = nltk.pos_tag(question)
    for each in question:
        wnl.lemmatize(each[0])
    print(question)

注释说明:

^{pr2}$

相关问题 更多 >