-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaccard_f.py
158 lines (91 loc) · 3.18 KB
/
jaccard_f.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import random
def get_shingles(filename):
comb = []
with open(filename, 'r') as file:
for line in file.readlines():
comb.extend(line.split(' '))
shingles = []
for com in comb:
shingles.append("".join(com))
return shingles
def get_shingles_from_text(text):
comb = []
comb.extend(x.strip() for x in text.split(' '))
shingles = []
for com in comb:
shingles.append("".join(com))
return shingles
def get_matrix_dict(shingles_1, shingles_2):
shin_dict = {}
for hash1 in shingles_1:
shin_dict[hash1] = [1, 0]
for hash2 in shingles_2:
if (shin_dict.get(hash2)):
shin_dict[hash2] = [1, 1]
else:
shin_dict[hash2] = [0, 1]
return shin_dict
def jaccard_similarity(shin_dict) -> None:
print(shin_dict)
intersected = 0
union = len(shin_dict.keys())
for v in shin_dict.values():
if (v == [0, 0] or v == [1, 1]):
intersected += 1
with open('output.txt', 'w',encoding="utf-8") as file:
for k in shin_dict.keys():
file.write(f"{k} {shin_dict.get(k)[0]} {shin_dict.get(k)[1]}\n")
print(f'intersection: {intersected}')
print(f'union: {union}')
return intersected / union
def get_min_hash(shin_dict, k=10):
keys = shin_dict.keys()
one_count = 0
sign_matr = []
for _ in range(k):
temp = []
temp.extend(keys)
random.shuffle(temp)
temp_count = get_single_min_hash(temp, shin_dict)
sign_matr.append(get_signature_matrix(temp, shin_dict))
print(f'for permutation {temp[:10]}: {temp_count}')
one_count += temp_count
print(f'Sign matrix: {sign_matr}')
return one_count / k
def get_signature_matrix(keys, shin_dict):
first = -1
second = -1
count = 0
for k in keys:
if not first >= 0:
if shin_dict.get(k)[0] == 1:
first = count
if not second >= 0:
if shin_dict.get(k)[1] == 1:
second = count
count += 1
if first >= 0 and second >= 0:
break
return [first, second]
def get_single_min_hash(keys, shin_dict):
for k in keys:
if shin_dict.get(k)[0] == 1 or shin_dict.get(k)[1] == 1:
return 1 if shin_dict.get(k)[0] == shin_dict.get(k)[1] else 0
def similarity(filename1, filename2):
shingles_1 = get_shingles(filename1)
shingles_2 = get_shingles(filename2)
shin_dict = get_matrix_dict(shingles_1, shingles_2)
jacc_sim = jaccard_similarity(shin_dict)
min_hash_similarity = get_min_hash(shin_dict, 1000)
print(f'Min hash similarity= {min_hash_similarity}')
print(f'Jaccard similarity= {jacc_sim}')
def similarity_from_text(text1, text2):
shingles_1 = get_shingles_from_text(text1)
shingles_2 = get_shingles_from_text(text2)
shin_dict = get_matrix_dict(shingles_1, shingles_2)
jacc_sim = jaccard_similarity(shin_dict)
min_hash_similarity = get_min_hash(shin_dict, 1000)
print(f'Min hash similarity= {min_hash_similarity}')
print(f'Jaccard similarity= {jacc_sim}')
if __name__ == "__main__":
similarity("doc1", "doc2")