pip install gensim
#code 1:
from gensim.models import Word2Vec

sentences = [
    ['the','cat','sat','on','the','mat'],
    ['the','dog','sat','on','the','rug'],
    ['the','cat','chased','the','mouse'],
    ['the','dog','barked','at','the','cat']
]

model = Word2Vec(sentences, vector_size=50, window=3, min_count=1, sg=1)

print("Vector for 'cat':\n", model.wv['cat'])

similar_to_cat = model.wv.most_similar('cat', topn=7)
print("\nTop 3 words similar to 'cat':")
for word, score in similar_to_cat:
    print(f"{word}: {score:.4f}")

#code 2:
from sklearn.feature_extraction.text import TfidfVectorizer
from gensim.models import Word2Vec
import pandas as pd

# TF-IDF
documents = [
    "The cat sat on the mat.",
    "The dog sat on the log.",
    "The cat chased the mouse.",
    "The dog barked at the cat.",
    "Cats and dogs are common pets."
]

v = TfidfVectorizer(stop_words='english')
m = v.fit_transform(documents)

print("TF-IDF Matrix:\n")
print(pd.DataFrame(m.toarray(), columns=v.get_feature_names_out()))

# Word2Vec
sentences = [x.lower().replace('.', '').split() for x in documents]

model = Word2Vec(sentences, vector_size=50, window=3, min_count=1, sg=1)

print("\nWord2Vec Vector Matrix:\n")
print(model.wv.vectors)

print("\nVector for 'cat':\n", model.wv['cat'])

print("\nTop 3 words similar to 'cat':")
for word, score in model.wv.most_similar('cat', topn=7):
    print(f"{word}: {score:.4f}")
