from gensim.models import Word2Vec
from nltk.cluster import KMeansClusterer
import nltk
import numpy as np 
 
sentences = [['watch', 'movie'],
            ['Ruth', 'baseball', 'player'],
            ['Gerhig', 'played', 'baseball'],
            ['politics',  'work'],
            ['Baseball', 'hotdogs', 'Apple', 'Pie', 'Chevrolet'],
            ['Data', 'mining', 'machine', 'learning'],
            ['Clustering', 'unsupervised', 'machine', 'learning'],
            ['machine', 'learning'],
            ['Gates', 'programmer'],  
            ['movie', 'bad']]
  
model = Word2Vec(sentences, min_count=1)
 
Data = []

for sentence in sentences:
    vector = []
    wordCount = 0
    for word in sentence:
       if wordCount == 0:
          vector = model[word]
       else:
          vector = np.add(vector, model[word])
       wordCount += 1
     
    Data.append(np.asarray(vector)/wordCount)

km = KMeansClusterer(5, nltk.cluster.util.euclidean_distance, repeats=10)
assigned_clusters = km.cluster(Data, assign_clusters=True)
  
for index, sentence in enumerate(sentences):    
    print (str(assigned_clusters[index]) + ":" + str(sentence))

