基于朴素贝叶斯的垃圾邮件过滤器构建与优化
1. 邮件解析与文本处理
在处理邮件时,我们使用 BeautifulSoup 来计算邮件的内部文本,同时还需要检测邮件内容的类型。以下是实现邮件解析的代码:
import email
from BeautifulSoup import BeautifulSoup
class EmailObject:
def __init__(self, filepath, category = None):
self.filepath = filepath
self.category = category
self.mail = email.message_from_file(self.filepath)
def subject(self):
return self.mail.get('Subject')
def body(self):
content_type = part.get_content_type()
body = part.get_payload(decode=True)
if content_type == 'text/html':
return BeautifulSoup(body).text
elif content_type == 'text/plain':
return body
else:
return ''
超级会员免费看
订阅专栏 解锁全文
2867

被折叠的 条评论
为什么被折叠?



