1

我目前正在使用 Naive Bayles 制作一个简单的垃圾邮件/火腿电子邮件过滤器。

为了让您了解我的算法逻辑:我有一个包含很多 os 文件的文件夹,这些文件是垃圾邮件/火腿电子邮件的示例。我在这个文件夹中还有另外两个文件,其中包含我所有火腿示例的标题,另一个包含我所有垃圾邮件示例的标题。我是这样组织的,所以我可以正确打开和阅读这些电子邮件。

我将我认为重要的所有单词都放在字典结构中,并带有标签“垃圾邮件”或“火腿”,具体取决于我从哪种文件中提取它们。

然后我使用 nltk.NaiveBayesClassifier.train() 所以我可以训练我的分类器,但我得到了错误:

对于功能集,labeled_featuresets 中的标签:ValueError:要解包的值太多

我不知道为什么会这样。当我寻找解决方案时,我发现字符串不是可散列的,并且我正在使用列表来执行此操作,然后我将其变成了字典,据我所知,它是可散列的,但它一直出现此错误。有人知道如何解决吗?谢谢!

我的所有代码都列在下面:

import nltk
import re 
import random

stopwords   = nltk.corpus.stopwords.words('english')    #Words I should avoid since they have weak value for classification
my_file     = open("spam_files.txt", "r")   #my_file now has the name of each file that contains a spam email example
word        = {}    #a dictionary where I will storage all the words and which value they have (spam or ham)

for lines in my_file:   #for each name of file (which will be represenetd by LINES) of my_file
with open(lines.rsplit('\n')[0]) as email: #I will open the file pointed by LINES, and then, read the email example that is inside this file
    for phrase in email:    #After that, I will take every phrase of this email example I just opened
        try:    #and I'll try to tokenize it
            tokens = nltk.word_tokenize(phrase)
        except: 
            continue    #I will ignore non-ascii elements
        for c in tokens:    #for each token
            regex = re.compile('[^a-zA-Z]') #I will also exclude numbers
            c = regex.sub('', c)
            if (c): #If there is any element left
                if (c not in stopwords): #And if this element is a not a stopword
                    c.lower()
                    word.update({c: 'spam'})#I put this element in my dictionary. Since I'm analysing spam examples, variable C is labeled "spam".

my_file.close() 
email.close()

#The same logic is used for the Ham emails. Since my ham emails contain only ascii elements, I dont test it with TRY
my_file = open("ham_files.txt", "r")
for lines in my_file:
with open(lines.rsplit('\n')[0]) as email:
    for phrase in email:
        tokens = nltk.word_tokenize(phrase)
        for c in tokens:
            regex = re.compile('[^a-zA-Z]')
            c = regex.sub('', c)
            if (c):
                if (c not in stopwords):
                    c.lower()
                    word.update({c: 'ham'})

my_file.close() 
email.close()

#And here I train my classifier
classifier = nltk.NaiveBayesClassifier.train(word)
classifier.show_most_informative_features(5)
4

1 回答 1

1

nltk.NaiveBayesClassifier.train()期望“<em>元组列表(featureset, label)”(参见train()方法的文档)没有提到的是featureset应该是映射到特征值的特征名称的字典。

因此,在具有词袋模型的典型垃圾邮件/火腿分类中,标签是“垃圾邮件”/“火腿”或 1/0 或真/假;特征名称是出现的单词,值是每个单词出现的次数。例如,该train()方法的参数可能如下所示:

[({'greetings': 1, 'loan': 2, 'offer': 1}, 'spam'),
 ({'money': 3}, 'spam'),
 ...
 ({'dear': 1, 'meeting': 2}, 'ham'),
 ...
]

如果您的数据集相当小,您可能希望将实际字数替换为 1,以减少数据稀疏性。

于 2015-05-23T22:10:34.890 回答