forked from liuhuanyong/SentenceSimilarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_vsm.py
72 lines (58 loc) · 2.06 KB
/
sim_vsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# coding: utf-8
# File: sim_vsm.py
# Author: lhy<[email protected],https://huangyong.github.io>
# Date: 18-4-27
import jieba.posseg as pesg
import math
import numpy as np
class SimVsm:
'''比较相似度'''
def distance(self, text1, text2):
words1 = [word.word for word in pesg.cut(text1) if word.flag[0] not in ['u', 'x', 'w']]
words2 = [word.word for word in pesg.cut(text2) if word.flag[0] not in ['u', 'x', 'w']]
tfidf_reps = self.tfidf_rep([words1, words2])
return self.cosine_sim(np.array(tfidf_reps[0]), np.array(tfidf_reps[1]))
'''对句子进行tfidf向量表示'''
def tfidf_rep(self, sents):
sent_list = []
df_dict = {}
tfidf_list = []
for sent in sents:
tmp = {}
for word in sent:
if word not in tmp:
tmp[word] = 1
else:
tmp[word] += 1
tmp = {word:word_count/sum(tmp.values()) for word, word_count in tmp.items()}
for word in set(sent):
if word not in df_dict:
df_dict[word] = 1
else:
df_dict[word] += 1
sent_list.append(tmp)
df_dict = {word :math.log(len(sents)/df+1) for word, df in df_dict.items()}
words = list(df_dict.keys())
for sent in sent_list:
tmp = []
for word in words:
tmp.append(sent.get(word, 0))
tfidf_list.append(tmp)
return tfidf_list
'''余弦相似度计算相似度'''
def cosine_sim(self, vector1, vector2):
cos1 = np.sum(vector1 * vector2)
cos21 = np.sqrt(sum(vector1 ** 2))
cos22 = np.sqrt(sum(vector2 ** 2))
similarity = cos1 / float(cos21 * cos22)
return similarity
def test():
text1 = '南昌是江西的省会'
text2 = '北京乃中国之首都'
text1 = '周杰伦是一个歌手'
text2 = '刘若英是个演员'
simer = SimVsm()
sim = simer.distance(text1, text2)
print(sim)
test()