NLTK分类器-NLTK朴素贝叶斯分类中无法调用“ dict”对象

时间:2018-07-04 17:25:17

标签: python nltk text-classification

我正在尝试编写一个小的分类器,以对男名和女名进行分类,并不断出现“ dict object not callable”错误。我已经用my_dict()替换了dict(),看是否是由于保留字引起的,但是错误变成“ my_dict()未定义”。请帮忙。

我的代码是:

import nltk, random


class GenderApp(object):
    def __init__(self):
        names_sample = nltk.corpus.names
        self.names = [(name.lower(), 'male') for name in names_sample.words('male.txt')] + [(name.lower(), 'female') for name in names_sample.words('female.txt')]
        random.shuffle(self.names)
        self.feature_sets = [(GenderApp.gender_features_part2(name), gender) for name, gender in self.names]
        self.train_set = self.feature_sets[:4000]
        self.test_set = self.feature_sets[4000:]
        self.classifier = nltk.NaiveBayesClassifier.train(self.train_set)

    @staticmethod
    def gender_features_part2(word):
        name = word.lower()  # let's normalise our input
        features = dict()
        features['first_letter'] = name[0]
        features['last_letter'] = name[1]
        for letter in 'abcdefghijklmnopqrstuvwxyz':
            features['count' + letter] = name.count(letter)
            features['has' + letter] = letter in name
        return features

    def check_gender(self, name):
        name = name.lower()
        print('Gender for ' + name + ' : ' + self.classifier.classify(GenderApp.gender_features_part2(name)))

    def check_accuracy_of_the_classifier(self):
        print('Accuracy of classifier is : ', nltk.classify.accuracy(self.classifier, self.test_set) * 100)

    def see_learned_information_of_classifier(self, n=10):
        self.classifier.show_most_informative_features(n)

if __name__ == '__main__':
    app = GenderApp()
    app.check_gender('Neo')
    app.see_learned_information_of_classifier(20)
    app.check_accuracy_of_the_classifier()

错误是:

<ipython-input-143-050ff81df34d> in <listcomp>(.0)
      8 for name in names_sample.words('female.txt')]
      9 random.shuffle(self.names)
---> 10         self.feature_sets = [(GenderApp.gender_features_part2(name), gender) for name, gender in self.names]
     11         self.train_set = self.feature_sets[:4000]
     12         self.test_set = self.feature_sets[4000:]

<ipython-input-143-050ff81df34d> in gender_features_part2(word)
     16     def gender_features_part2(word):
     17         name = word.lower()  # let's normalise our input
---> 18         features = dict()
     19         features['first_letter'] = name[0]
     20         features['last_letter'] = name[1]

TypeError: 'dict' object is not callable

0 个答案:

没有答案