参考链接:https://blog.csdn.net/whzhcahzxh/article/details/17528261 demo1:结巴分词: # 构造分词库,格式如下: ‘‘‘ [[‘同事‘, ‘意见‘, ‘相左‘, ‘我会‘, ‘心中‘, ‘回顾‘, ‘事情‘,
参考链接:https://blog.csdn.net/whzhcahzxh/article/details/17528261
demo1:结巴分词:
# 构造分词库,格式如下: ‘‘‘ [[‘同事‘, ‘意见‘, ‘相左‘, ‘我会‘, ‘心中‘, ‘回顾‘, ‘事情‘, ‘原委‘, ‘同事‘, ‘意见‘, ‘建议‘, ‘讨论‘, ‘结合实际‘, ‘寻找‘, ‘接受‘, ‘方案‘, ‘解决问题‘, ‘集思广益‘, ‘完美‘, ‘解决问题‘], [‘车行‘, ‘做车‘, ‘贷‘, ‘有个‘, ‘客户‘, ‘在意‘, ‘利息‘, ‘客户‘, ‘说‘, ‘利息‘, ‘综合‘, ‘资质‘, ‘帮‘, ‘审批‘, ‘低息‘, ‘配合‘, ‘客户‘, ‘乐意‘, ‘配合‘, ‘最终‘, ‘批出来‘, ‘接受‘, ‘转‘, ‘介绍‘, ‘客户‘], [‘楼下‘, ‘买‘, ‘水果‘, ‘这家‘, ‘店‘, ‘价格比‘, ‘店要‘, ‘高‘, ‘‘, ‘‘, ‘%‘, ‘价格‘, ‘太高‘, ‘老板‘, ‘说‘, ‘老板‘, ‘您好‘, ‘家‘, ‘水果‘, ‘很漂亮‘, ‘新鲜‘, ‘进货‘, ‘价格‘, ‘挺‘, ‘高‘, ‘我刚‘, ‘搬‘, ‘喜欢‘, ‘吃水果‘, ‘购买‘, ‘老板‘, ‘回应‘, ‘说‘, ‘价格‘, ‘优惠‘, ‘一点‘, ‘更好‘, ‘老板‘, ‘微笑‘, ‘说‘, ‘长期‘, ‘购买‘, ‘‘, ‘‘, ‘折‘, ‘说‘, ‘老板‘, ‘真‘, ‘大方‘, ‘就常‘, ‘买‘, ‘‘, ‘‘, ‘折‘, ‘老板‘, ‘听后‘, ‘计算机‘, ‘算了‘, ‘‘, ‘‘, ‘折‘, ‘再少‘, ‘开心‘, ‘‘, ‘‘, ‘折‘, ‘价格‘, ‘购买‘, ‘水果‘], [‘工作‘, ‘做‘, ‘行政助理‘, ‘跟着‘, ‘老板‘, ‘一年‘, ‘工资‘, ‘变化‘, ‘‘, ‘‘, ‘有次‘, ‘老板‘, ‘聊天‘, ‘问‘, ‘一年‘, ‘表现‘, ‘有没有‘, ‘批评‘, ‘指导‘, ‘一年‘, ‘表现‘, ‘帮‘, ‘加‘, ‘工资‘, ‘老板‘, ‘‘, ‘‘, ‘帮‘, ‘工资‘, ‘提高‘]] ‘‘‘ stopwords = [line.strip() for line in codecs.open(‘./data/stopwords.txt‘, ‘r‘, encoding = ‘utf-8‘).readlines()] train = [] fp = codecs.open(‘./result/demo5_sheet1_1.csv‘,‘r‘,encoding=‘utf8‘) for line in fp.readlines(): line = line.strip() if not len(line):#判断是否为空行 continue outstr = ‘ ‘ seg_list =jieba.cut(line,cut_all=False)#采用精确模式分词,效果最好 for word in seg_list: if word not in stopwords: if word != ‘\t‘: outstr += word outstr += " " train.append(outstr.strip().split(" "))#字符串转列表 fp.close()
详细实例:
# coding=utf-8 import codecs import gensim import jieba from gensim import corpora,models,similarities from gensim.models import LdaModel from gensim.corpora import Dictionary # 构造分词库,格式如下: ‘‘‘ [[‘同事‘, ‘意见‘, ‘相左‘, ‘我会‘, ‘心中‘, ‘回顾‘, ‘事情‘, ‘原委‘, ‘同事‘, ‘意见‘, ‘建议‘, ‘讨论‘, ‘结合实际‘, ‘寻找‘, ‘接受‘, ‘方案‘, ‘解决问题‘, ‘集思广益‘, ‘完美‘, ‘解决问题‘], [‘车行‘, ‘做车‘, ‘贷‘, ‘有个‘, ‘客户‘, ‘在意‘, ‘利息‘, ‘客户‘, ‘说‘, ‘利息‘, ‘综合‘, ‘资质‘, ‘帮‘, ‘审批‘, ‘低息‘, ‘配合‘, ‘客户‘, ‘乐意‘, ‘配合‘, ‘最终‘, ‘批出来‘, ‘接受‘, ‘转‘, ‘介绍‘, ‘客户‘], [‘楼下‘, ‘买‘, ‘水果‘, ‘这家‘, ‘店‘, ‘价格比‘, ‘店要‘, ‘高‘, ‘‘, ‘‘, ‘%‘, ‘价格‘, ‘太高‘, ‘老板‘, ‘说‘, ‘老板‘, ‘您好‘, ‘家‘, ‘水果‘, ‘很漂亮‘, ‘新鲜‘, ‘进货‘, ‘价格‘, ‘挺‘, ‘高‘, ‘我刚‘, ‘搬‘, ‘喜欢‘, ‘吃水果‘, ‘购买‘, ‘老板‘, ‘回应‘, ‘说‘, ‘价格‘, ‘优惠‘, ‘一点‘, ‘更好‘, ‘老板‘, ‘微笑‘, ‘说‘, ‘长期‘, ‘购买‘, ‘‘, ‘‘, ‘折‘, ‘说‘, ‘老板‘, ‘真‘, ‘大方‘, ‘就常‘, ‘买‘, ‘‘, ‘‘, ‘折‘, ‘老板‘, ‘听后‘, ‘计算机‘, ‘算了‘, ‘‘, ‘‘, ‘折‘, ‘再少‘, ‘开心‘, ‘‘, ‘‘, ‘折‘, ‘价格‘, ‘购买‘, ‘水果‘], [‘工作‘, ‘做‘, ‘行政助理‘, ‘跟着‘, ‘老板‘, ‘一年‘, ‘工资‘, ‘变化‘, ‘‘, ‘‘, ‘有次‘, ‘老板‘, ‘聊天‘, ‘问‘, ‘一年‘, ‘表现‘, ‘有没有‘, ‘批评‘, ‘指导‘, ‘一年‘, ‘表现‘, ‘帮‘, ‘加‘, ‘工资‘, ‘老板‘, ‘‘, ‘‘, ‘帮‘, ‘工资‘, ‘提高‘]] ‘‘‘ stopwords = [line.strip() for line in codecs.open(‘./data/stopwords.txt‘, ‘r‘, encoding = ‘utf-8‘).readlines()] train = [] fp = codecs.open(‘./result/demo5_sheet1_1.csv‘,‘r‘,encoding=‘utf8‘) for line in fp.readlines(): line = line.strip() if not len(line):#判断是否为空行 continue outstr = ‘ ‘ seg_list =jieba.cut(line,cut_all=False)#采用精确模式分词,效果最好 for word in seg_list: if word not in stopwords: if word != ‘\t‘: outstr += word outstr += " " train.append(outstr.strip().split(" "))#字符串转列表 fp.close() # print(type(train)) # print(train) # exit() #各个词或词组在字典中的编号 ‘‘‘ 北京 编号为:12 搭 编号为:6 的 编号为:9 喜欢 编号为:1 不 编号为:10 东西 编号为:4 土豆 编号为:2 霾 编号为:14 是 编号为:7 个 编号为:5 雾 编号为:13 百 编号为:8 今天 编号为:11 我 编号为:3 吃 编号为:0 ‘‘‘ dic = corpora.Dictionary(train) for name,index in dic.token2id.items(): print(name+":"+str(index)) break#执行一次 # 词典生成好之后,就开始生成语料库了 ‘‘‘ corpus=[[(0, 1), (1, 1), (2, 1), (3, 1)], [(2, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1)], [(1, 1), (3, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1)]] 每个中括号代表一句话,用逗号隔开,(0,1)代表词典中编号为0的词出现了一次,以此类推,很好理解 ‘‘‘ corpus = [ dic.doc2bow(text) for text in train ] # print(corpus) # exit() ‘‘‘ 得到了语料库,接下来做一个TF-IDF变换 可以理解成 将用词频向量表示一句话 变换成为用 词的重要性向量表示一句话 (TF-IDF变换:评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度。字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降。) vec是查询文本向量,比较vec和文档中的文本的相似度 执行结果: [(0, 0.7071067811865475), (4, 0.7071067811865475)] [(0, 0.8425587958192721), (1, 0.3109633824035548), (2, 0.3109633824035548), (3, 0.3109633824035548)] [(2, 0.16073253746956623), (4, 0.4355066251613605), (5, 0.4355066251613605), (6, 0.4355066251613605), (7, 0.4355066251613605), (8, 0.4355066251613605), (9, 0.16073253746956623)] [(1, 0.1586956620869655), (3, 0.1586956620869655), (9, 0.1586956620869655), (10, 0.42998768831312806), (11, 0.42998768831312806), (12, 0.42998768831312806), (13, 0.42998768831312806), (14, 0.42998768831312806)] ‘‘‘ tfidf = models.TfidfModel(corpus) vec =[(0,1),(4,1)] corpus_tfidf = tfidf[corpus] for doc in corpus_tfidf: print(doc) break#执行一次 # exit() ‘‘‘ 得到比较结果: [(0, 0.59577906), (1, 0.30794966), (2, 0.0)] 表示和第1句话相似度为59.578%,和第二句话的相似度位30.79%,第三句没有相似度, 我们看看vec这句话是什么:0为吃,4为东西,所以vec这句话可以是["吃东西"]或者["东西吃"] 而第一句话"我喜欢吃土豆","土豆是个百搭的东西"明显有相似度,而第三句话"我不喜欢今天雾霾的北京",相似度几乎为0,至于为什么第一句比第二句更相似,就需要考虑TfIdf document representation和cosine similarity measure了 ‘‘‘ # index = similarities.SparseMatrixSimilarity(tfidf[corpus],num_features=3) # sims =index[tfidf[vec]] # print(list(enumerate(sims)))#报错待调试 ‘‘‘ 回到tfidf转换,接着训练LSI模型,假定文档属于10个主题, ‘‘‘ lsi = models.LsiModel(corpus_tfidf,id2word=dic,num_topics=10) lsiout =lsi.print_topics(10) print(lsiout[0]) # exit() ‘‘‘ 基于SVD建立的两个主题模型内容,将文章投影到主题空间中 输出: [(0, -0.70861576320682107), (1, 0.1431958007198823)] [(0, -0.42764142348481798), (1, -0.88527674470703799)] [(0, -0.66124862582594512), (1, 0.4190711252114323)] 因此第一三两句和主题一相似,第二句和主题二相似 ‘‘‘ corpus_lsi = lsi[corpus_tfidf] for doc in corpus_lsi: print(doc) break ‘‘‘ LDA 得到的结果每次都变,给一次的输出: 0.077*吃 + 0.075*北京 + 0.075*雾 + 0.074*今天 + 0.073*不 + 0.072*霾 + 0.070*喜欢 + 0.068*我 + 0.062*的 + 0.061*土豆 0.091*吃 + 0.073*搭 + 0.073*土豆 + 0.073*个 + 0.073*是 + 0.072*百 + 0.071*东西 + 0.066*我 + 0.065*喜欢 + 0.059*霾 [(0, 0.31271095988105352), (1, 0.68728904011894654)] [(0, 0.19957991735916861), (1, 0.80042008264083142)] [(0, 0.80940337254233863), (1, 0.19059662745766134)] 第一二句和主题二相似,第三句和主题一相似 结论和LSI不一样,我估计这和样本数目太少,区别度不高有关,毕竟让我来区分把第一句和哪一句分在一个主题,我也不确定 ‘‘‘ lda = models.LdaModel(corpus_tfidf, id2word=dic, num_topics=10) ldaOut=lda.print_topics(10) print (ldaOut[0]) print (ldaOut[1]) corpus_lda = lda[corpus_tfidf] for doc in corpus_lda: print (doc) break ‘‘‘ 输入一句话,查询属于LSI得到的哪个主题类型,先建立索引: 输出: [(13, 1), (14, 1)] [(0, 0.50670602027401368), (1, -0.3678056037187441)] 与第一个主题相似 ‘‘‘ index = similarities.MatrixSimilarity(lsi[corpus]) query = "客户" query_bow = dic.doc2bow(list(jieba.cut(query))) print (query_bow) query_lsi = lsi[query_bow] print (query_lsi) ‘‘‘ 比较和第几句话相似,用LSI得到的索引接着做,并排序输出 [(0, 0.90161765), (1, -0.10271341), (2, 0.99058259)] [(2, 0.99058259), (0, 0.90161765), (1, -0.10271341)] 可见和第二句话相似度很高,因为只有第二句话出现了雾霾两个词,可是惊讶的是和第一句话的相似度也很高,这得益于LSI模型的算法:在A和C共现,B和C共现的同时,可以找到A和B的相似度 ‘‘‘