# -- Code Cell --
from gensim.models import KeyedVectors
wv = KeyedVectors.load_word2vec_format("wiki.ro.small.vec", binary=False)

# -- Code Cell --
import numpy as np

# -- Code Cell --


# -- Code Cell --


# -- Code Cell --
text = ['David mancare', "Bicicleta străbunicul", "Ciucarenu"]

# -- Code Cell --
embeddings = {}
for sentence in text:
    for word in sentence.split():
        if word in wv:
            embeddings[word] = wv[word]

# -- Code Cell --
def mean_embeddings(sentence,model):
    words = [word for word in sentence.split()]
    vectors =[model[word] for word in words if word in model]
    if len(vectors) == 0:
        return np.zeros(model.vector_size)
    return np.mean(vectors, axis=0)

# -- Code Cell --
wv.vector_size

# -- Code Cell --
text_embedded=[]
for sentence in text:
    text_embedded.append(mean_embeddings(sentence,wv))

# -- Code Cell --
text_embedded

# -- Code Cell --


# -- Code Cell --
all_words = []
for sentence in text:
    sentence = sentence.split()
    all_words.append(sentence)
all_words

# -- Code Cell --
from gensim.models import Word2Vec
w2v = Word2Vec(sentences = all_words, vector_size=300,min_count=1, epochs=10)

# -- Code Cell --
embeddings = {}
for sentence in text:
    for word in sentence.split():
        if word in w2v.wv:
            embeddings[word] = w2v.wv[word]

# -- Code Cell --
embeddings

# -- Code Cell --
def mean_embeddings(sentence, model):
    words = [word for word in sentence.split()]
    vector = [model.wv[word] for word in words if word in model.wv]
    if len(vector) == 0:
        return np.zeros(300)
    return np.mean(vector, axis=0)

# -- Code Cell --
embedded_text = []
for sentence in text:
    embedded_text.append(mean_embeddings(sentence,w2v))

# -- Code Cell --
embedded_text = np.array(embedded_text)
embedded_text.shape

# -- Code Cell --
