【天然语言处理】利用LDA对希拉里邮件进行主题分析

首先是读取数据集,并将csv中ExtractedBodyText为空的给去除掉html

import pandas as pd import re import os dir_path=os.path.dirname(os.path.abspath(__file__)) data_path=dir_path+"/Database/HillaryEmails.csv" df=pd.read_csv(data_path) df=df[['Id','ExtractedBodyText']].dropna()

对于这些邮件信息,并非全部的词都是有意义的,也就是先要去除掉一些噪声数据:app

def clean_email_text(text): text = text.replace('\n'," ") #新行,咱们是不须要的
    text = re.sub(r"-", " ", text) #把 "-" 的两个单词,分开。(好比:july-edu ==> july edu)
    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 = ''
    # 以防还有其余特殊字符(数字)等等,咱们直接把他们loop一遍,过滤掉
    for letter in text: # 只留下字母和空格
        if letter.isalpha() or letter==' ': pure_text += letter # 再把那些去除特殊字符后落单的单词,直接排除。
    # 咱们就只剩下有意义的单词了。
    text = ' '.join(word for word in pure_text.split() if len(word)>1) return text

而后取出ExtractedBodyText的那一列,对每一行email进行噪声过滤,并返回一个对象:oop

docs = df['ExtractedBodyText'] docs = docs.apply(lambda s: clean_email_text(s))

而后咱们呢把里面的email提取出来:this

doclist=docs.values

接下来,咱们使用gensim库来进行LDA模型的构建,gensim可用指令pip install -U gensim安装。可是,要注意输入到模型中的数据的格式。例如:[[一条邮件字符串],[另外一条邮件字符串], ...]转换成[[一,条,邮件,在,这里],[第,二,条,邮件,在,这里],[今天,天气,肿么,样],...]。对于英文的分词,只须要对空白处分割便可。同时,有些词语(不一样于噪声)是没有意义的,咱们要过滤掉那些没有意义的词语,这里简单的写一个中止词列表:spa

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']

而后咱们将输入转换成gensim所需的格式,并过滤掉停用词:code

texts = [[word for word in doc.lower().split() if word not in stoplist] for doc in doclist]

再将这全部的单词放入到一个词袋中,把每一个单词用一个数字index指代:htm

from gensim import corpora, models, similarities import gensim dictionary = corpora.Dictionary(texts)

再分别统计每一篇email中每一个词语在这个词袋中出现的次数,并返回一个列表:对象

corpus = [dictionary.doc2bow(text) for text in texts]

 这个列表告诉咱们,第14(从0开始是第一)个邮件中,一共6个有意义的单词(通过咱们的文本预处理,并去除了中止词后)其中,51号单词出现1次,505号单词出现1次,以此类推。。。blog

最后,就能够开始构建咱们的模型了:ip

lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20) print(lda.print_topic(10, topn=5))

 能够看到,第11个主题最经常使用的单词,接下来,咱们看下全部的主题:

for i in lda.print_topics(num_topics=20, num_words=5): print(i)

 咱们再看下第一篇email属于哪个主题:

print(lda.get_document_topics(corpus[0]))

 属于第四个主题的几率是0.95

相关代码和数据:连接: https://pan.baidu.com/s/1sl1I5IeQFDHjVwf2a0C89g 提取码: xqqf 

原文出处:https://www.cnblogs.com/xiximayou/p/11876580.html