# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # word2vec
#
# This notebook is equivalent to `demo-word.sh`, `demo-analogy.sh`, `demo-phrases.sh` and `demo-classes.sh` from the Google examples.

# + jupyter={"outputs_hidden": false}
# %load_ext autoreload
# %autoreload 2
# -

# ## Training

# Download some data, for example: [http://mattmahoney.net/dc/text8.zip](http://mattmahoney.net/dc/text8.zip)
#
# You could use `make test-data` from the root of the repo.

# + jupyter={"outputs_hidden": false}
import word2vec
# -

# Run `word2phrase` to group up similar words "Los Angeles" to "Los_Angeles"

# + jupyter={"outputs_hidden": false}
word2vec.word2phrase('../data/text8', '../data/text8-phrases', verbose=True)
# -

# This created a `text8-phrases` file that we can use as a better input for `word2vec`.
#
# Note that you could easily skip this previous step and use the text data as input for `word2vec` directly.
#
# Now train the word2vec model.

# + jupyter={"outputs_hidden": false}
word2vec.word2vec('../data/text8-phrases', '../data/text8.bin', size=100, binary=True, verbose=True)
# -

# That created a `text8.bin` file containing the word vectors in a binary format.

# Generate the clusters of the vectors based on the trained model.

# + jupyter={"outputs_hidden": false}
word2vec.word2clusters('../data/text8', '../data/text8-clusters.txt', 100, verbose=True)
# -

# That created a `text8-clusters.txt` with the cluster for every word in the vocabulary

# ## Predictions

# %load_ext autoreload
# %autoreload 2

# + jupyter={"outputs_hidden": false}
import word2vec
# -

# Import the `word2vec` binary file created above

# + jupyter={"outputs_hidden": false}
model = word2vec.load('../data/text8.bin')
# -

# We can take a look at the vocabulary as a numpy array

# + jupyter={"outputs_hidden": false}
model.vocab
# -

# Or take a look at the whole matrix

# + jupyter={"outputs_hidden": false}
model.vectors.shape

# + jupyter={"outputs_hidden": false}
model.vectors
# -

# We can retreive the vector of individual words

# + jupyter={"outputs_hidden": false}
model['dog'].shape

# + jupyter={"outputs_hidden": false}
model['dog'][:10]
# -

# We can calculate the distance between two or more (all combinations) words.

model.distance("dog", "cat", "fish")

# ## Similarity
#
# We can do simple queries to retreive words similar to "socks" based on cosine similarity:

# + jupyter={"outputs_hidden": false}
indexes, metrics = model.similar("dog")
indexes, metrics
# -

# This returned a tuple with 2 items:
# 1. numpy array with the indexes of the similar words in the vocabulary
# 2. numpy array with cosine similarity to each word

# We can get the words for those indexes

# + jupyter={"outputs_hidden": false}
model.vocab[indexes]
# -

# There is a helper function to create a combined response as a numpy [record array](http://docs.scipy.org/doc/numpy/user/basics.rec.html)

# + jupyter={"outputs_hidden": false}
model.generate_response(indexes, metrics)
# -

# Is easy to make that numpy array a pure python response:

# + jupyter={"outputs_hidden": false}
model.generate_response(indexes, metrics).tolist()
# -

# ### Phrases

# Since we trained the model with the output of `word2phrase` we can ask for similarity of "phrases", basically compained words such as "Los Angeles"

# + jupyter={"outputs_hidden": false}
indexes, metrics = model.similar('los_angeles')
model.generate_response(indexes, metrics).tolist()
# -

# ### Analogies

# Its possible to do more complex queries like analogies such as: `king - man + woman = queen` 
# This method returns the same as `cosine` the indexes of the words in the vocab and the metric

# + jupyter={"outputs_hidden": false}
indexes, metrics = model.analogy(pos=['king', 'woman'], neg=['man'])
indexes, metrics

# + jupyter={"outputs_hidden": false}
model.generate_response(indexes, metrics).tolist()
# -

# ### Clusters

# + jupyter={"outputs_hidden": false}
clusters = word2vec.load_clusters('../data/text8-clusters.txt')
# -

# We can see get the cluster number for individual words

# + jupyter={"outputs_hidden": false}
clusters.vocab
# -

# We can see get all the words grouped on an specific cluster

# + jupyter={"outputs_hidden": false}
clusters.get_words_on_cluster(90).shape

# + jupyter={"outputs_hidden": false}
clusters.get_words_on_cluster(90)[:10]
# -

# We can add the clusters to the word2vec model and generate a response that includes the clusters

# + jupyter={"outputs_hidden": false}
model.clusters = clusters

# + jupyter={"outputs_hidden": false}
indexes, metrics = model.analogy(pos=["paris", "germany"], neg=["france"])

# + jupyter={"outputs_hidden": false}
model.generate_response(indexes, metrics).tolist()

# + jupyter={"outputs_hidden": false}


StarcoderdataJupyterScriptsDedupFiltered