利用LDA模型对邮件内的内容做主题分类
# -*-coding: UTF-8 -*-
# @Time:2019/9/614:59
# @author superxjz
# @func 邮件分类
#导入需要的一些库
import numpy as np
import pandas as pd
import re
from gensim import corpora,models,similarities
import gensim
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
#然后进行读取邮件
df= pd.read_csv("./LDA_Data.csv",encoding="gbk")
#原邮件里面有很多的NAN值,直接扔了
df=df[["Id","ExtractedBodyText"]].dropna()
#之后进行文本的预处理,针对邮件的内容写一组正则表达式
def clean_email_text(text):
text = text.replace("\n"," ")#新行,我们是不需要的
text = re.sub(r"-"," ",text)#把“-”连接的两个单词分开
text = re.sub(r"\d+/\d+/\d+","",text)#去除日期,因为日期对我们的主题分类没有什么影响
text= re.sub(r"[0-2]?[0-9]:[0-6][0-9]","",text)#去除时间,没有意义
text = re.sub(r"[\w]+@[\.\w]+","",text)#去除邮件地址
text = re.sub(r"/[a-zA-Z]*[:\//\]*[A-Za-z0-9\-]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i","",text)#去除网址
#定义一个空字符串
pure_text = ""
for letter in text:
#只留下字母isalpha和空格
if letter.isalpha() or letter==" ":
pure_text+=letter
#再把去除特殊字符字符后的落单的单词排除。剩下有意义的单词
text = " ".join(word for word in pure_text.split() if len(word)>1)
return text
#新建一列
# docs = df["ExtractedBodyText"]
# docs = docs.apply(lambda s:clean_email_text(s))
df["new"]=df["ExtractedBodyText"].apply(lambda s:clean_email_text(s))
#查看处理后的文本
# docs.head(1).values
#将所有的邮件内容拿出来
doclist = df["new"].values
#输出改变表格后的格式
print("修改后为-",df)
#停用词
stoplist = ['very', 'ourselves', 'am', 'doesn', 'through', 'me', 'against', 'up', 'just', 'her', 'ours',
'couldn', 'because', 'is', 'isn', 'it', 'only', 'in', 'such', 'too', 'mustn', 'under', 'their',
'if', 'to', 'my', 'himself', 'after', 'why', 'while', 'can', 'each', 'itself', 'his', 'all', 'once',
'herself', 'more', 'our', 'they', 'hasn', 'on', 'ma', 'them', 'its', 'where', 'did', 'll', 'you',
'didn', 'nor', 'as', 'now', 'before', 'those', 'yours', 'from', 'who', 'was', 'm', 'been', 'will',
'into', 'same', 'how', 'some', 'of', 'out', 'with', 's', 'being', 't', 'mightn', 'she', 'again', 'be',
'by', 'shan', 'have', 'yourselves', 'needn', 'and', 'are', 'o', 'these', 'further', 'most', 'yourself',
'having', 'aren', 'here', 'he', 'were', 'but', 'this', 'myself', 'own', 'we', 'so', 'i', 'does', 'both',
'when', 'between', 'd', 'had', 'the', 'y', 'has', 'down', 'off', 'than', 'haven', 'whom', 'wouldn',
'should', 've', 'over', 'themselves', 'few', 'then', 'hadn', 'what', 'until', 'won', 'no', 'about',
'any', 'that', 'for', 'shouldn', 'don', 'do', 'there', 'doing', 'an', 'or', 'ain', 'hers', 'wasn',
'weren', 'above', 'a', 'at', 'your', 'theirs', 'below', 'other', 'not', 're', 'him', 'during', 'which']
# doc代表一个邮件的内容
texts = [[word for word in doc.lower().split() if word not in stoplist] for doc in doclist]
#建立语料库
#将所有的词转变成了一个词典
dictionary = corpora.Dictionary(texts)
#将每一句话变成一个数组的形式,其中每一个词的形式表示为(num1,num2),num1表示的是在词典中的位置,num2表示出现的次数
corpus = [dictionary.doc2bow(text) for text in texts]
#corpus[4]代表的是第14封邮件中的有意义单词的数字化表示
print(corpus[4])
#建立LDA主题模型
lda = gensim.models.ldamodel.LdaModel(corpus=corpus,id2word=dictionary,num_topics=3)
#第2号主题分类中,最常出现的几个单词
print(lda.print_topic(2,topn=5))
#将所有的主题打印出来
print(lda.print_topics(num_topics = 3,num_words = 5))
#如何对LDA模型进行测试
#给出一句话
sentence = "As long as you are willing to work hard, you can always find your own happiness."
#要对句子做相同的处理
sentence = clean_email_text(sentence)
sentence = [word for word in sentence.lower().split() if word not in stoplist]
print("句子为",sentence)
#将则换句话处理成数组的形式
sentence_bows = dictionary.doc2bow(sentence)
#判断这句话的主题是什么
print(lda.get_document_topics(sentence_bows))
#获取某一个单词的主题
print("词典",sentence_bows)
print(lda.get_term_topics(0))
自己编写的数据跑的实验,数据格式如下
经过代码
df["new"]=df["ExtractedBodyText"].apply(lambda s:clean_email_text(s))
的运行,处理后的表格形式为