-
Notifications
You must be signed in to change notification settings - Fork 3
/
CalcFather.py
executable file
·203 lines (182 loc) · 6.54 KB
/
CalcFather.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import math
import re
#SimiarityMethod = "EditDistance"
SimiarityMethod = "WordVector"
word2vec = {}
len_vec = 0
def init_similarity():
"""
read word2vector data
"""
global word2vec
global len_vec
filename = "abstract_vector.dat"
lines = [line for line in file(filename)]
(num_words, len_vec) = map(int, lines[0].strip().split(' '))
for line in lines[1:]:
items = line.strip().split(' ')
vector = map(float, items[1:])
word2vec[items[0]] = vector
def CalcFather(wordlist, usedset):
"""
Given wordlist and usedset, output the father word
Compare each pair of word, calculate the similarity
Use the most similar word to others as father
"""
score = {}
for word in wordlist:
p = 0.0
for other in wordlist:
sim = WordSimilarty(word, other)
p += sim
if score.has_key(word):
score[word] += p
else:
score[word] = p
bestword = ""
bestscore = 0
for word in wordlist:
if word in usedset:
continue
if score[word] > bestscore or (score[word] == bestscore and len(word) < len(bestword)):
bestword = word
bestscore = score[word]
return bestword
def CalcFatherv2(wordlist, usedset):
"""
Given wordlist and usedset, output the father word
Use the most frequency word as father.
"""
score = {}
for word in wordlist:
if score.has_key(word):
score[word] += 1
else:
score[word] = 1
bestword = ""
bestscore = 0
for word in wordlist:
if word in usedset:
continue
if score[word] > bestscore or (score[word] == bestscore and len(word) < len(bestword)):
bestword = word
bestscore = score[word]
return bestword
def GetWordVector(word):
vec = [0 for dummy in range(len_vec)]
for item in re.split('-|_|\s', word.lower().strip()):
if word2vec.has_key(item):
itemvec = word2vec[item]
for idx in range(len_vec):
vec[idx] += itemvec[idx]
return vec
def WordSimilarty_wordvec(wa, wb):
veca = GetWordVector(wa)
vecb = GetWordVector(wb)
lena = lenb = dot = 0.0
for item in veca:
lena += item * item
for item in vecb:
lenb += item * item
if lena == 0 or lenb == 0:
return 0
for idx in range(len_vec):
dot += veca[idx] * vecb[idx]
return dot / (math.sqrt(lena) * math.sqrt(lenb))
def IsSameWords_wordvec(wa, wb):
sim = WordSimilarty_wordvec(wa, wb)
if sim >= 0.45:
return True
else:
return False
def WordSimilarty_editdis(wa, wb):
lena = len(wa)
lenb = len(wb)
f = list()
for i in range(lena + 1):
j = list([0 for k in range(lenb + 1)])
f.append(j)
for i in range(1, lena + 1):
for j in range(1, lenb + 1):
if wa[i - 1] == wb[j - 1]:
f[i][j] = f[i - 1][j - 1] + 1
else:
f[i][j] = max(f[i - 1][j], f[i][j - 1])
return 1.0 * f[lena][lenb] / max(lena, lenb);
def IsSameWords_editdis(wa, wb):
sim = (1 - WordSimilarty_editdis(wa, wb)) * max(len(wa), len(wb))
if sim < 4.0:
return True
else:
return False
def WordSimilarty(wa, wb):
if SimiarityMethod == "EditDistance":
return WordSimilarty_editdis(wa, wb)
elif SimiarityMethod == "WordVector":
return WordSimilarty_wordvec(wa, wb)
else:
return 0.0
def IsSameWords(wa, wb):
if SimiarityMethod == "EditDistance":
return IsSameWords_editdis(wa, wb)
elif SimiarityMethod == "WordVector":
return IsSameWords_wordvec(wa, wb)
else:
return False
def DuplicatedWords(wordlist, srcWord):
"""
find similar words with srcWord in the wordlist
output list
"""
dup = list()
for word in wordlist:
if IsSameWords(word, srcWord):
dup.append(word)
return dup
def DuplicatedWords4ALL(wordlist):
"""
find all duplicated words in the wordlist
output set
"""
wordSim = {}
for word in wordlist:
simList = list()
for other in wordlist:
if IsSameWords(word, other) and word != other:
simList.append(other)
wordSim[word] = simList
dupSet = set()
for word in wordlist:
if word in dupSet:
continue
simSet = set()
GetSimSet(word, wordSim, simSet)
father = CalcFather(list(simSet), set())
for other in simSet:
if other != father:
dupSet.add(other)
return dupSet
def GetSimSet(word, wordSim, simSet):
if word in simSet:
return
simSet.add(word)
for other in wordSim[word]:
GetSimSet(other, wordSim, simSet)
#init_similarity()
#print GetWordVector("complex network")
# print WordSimilarty("cluster", "clusters")
# print WordSimilarty("cluster", "community")
# print WordSimilarty("cluster", "recommender")
#wordlist = ['cluster', 'clusters', 'clustering', 'community' ,'communities', 'recommender', 'recommendation', 'recommend']
#wordlist.append('recommend')
#print DuplicatedWords4ALL(wordlist)
#print CalcFatherv2(wordlist, set())
#wordlist = ['cluster', 'clusters', 'clustering', 'community' ,'communities']
#print CalcFather(wordlist, set())
#wordlist = ['recommender', 'recommendation', 'recommend']
#print CalcFather(wordlist, set())
# fout = open('level1_father.txt', 'w')
# for line in file('../data/level1_lower.txt'):
# items = line.strip().split(' ')
# fout.write(CalcFather(items, set()) + '\n')
# fout.close()