2016-03-29 49 views
1

NLPプロジェクトのプリプロセッサを作成していて、lemmatizerが期待どおりに動作していません。私はコードがすべての単語を字形にすることを期待していましたが、エラーAttributeError: 'tuple' object has no attribute 'endswith'が表示されています。申し訳ありませんが、それは愚かな間違いですが、何が間違っているのですか?私はPythonを使用しています。ここに私のコードです:AttributeError: 'tuple'属性に 'endswith'属性がありません。Python NLTK Lemmatizer

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) 

更新:

それがコンパイルされるように、私は、コードを更新しましたが、それは実際に今の言葉をlemmatizingされていません。ここで更新されたコードは次のとおりです。

from pymongo import MongoClient 
from nltk import * 
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) 
    for each in question: 
     lemma.lemmatize(each[0]) 
    print(question) 
+0

は、エラーの完全なトレースバックを投稿することができますか?私はあなたが 'lemma.lemmatize(each [0])'をしているべきだと思う。 – alvas

答えて

2

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) 

コメントで説明:

>>> from nltk import word_tokenize, pos_tag, WordNetLemmatizer 
>>> wnl = WordNetLemmatizer() 
>>> sent = "this is a two parts sentence, with some weird lemmas" 
>>> word_tokenize(sent) # Return a list of string 
['this', 'is', 'a', 'two', 'parts', 'sentence', ',', 'with', 'some', 'weird', 'lemmas'] 
>>> pos_tag(word_tokenize(sent)) # Returns a list of tuple with (word, pos) 
[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('two', 'CD'), ('parts', 'NNS'), ('sentence', 'NN'), (',', ','), ('with', 'IN'), ('some', 'DT'), ('weird', 'JJ'), ('lemmas', 'NN')] 
>>> pos_tag(word_tokenize(sent))[0] 
('this', 'DT') 
>>> pos_tag(word_tokenize(sent))[0][0] 
'this' 
>>> each = pos_tag(word_tokenize(sent))[0][0] 
>>> each 
'this' 
>>> wnl.lemmatize(each) 
'this' 
+0

(インポートを少し変更した後で)実行されますが、実際に単語をリーマーム化しません。 –

+0

インポートは重要な問題ではなく、 'wnl.lemmatize(each [0])' – alvas

+0

行ごとに説明コードを調べると、問題の理解に役立ちます。 – alvas

関連する問題