AttributeError:'tuple'属性没有属性'endswith'Python NLTK Lemmatizer

时间:2016-03-29 13:21:53

标签: python nlp nltk

我正在为NLP项目创建一个预处理器,并且该引理器没有按预期工作。我希望代码可以使每个单词变形,但我看到错误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)

更新

我已经更新了代码以便编译,但现在实际上并没有将这些词语变为lematizing。这是更新的代码:

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)

1 个答案:

答案 0 :(得分:2)

<强> TL; DR

// Step 7
if (isset($_POST['submit'])) {
    $SongToAdd = stripslashes($_POST['SongName']) . "\n";
    $ExistingSongs = array();
    if (file_exists("SongOrganizer/songs.txt") && filesize("SongOrganizer/songs.txt") > 0) {
        $ExistingSongs = file("SongOrganizer/songs.txt");


        // Step 8 and Step 9    
        if (in_array($SongToAdd, $ExistingSongs)) {
            echo "<p>The song you entered already exists!<br />\n";
            echo "Your song was not added to the list.</p>";
        } else {
            $SongFile = fopen("SongOrganizer/songs.txt", "ab");
            if ($SongFile === false)
                echo "There was an error saving your message!\n";
            else {
                fwrite($SongFile, $SongToAdd);
                fclose($SongFile);
                echo "Your Song has been added to the list.\n";
            }
        }

    }
}   

评论中的解释:

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)