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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

西西嘛呦   2019-11-17 我要评论

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

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()

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

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进行噪声过滤,并返回一个对象:

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

然后我们呢把里面的email提取出来:

doclist=docs.values

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

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所需的格式,并过滤掉停用词:

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

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

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次,以此类推。。。

最后,就可以开始构建我们的模型了:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们