1

我想制作一个程序,使用点系统对垃圾邮件中的邮件进行分类。

对于邮件中的一些单词,
我希望程序为我在程序中归类为“垃圾词”的每个单词给出不同的分数,我也为不同的单词分配不同的分数,这样每个单词都值得一些的点。

我的伪代码:

  1. 从文件中读取文本
  2. 寻找“垃圾词”
    • 对于出现的每个单词,给出该单词的价值。
  3. 如果每个垃圾词的总分是 10,则打印“SPAM”,然后是文件中的单词列表并分类为垃圾词及其分数。

示例(文本文件):

Hello!  
Do you have trouble sleeping? 
Do you need to rest?
Then dont hesitate call us for the absolute solution- without charge!

因此,当程序运行并分析上面的文本时,它应该如下所示:

SPAM 14p
trouble 6p
charge 3p 
solution 5p 

所以我打算这样写:

class junk(object):
    fil = open("filnamne.txt","r")
    junkwords = {"trouble":"6p","solution":"3p","virus":"4p"}
    words = junkwords

    if words in fil:
        print("SPAM")
    else:
        print("The file doesn't contain any junk")

所以我现在的问题是如何为文件中出现的列表中的每个单词打分?
以及如何对总分求和,以便if total_points are > 10程序应该print "SPAM",然后
是在文件中找到的“垃圾词”列表以及每个单词的总分..

4

3 回答 3

0

这是一个快速脚本,可能会让您接近那里:

MAXPOINTS = 10
JUNKWORDS={"trouble":6,"solution":5,"charge":3,"virus":7}
fil = open("filnamne.txt", "r")

foundwords = {}

points = 0

for word in fil.read().split():
   if word in JUNKWORDS:
       if word not in foundwords:
           foundwords[word] = 0
       points += JUNKWORDS[word]
       foundwords[word] += 1

if points > 10:
    print "SPAM"
    for word in foundwords:
        print word, foundwords[word]*JUNKWORDS[word]
else:
    print "The file doesn't contain any junk"

您可能希望.lower()在单词上使用并使所有字典键小写。也许还删除所有非字母数字字符。

于 2013-03-04T13:31:14.893 回答
0

这是另一种方法:

from collections import Counter

word_points = {'trouble': 6, 'solution': 5, 'charge': 3, 'virus': 7}

words = []

with open('ham.txt') as f:
   for line in f:
      if line.strip(): # weed out empty lines
         for word in line.split():
             words.append(word)

count_of_words = Counter(words)

total_points = {}
for word in word_points:
    if word in count_of_words:
       total_points[word] = word_points[word] * count_of_words[word]

if sum(i[0] for i in total_points.iteritems()) > 10:
   print 'SPAM {}'.format(sum(i[0] for i in total_points.iteritems()))
   for i in total_points.iteritems():
      print 'Word: {} Points: {}'.format(*i)

您可以进行一些优化,但它应该让您了解一般逻辑。Counter可从 Python 2.7 及更高版本获得。

于 2013-03-04T13:38:06.323 回答
0

我假设每个单词都有不同的点,所以我使用了字典
您需要查找单词中的单词在文件中出现的次数。
您应该将每个单词的点存储为整数。不作为'6p''4p'

所以,试试这个:

def find_junk(filename):
    word_points = {"trouble":6,"solution":3,"charge":2,"virus":4}
    word_count = {word:0 for word in word_points}
    count = 0
    found = []
    with open(filename) as f:
        for line in f:
            line = line.lower()
            for word in word_points:
                c = line.count(word)
                if c > 0:
                    count += c * word_points[word]
                    found.append(word)
                    word_count[word] += c
    if count >= 10:
        print '  SPAM'*4
        for word in found:
            print '%10s%3s%3s' % (word, word_points[word], word_count[word])
    else:
        print "Not spam"
find_junk('spam.txt')
于 2013-03-04T13:46:57.760 回答