Blog of CodeJELLOW’S ENGINEERING NOTES
← 返回文章

TREC(trec06p)数据集处理

2006 TREC Public Spam Corpora (trec06p) https://plg.uwaterloo.ca/~gvcormac/treccorpus06/ TREC的数据集是按照文件提供的,每个邮件在一个文件中,通过一个index索引标记…

本文目录 ⌄

    2006 TREC Public Spam Corpora (trec06p)

    https://plg.uwaterloo.ca/~gvcormac/treccorpus06/

    TREC的数据集是按照文件提供的,每个邮件在一个文件中,通过一个index索引标记spam和ham。下面这段代码提取出邮件正文,并将所有邮件的正文和标记输出到一个文件中,便于下一步处理转化词袋模型。

    TEXT
    from email.parser import Parser
    
    filetype="utf-8"
    # 解析邮件内容
    def get_body(msg):
        if msg.is_multipart():
            return get_body(msg.get_payload(0))
        else:
            return msg.get_payload(None, decode=True)
    
    
    if __name__ == '__main__':
        f = open("E:/毕设/毕设数据/trec06p/full/index.txt", encoding=filetype)
        f1 = open("data.txt", "a+", encoding=filetype, errors='ignore')
        line = f.readline()
        num = 0
        while line:
            line = line.rstrip()
            temp = line.split(' ', 1)  # 以空格为分隔符,分隔成两个
            print(num / 37822 * 100)
            num += 1
            print("目前文件数:", num)
            path = "E:\\毕设\\毕设数据\\trec06p" + temp[1].lstrip('..').replace('/', "\\")
            with open(path, encoding=filetype, errors='ignore') as f2:
                text = f2.read()
            f2.close()
            email = Parser().parsestr(text)
            text=get_body(email).decode(filetype, errors='ignore').replace('\n', '').replace('\r', '').replace('\t', '').strip()
            text=temp[0]+'\t'+text+'\n'
            f1.write(text)
            print(text)
            line = f.readline()
        f.close()
        f1.close()
        print("处理完成")
    
    通过 RSS 订阅更新 →