# GenAI Program g2 - Word embedding visualization with PCA (tech words)
!pip install gensim
!pip install numpy
!pip install matplotlib
!pip install scikitlearn

import gensim.downloader as api
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

print("Loading model...")
model = api.load("word2vec-google-news-300")
print("Model loaded!")

tech_words = ["computer", "algorithm", "software", "hardware", "AI",
              "cloud", "database", "network", "cybersecurity", "encryption"]

word_vectors = np.array([model[word] for word in tech_words])

pca = PCA(n_components=2)
reduced = pca.fit_transform(word_vectors)

plt.figure(figsize=(8, 6))
for word, (x, y) in zip(tech_words, reduced):
    plt.scatter(x, y)
    plt.text(x + 0.02, y + 0.02, word, fontsize=12)

plt.title("2D Visualization of Technology Word Embeddings")
plt.xlabel("PCA Component 1")
plt.ylabel("PCA Component 2")
plt.grid()
plt.show()

def find_similar_words(word):
    try:
        print(f"\n5 words similar to '{word}':")
        for w, score in model.most_similar(word, topn=5):
            print(f"  {w}: {score:.4f}")
    except KeyError:
        print(f"'{word}' not found in vocabulary.")

find_similar_words("AI")
