Python nltk使用大型功能集进行分类(Replicate Go Et Al 2009)

时间:2016-04-20 14:57:43

标签: python twitter nltk sentiment-analysis

我正在尝试复制Go Et Al。 Twitter情绪分析可在此处找到http://help.sentiment140.com/for-students 我遇到的问题是功能的数量是364464.我目前正在使用nltk和nltk.NaiveBayesClassifier来执行此操作,其中推文包含1,600,000条推文的复制并且存在极性:

for tweet in tweets:
    tweet[0] = extract_features(tweet[0], features)

classifier = nltk.NaiveBayesClassifier.train(training_set)
# print "NB Classified"
classifier.show_most_informative_features()
print(nltk.classify.util.accuracy(classifier, testdata))

除了extract_features函数

之外,所有事情都不需要很长时间
def extract_features(tweet, featureList):
  tweet_words = set(tweet)
  features = {}
  for word in featureList:
      features['contains(%s)' % word] = (word in tweet_words)
  return features

这是因为对于每条推文,它都会创建一个大小为364,464的字典来表示是否存在某些内容。

有没有办法让这个更快或更有效率而不减少本文中的功能数量?

1 个答案:

答案 0 :(得分:0)

原来有一个很棒的功能叫做: nltk.classify.util.apply_features() 你可以在这里找到http://www.nltk.org/api/nltk.classify.html

    training_set = nltk.classify.apply_features(extract_features, tweets)

我不得不改变我的extract_features功能,但它现在适用于没有内存问题的巨大尺寸。

这是功能描述的缩写:

  

此函数的主要目的是避免为语料库中的每个标记存储所有要素集所涉及的内存开销。相反,这些功能集是根据需要延迟构建的。当基础的令牌列表本身很懒惰时(如许多语料库读者的情况),内存开销的减少尤其重要。

和我改变的功能:

    def extract_features(tweet):
         tweet_words = set(tweet)
         global featureList
         features = {}
         for word in featureList:
            features[word] = False
         for word in tweet_words:
            if word in featureList:
                features[word] = True
         return features
相关问题