-
Notifications
You must be signed in to change notification settings - Fork 0
/
topk.py
178 lines (155 loc) · 6.65 KB
/
topk.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from openpyxl import Workbook
from openpyxl import load_workbook
import difflib
import time
import re
from simhash import Simhash
def topk(todo):
# start
timeStart = time.time()
command = list(todo.split())
k = command[0] # the first is topk
lens = len(command)
# getExcel:
# 实例化
wb = Workbook()
# 激活 worksheet
ws = wb.active
wb2 = load_workbook('测试.xlsx')
sheet = wb2["关联关系"]
max_row = sheet.max_row
# creat Array:
boardD = ['s' for i in range(max_row)]
boardA = ['s' for i in range(max_row)]
boardB = ['s' for i in range(max_row)]
similarities_of_data_element = [0 for i in range(max_row)]
# read the data element:
count = 0
for row in sheet.rows:
boardD[count] = row[3].value
count += 1
# only have : topk \ B \ D
if lens == 3:
for i in range(0, max_row):
similarities_of_data_element[i] = difflib.SequenceMatcher(None, command[2], boardD[i]).ratio()
# only have : topk \ A \ B \ D
if lens == 4:
for i in range(0, max_row):
similarities_of_data_element[i] = difflib.SequenceMatcher(None, command[3], boardD[i]).ratio()
# only have : topk \ B \ D
if lens == 3:
for i in range(0, max_row):
a_simhash = Simhash(command[2])
b_simhash = Simhash(boardD[i])
max_hashbit = max(len(bin(a_simhash.value)), len(bin(b_simhash.value)))
# 汉明距离
distince = a_simhash.distance(b_simhash)
similar = 1 - distince / max_hashbit
similarities_of_data_element[i] = (similar + similarities_of_data_element[i]) / 2
# only have : topk \ A \ B \ D
if lens == 4:
for i in range(0, max_row):
a_simhash = Simhash(command[3])
b_simhash = Simhash(boardD[i])
max_hashbit = max(len(bin(a_simhash.value)), len(bin(b_simhash.value)))
# 汉明距离
distince = a_simhash.distance(b_simhash)
similar = 1 - distince / max_hashbit
similarities_of_data_element[i] = (similar + similarities_of_data_element[i]) / 2
# choose the top50percent:
num_of_chosen = 0
numk = int(k)
for i in range(0, max_row):
if similarities_of_data_element[i] >= 0.5:
num_of_chosen += 1
if numk > num_of_chosen:
num_of_chosen = numk
num_dict = {}
for i in range(len(similarities_of_data_element)):
num_dict[i] = similarities_of_data_element[i]
res_list = sorted(num_dict.items(), key=lambda e: e[1])
while similarities_of_data_element[max_row - num_of_chosen - 1] == similarities_of_data_element[
max_row - num_of_chosen] and max_row - num_of_chosen - 1 >= 0:
num_of_chosen = num_of_chosen + 1
data_element_largestTopk_index = list([one[0] for one in res_list[::-1][:num_of_chosen]])
# compare the largest similarities' topk:
similarities_of_institute_name = [0 for i in range(0, num_of_chosen)]
similarities_of_table_Chinese_name = [0 for i in range(0, num_of_chosen)]
# read the institute name/A:
count = 0
for i in range(0, max_row):
boardA[i] = None
for row in sheet.rows:
if row[0].value is not None:
boardA[count] = row[0].value
count += 1
# only have : topk \ B \ D
if lens == 3:
for i in range(0, num_of_chosen):
if boardA[data_element_largestTopk_index[i]] is not None:
similarities_of_institute_name[i] = 0
else:
similarities_of_institute_name[i] = 1
# only have : topk \ A \ B \ D
elif lens == 4:
for i in range(0, num_of_chosen):
if boardA[data_element_largestTopk_index[i]] is not None:
similarities_of_institute_name[i] = difflib.SequenceMatcher(None, command[1], boardA[
data_element_largestTopk_index[i]]).ratio()
a_simhash = Simhash(command[1])
b_simhash = Simhash(boardA[data_element_largestTopk_index[i]])
max_hashbit = max(len(bin(a_simhash.value)), len(bin(b_simhash.value)))
# 汉明距离
distince = a_simhash.distance(b_simhash)
similar = 1 - distince / max_hashbit
similarities_of_institute_name[i] = (similarities_of_institute_name[i] + similar ) / 2
else:
similarities_of_institute_name[i] = 0
# read the table Chinese name/B:
count = 0
for i in range(0, max_row):
boardB[i] = None
for row in sheet.rows:
boardB[count] = row[1].value
count += 1
for i in range(0, num_of_chosen):
similarities_of_table_Chinese_name[i] = difflib.SequenceMatcher(None, command[2], boardB[
data_element_largestTopk_index[i]]).ratio()
a_simhash = Simhash(command[2])
b_simhash = Simhash(boardB[data_element_largestTopk_index[i]])
max_hashbit = max(len(bin(a_simhash.value)), len(bin(b_simhash.value)))
# 汉明距离
distince = a_simhash.distance(b_simhash)
similar = 1 - distince / max_hashbit
similarities_of_table_Chinese_name[i] = (similarities_of_table_Chinese_name[i] + similar) / 2
# calculate the similarities:
similarities = [0 for i in range(0, num_of_chosen)]
for i in range(0, num_of_chosen):
similarities[i] = (similarities_of_table_Chinese_name[i] + similarities_of_institute_name[i] +
similarities_of_data_element[data_element_largestTopk_index[i]]) / 3
###
# rank the topk:
num_of_topk = int(k)
num_sort = {}
for i in range(len(similarities)):
num_sort[i] = similarities[i]
res_list2 = sorted(num_sort.items(), key=lambda e: e[1])
largestTopk_index = list([one[0] for one in res_list2[::-1][:num_of_topk]])
# print:
for i in range(0, num_of_topk):
print(data_element_largestTopk_index[largestTopk_index[i]] + 1,
boardA[data_element_largestTopk_index[largestTopk_index[i]]],
boardB[data_element_largestTopk_index[largestTopk_index[i]]],
boardD[data_element_largestTopk_index[largestTopk_index[i]]], similarities[largestTopk_index[i]])
# end
timeEnd = time.time()
runTime = timeEnd - timeStart
print("The running time is " + str(runTime) + "s", end="")
result = str(data_element_largestTopk_index[largestTopk_index[0]]) + " " + str(runTime)
for i in range(0, num_of_topk):
result = result + " " + str(data_element_largestTopk_index[largestTopk_index[i]])
return result
if __name__ == "__main__":
# read from the terminal:
string = input("Please enter k and the string you want to compare\n")
topk(string)