-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodenames - Word2Vec.py
357 lines (269 loc) · 8.09 KB
/
Codenames - Word2Vec.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python
# coding: utf-8
# We'll use the python library gensim: https://radimrehurek.com/gensim/
import gensim
from gensim import corpora
import spacy
nlp = spacy.load('en_core_web_lg')
from scipy.spatial.distance import cosine
import numpy as np
import random
# Load a premade word2vec model built on Google News articles.
#
# Download from: https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM
model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True, limit=500000)
# We limit the vocabulary to the 500,000 most common words. Even at 500,000, it starts to get to nonsense words.
# Here are the top 50 and bottom 50 words by frequency. And even for the real words that are infrequent, if a word is too obscure, it wouldn't make for a good clue.
# print("Most common:",model.index2word[:50])
# print("Least common:",model.index2word[-50:])
# Here's an example Codenames board. `blue` is one team's words, `red` the other and `assassin` is the assassin word.
f = open('./wordlistmini','r')
# transform = random.randint(1,5)
print("请输入棋盘难易度1-5:")
transform = input()
lines1 = f.readlines()
matrix1 = []
for i in range(0, len(lines1)):
matrix1.append(lines1[i])
f.close()
for i in range(0, len(matrix1)):
matrix1[i] = matrix1[i].strip('\n')
text = " ".join(matrix1)
doc = nlp(text)
# embedding = []
word_list = []
for token in doc:
word_list.append(token.text)
# for word in word_list:
# embedding.append(nlp.vocab[word].vector)
#
# embedding = np.array(embedding)
def vector_similarity(x, y):
np.seterr(invalid='ignore')
return 1 - cosine(x, y)
num = random.choice(range(0, len(word_list)))
a1 = word_list[num]
arr1 = [] #与a1含义相近
arr2 = [] #与a1含义不相近
for i in range(0, len(word_list)):
if num!=i:
sim = vector_similarity(nlp.vocab[a1].vector, nlp.vocab[word_list[i]].vector)
if sim >= 0.45:
arr1.append(word_list[i])
else:
arr2.append(word_list[i])
#--------------a2------------------------
if transform == 1:
a2 = random.choice(arr2)
else:
a2 = random.choice(arr1)
arr3 = [] #与a1和a2含义都不相近
arr3_1 = [] #与a1和a2含义都相近
for i in range(0, len(arr2)):
sim = vector_similarity(nlp.vocab[a2].vector, nlp.vocab[arr2[i]].vector)
if sim < 0.45:
arr3.append(arr2[i])
else:
arr3_1.append(arr2[i])
arr3_1 = arr3_1 + arr1
arr3_1.remove(a2)
#--------------a3------------------------
if transform == 1 or transform == 2:
a3 = random.choice(arr3)
else:
a3 = random.choice(arr3_1)
arr4 = [] #与a1和a2和a3含义都不相近
arr4_1 = [] #与a1和a2和a3含义都相近
for i in range(0, len(arr3)):
sim = vector_similarity(nlp.vocab[a3].vector, nlp.vocab[arr3[i]].vector)
if sim < 0.45:
arr4.append(arr3[i])
else:
arr4_1.append(arr3[i])
arr4_1 = arr4_1 + arr3_1
arr4_1.remove(a3)
#--------------a4------------------------
if transform == 4 or transform == 5:
a4 = random.choice(arr4_1)
else:
a4 = random.choice(arr4)
arr5 = [] #与a1、a2、a3、a4含义都不相近
arr5_1 = [] #与a1、a2、a3、a4含义都相近
for i in range(0, len(arr4)):
sim = vector_similarity(nlp.vocab[a4].vector, nlp.vocab[arr4[i]].vector)
if sim < 0.45:
arr5.append(arr4[i])
else:
arr5_1.append(arr4[i])
arr5_1 = arr5_1 + arr4_1
arr5_1.remove(a4)
#--------------a5------------------------
if transform == 5:
a5 = random.choice(arr5_1)
else:
a5 = random.choice(arr5)
arr_far = [] #与a1、a2、a3、a4、a5含义都不相近
arr_near = [] #与a1、a2、a3、a4、a5含义都相近
for i in range(0, len(arr5)):
sim = vector_similarity(nlp.vocab[a5].vector, nlp.vocab[arr5[i]].vector)
if sim < 0.45:
arr_far.append(arr5[i])
else:
arr_near.append(arr5[i])
arr_near = arr_near + arr5_1
arr_near.remove(a5)
# print(a1, a2, a3, a4, a5)
blue = [a1, a2, a3, a4, a5]
# print(blue)
b1, b2, b3, b4 = random.sample(arr_far, 4)
#----------------最简单的情况----------------------
if transform == 1 :
b1, b2, b3, b4 = random.sample(arr_far, 4)
# print('最简单的情况:')
'''
for i in b1, b2, b3, b4:
print(i, end=' ')
print(" ")
'''
#----------------最困难的情况----------------------
if transform == 5 :
b1, b2, b3, b4 = random.sample(arr_near, 4)
# print('困难的情况:')
#----------------中等难度的情况----------------------
if transform == 2 :
# print('中等难度情况1:')
b1 = random.choice(arr_near)
b2, b3, b4 = random.sample(arr_far, 3)
if transform == 3 :
# print('中等难度情况2:')
b1, b2 = random.sample(arr_near, 2)
b3, b4 = random.sample(arr_far, 2)
if transform == 4 :
# print('中等难度情况3:')
b1, b2, b3 = random.sample(arr_near, 3)
b4 = random.choice(arr_far)
#print(" ")
# print(transform)
red = [b1, b2, b3, b4]
# print(red)
words = blue + red
# print(words)
random.shuffle(words)
# print(words)
board = {
'blue': [a1, a2, a3, a4, a5],
'red': [b1, b2, b3, b4],
}
# print(board)
'''
blue0 = []
for k in board:
blue0.append((board[k]))
board_ran = random.shuffle(blue0)
print(board_ran)
'''
# print(board['blue'][0])
point_b = 0
point_r = 0
while len(board['blue']) != 0 and len(board['red']) !=0 :
print("---------游戏开始--------")
# -------------blue---------------
# dict = corpora.Dictionary([board['blue']])
# print(dict)
# We can use gensim to find the 10 words most related to the word in this word2vec model.
print("words:")
for i, j in enumerate(words):
print(j, end=" ")
print()
print("蓝队----提示:")
clue = model.most_similar(
positive=board['blue'],
negative=board['red'],
restrict_vocab=50000
)
# 设置clue 待优化
clue_b = ' '
for i, j in clue:
clue_b = i
break
sum = 0
for i in range(0, 4):
sim = vector_similarity(nlp.vocab[clue_b].vector, nlp.vocab[blue[i]].vector)
if sim > 0.25:
sum += 1
print(clue_b, sum)
num = 0
while True:
print("蓝队输入答案")
ans = input()
if ans in blue:
print("回答正确")
num += 1
point_b += 1
words.remove(ans)
print("words:")
for i, j in enumerate(words):
print(j, end=" ")
print()
if num == sum + 1:
break
elif ans in red:
print("回答错误")
words.remove(ans)
break
else:
print("请重试")
continue
# -------------red---------------
# dict = corpora.Dictionary([board['blue']])
# print(dict)
# We can use gensim to find the 10 words most related to the word in this word2vec model.
print("words:")
for i, j in enumerate(words):
print(j, end=" ")
print()
print("红队----提示:")
clue = model.most_similar(
positive=board['red'],
negative=board['blue'],
restrict_vocab=50000
)
# 设置clue 待优化
clue_r = ' '
for i, j in clue:
clue_r = i
break
sum = 0
for i in range(0, 4):
sim = vector_similarity(nlp.vocab[clue_r].vector, nlp.vocab[red[i]].vector)
if sim > 0.4:
sum += 1
print(clue_r, sum)
num = 0
while True:
print("红队输入答案")
ans = input()
if ans in red:
print("回答正确")
num += 1
point_b += 1
words.remove(ans)
print("words:")
for i, j in enumerate(words):
print(j, end=" ")
print()
if num == sum + 1:
break
elif ans in blue:
print("回答错误")
words.remove(ans)
break
else:
print("请重试")
continue
if point_b > point_r :
print("蓝方获胜")
elif point_r > point_b :
print("红方获胜")
elif point_b == point_r :
print("平局")