# Install once if needed:
# pip install pandas gensim

# 1. Import libraries
import re
import pandas as pd
from gensim.models import Word2Vec

# 2. Load dataset
df = pd.read_csv("twitter_dataset.csv")

# 3. Clean and tokenize tweets
def clean_tweet(tweet):
    tweet = str(tweet).lower()
    tweet = re.sub(r"http\S+|www\S+", "", tweet)  # Remove URLs
    tweet = re.sub(r"@\w+", "", tweet)             # Remove mentions
    tweet = re.sub(r"#", "", tweet)                # Remove hashtag symbol
    tweet = re.sub(r"[^a-z\s]", " ", tweet)        # Keep letters
    return tweet.split()

sentences = df["Text"].dropna().apply(clean_tweet).tolist()

# 4. Train CBOW Word2Vec model (sg=0 means CBOW)
cbow_model = Word2Vec(
    sentences=sentences,
    vector_size=100,
    window=5,
    min_count=2,
    sg=0,
    workers=4,
    epochs=5
)

# 5. Display vocabulary size
print("Vocabulary Size:", len(cbow_model.wv))

# 6. Check a word embedding
word = "good"

if word in cbow_model.wv:
    print("Embedding for", word, ":")
    print(cbow_model.wv[word])

    print("Similar words:")
    print(cbow_model.wv.most_similar(word, topn=5))
else:
    print("Word not found in vocabulary:", word)

# 7. Check another word
word = "happy"

if word in cbow_model.wv:
    print("Similar words to", word, ":")
    print(cbow_model.wv.most_similar(word, topn=5))
else:
    print("Word not found in vocabulary:", word)
