文本相似性计算

文本相似性计算三个阶段:
1. 字面的匹配相似
2. 词汇的匹配相似
3. 语义的匹配相似

一、JaccardSimilarity方法
对文本进行分词,然后对每一个单词分配一个唯一的ID(token),为了计算文本之间的相似性。JaccardSimilarity方法的计算方法是:
两个集合的交集/两个集合的并集
二、文本的向量化
文本->向量化为向量->向量空间中的某一个点->求两个点(即两个文本)之间的距离->得到文档的相似性
2.1 简单的向量化
为每一个词语分配一个唯一的ID,假设所有的词语个数为N,用数组表示就是大小为N数组的下表。然后,如果文档中对应位置的词出现就将该位置置为1
2.2 TF-IDF向量化
通过TF-IDF向量化的方法,可以将每个词向量化成一个表示权重的小数,而不是0或1,它已经带有了文本的信息了。向量化后每一个词都带上了TF-IDF信息了,而TF-IDF的作用就是保留词在文档中的权重信息,这就相当于保留了文本的信息。于是,我们通过token的概念和TF-IDF方法,就把一个本文向量化了,并且向量化完了以后还保留了文本本身的信息,每一个向量就是一个前面提到的词袋。
     实践:利用gensim的库corpora、models、similarities实现文档相似性的计算: 
 

 1 训练语料:LDA_text.txt
 2 Human machine interface for lab abc computer applications Human Human
 3 A survey of user opinion of computer system response time
 4 The EPS user interface management with system
 5 System and human system engineering testing of EPS
 6 Relation of user perceived response time to error measurement
 7 The generation of random binary unordered trees
 8 The intersection graph of paths in trees
 9 Graph minors IV Widths of trees and well quasi ordering
10 Graph minors A survey
View Code
 1 from gensim import corpora, models, similarities
 2 
 3 if __name__=='__main__':
 4     f = open('./LDA_test.txt', 'r')
 5     stop_list = 'for a of the and to in'.split()
 6     texts = [[word for word in line.strip().lower().split() if word not in stop_list] for line in f.readlines()]
 7 
 8     # build dictionary
 9     dictionary = corpora.Dictionary(texts) # construct dictionary for all documents and the length of dic is the number of uniq words
10     corpus = [dictionary.doc2bow(text) for text in texts] # transform document to bag of words representaion according to dictionray
11 
12     # calculates the idf for each word in document
13     tfidf_model = models.TfidfModel(corpus)
14     tfidf = tfidf_model[corpus] # [] method transform the bow representation to tfidf
15 
16     query = 'human system with System engineering testing'
17     query_bow = dictionary.doc2bow(query.split())
18     query_tfidf = tfidf_model[query_bow] # calculate the query itfidf reprensentation using the bow  reprensentation
19     # print query_tfidf
20     similarity = similarities.Similarity('Similarity-index',tfidf, num_features=600)
21     similarity.num_best = 3
22     print 'query =', query
23     for item in similarity[query_tfidf]:
24         print ' '.join(texts[item[0]]), item[1] # 打印top3相似的文档和文档相似性
View Code
1 输出结果
2                         document                                       score
3 system human system engineering testing eps     0.775833368301
4 eps user interface management with system       0.349639117718
5 human machine interface lab abc computer applications human human       0.240938946605
6                 
View Code

三、向量空间模型(VSM)
对本文进行向量化完了之后,就是将文本映射为向量空间中的一个点。然后,通过计算向量空间中的两个点之间距离的方法计算文本之间的相似性:
3.1 欧式距离
3.2 余弦相似度距离
四、LDA主题模型
前述的方法构建文本向量的方法:只是机械的计算了词的向量,并没有任何上下文的关系,所有思想还停留在机器层面,还没有到更高层次上来
4.2 LDA
P(W(词)|D(文章))=P(W(词)|T(主题))*P(T(主题)|D(文章))
(1)P(W(词)|D(文章)) 这个其实是可以直接统计出来的
(2)P(W(词)|T(主题)) 这个是模型的一部分,是要求出来的
(3)P(T(主题)|D(文章)) 这个是最后分类的结果
因此,模型的关键是求出来每一个词所属的主题分布情况。当来了一片新的文档后,统计出该文档属于每一个主题的概率分布。

转载于:https://www.cnblogs.com/mjbjtunlp/p/7586909.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值