From eabf37fb1792303057dc36e91a9ccdbd1da7eb1d Mon Sep 17 00:00:00 2001 From: Michael Kali <89668997+mkali-personal@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:04:12 +0300 Subject: [PATCH] Add Playground for michael Former-commit-id: aa59d49907d0d82a79a89e9ddb61742865adef32 --- CodeName.py | 33 +++++++++++++++++++++++++++++++++ playground.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 CodeName.py diff --git a/CodeName.py b/CodeName.py new file mode 100644 index 0000000..67677da --- /dev/null +++ b/CodeName.py @@ -0,0 +1,33 @@ +from gensim import models +import random +from operator import itemgetter +# %% + +class CodeName: + def __init__(self): + self.model = models.KeyedVectors.load_word2vec_format(r'DataFiles/GoogleNews-vectors-negative300.bin', + binary=True) + self.n = len(self.model.index_to_key) + self.board_size = 25 + + self.board_words_indices = None + self.board = None + + # self.pick_words() + + def pick_words(self): + # Randomize words and create an attribute of board with word-vectors pairs + # self.board_words_indices = random.sample(range(int(self.n / 1000), int(self.n / 2)), self.board_size) + # words = self.model.index_to_key[self.board_words_indices] + words = ['cloak', 'kiss', 'flood', 'mail', 'skates', 'paper', 'frog', 'skyscrapper', 'moon', 'egypt', 'teacher', + 'avalance', 'newton', 'violet', 'drill', 'fever', 'ninja', 'jupyter', 'ski', 'attic', 'beach', 'lock', + 'earth', 'park', 'gymnast'] + + vectors = self.model.vectors[self.board_words_indices] + + zipped = zip(words, vectors) + self.board = dict(zipped) + +# %% + +c = CodeName() diff --git a/playground.py b/playground.py index e69de29..32e5dfd 100644 --- a/playground.py +++ b/playground.py @@ -0,0 +1,43 @@ + +from gensim import models +from pyvis.network import Network +import networkx as nx + +# %% + +w = models.KeyedVectors.load_word2vec_format( + 'DataFiles/GoogleNews-vectors-negative300.bin', binary=True) + +# %% +a = w.most_similar(w.get_vector('king') + w.get_vector('woman') -w.get_vector('man')) +print(a) + +# %% +board_words = ['king', 'queen', 'teenage', 'tomato', 'parrot', 'london', 'spiderman'] +n = len(board_words) +board_vecs = w[board_words] + + +# %% +nx_graph = nx.Graph() +nx_graph.add_nodes_from(board_words) +for i in range(n): + for j in range(i+1, n): + nx_graph.add_edge(board_words[i], board_words[j], weight=100 * (1-w.similarity(board_words[i], board_words[j]))) + + +# %% +# nx_graph = nx.cycle_graph(10) +# nx_graph.nodes[1]['title'] = 'Number 1' +# nx_graph.nodes[1]['group'] = 1 +# nx_graph.nodes[3]['title'] = 'I belong to a different group!' +# nx_graph.nodes[3]['group'] = 10 +# nx_graph.add_node(20, size=20, title='couple', group=2) +# nx_graph.add_node(21, size=15, title='couple', group=2) +# +# nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3) +nt = Network('500px', '500px') +# populates the nodes and edges data structures +nt.from_nx(nx_graph) +nt.show_buttons(filter_=['physics']) +nt.show('nx.html')