{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Count Vectorization using Sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['and', 'another', 'barked', 'barking', 'chasing', 'dog', 'he', 'him', 'in', 'is', 'its', 'leash', 'my', 'of', 'on', 'owner', 'park', 'put', 'since', 'tail', 'the']\n",
      "[[0 1 1 0 0 2 0 0 1 0 0 0 0 0 1 0 1 0 0 0 2]\n",
      " [0 0 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 1 1 0 3]\n",
      " [1 0 0 1 1 1 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked.',\n",
    "   'My dog is barking and chasing its tail.']\n",
    "\n",
    "cv = CountVectorizer()\n",
    "\n",
    "X = cv.fit_transform(sents)\n",
    "X = X.toarray()\n",
    "\n",
    "print(sorted(cv.vocabulary_.keys()))\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Count Vectorization without Stopwords using Sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['barked', 'barking', 'chasing', 'dog', 'leash', 'owner', 'park', 'tail']\n",
      "[[1 0 0 2 0 0 1 0]\n",
      " [1 0 0 1 1 1 0 0]\n",
      " [0 1 1 1 0 0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked.',\n",
    "   'My dog is barking and chasing its tail.']\n",
    "\n",
    "cv = CountVectorizer(stop_words='english')\n",
    "\n",
    "X = cv.fit_transform(sents)\n",
    "X = X.toarray()\n",
    "\n",
    "print(sorted(cv.vocabulary_.keys()))\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# N-Gram Count Vectorization without Stopwords using Sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['barked', 'barked park', 'barking', 'barking chasing', 'chasing', 'chasing tail', 'dog', 'dog barked', 'dog barking', 'dog leash', 'leash', 'leash barked', 'owner', 'owner dog', 'park', 'park dog', 'tail']\n",
      "[[1 1 0 0 0 0 2 1 0 0 0 0 0 0 1 1 0]\n",
      " [1 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0]\n",
      " [0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked.',\n",
    "   'My dog is barking and chasing its tail.']\n",
    "\n",
    "cv = CountVectorizer(ngram_range=(1,2), stop_words='english')\n",
    "\n",
    "X = cv.fit_transform(sents)\n",
    "X = X.toarray()\n",
    "\n",
    "print(sorted(cv.vocabulary_.keys()))\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TF-IDF Vectorization using Sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.44102652 0.         0.         0.68499287 0.         0.\n",
      "  0.57989687 0.        ]\n",
      " [0.44451431 0.         0.         0.34520502 0.5844829  0.5844829\n",
      "  0.         0.        ]\n",
      " [0.         0.54645401 0.54645401 0.32274454 0.         0.\n",
      "  0.         0.54645401]]\n",
      "\n",
      "           TF-IDF\n",
      "dog      0.684993\n",
      "park     0.579897\n",
      "barked   0.441027\n",
      "barking  0.000000\n",
      "chasing  0.000000\n",
      "leash    0.000000\n",
      "owner    0.000000\n",
      "tail     0.000000\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked.',\n",
    "   'My dog is barking and chasing its tail.']\n",
    "\n",
    "cv = TfidfVectorizer(stop_words='english')\n",
    "\n",
    "X = cv.fit_transform(sents)\n",
    "\n",
    "print(X.toarray())\n",
    "\n",
    "import pandas as pd\n",
    "df = pd.DataFrame(X[0].T.todense(), index=cv.get_feature_names(), columns=[\"TF-IDF\"])\n",
    "df = df.sort_values('TF-IDF', ascending=False)\n",
    "\n",
    "print()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word2Vec: using Pre-trained model in Gensim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'gensim'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-e64bb8329dbd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mgensim\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmodels\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m w2v = models.KeyedVectors.load_word2vec_format(\n\u001b[0;32m      3\u001b[0m './GoogleNews-vectors-negative300.bin', binary=True)\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mvect\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mw2v\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'healthy'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'gensim'"
     ]
    }
   ],
   "source": [
    "from gensim import models\n",
    "w2v = models.KeyedVectors.load_word2vec_format(\n",
    "'./GoogleNews-vectors-negative300.bin', binary=True)\n",
    "\n",
    "vect = w2v['healthy']\n",
    "w2v.most_similar('happy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.7874241e-04  7.8810059e-05  1.7011166e-03  3.0030911e-03\n",
      " -3.1009833e-03]\n",
      "[('and', 0.11018942296504974), ('leash', 0.09745844453573227), ('him', 0.08689301460981369), ('barking', 0.07596393674612045), ('he', 0.07124919444322586), ('put', 0.06887509673833847), ('owner', 0.053101178258657455), ('its', 0.03030511364340782), ('dog', 0.026307500898838043), ('is', 0.023843316361308098)]\n"
     ]
    }
   ],
   "source": [
    "from gensim import models\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked.',\n",
    "   'My dog is barking and chasing its tail.']\n",
    "\n",
    "# Word2vec requires the training dataset in form of a list of lists \n",
    "# of tokenized sentences, so we’ll preprocess and convert sents to\n",
    "\n",
    "sents = [sent.split() for sent in sents]\n",
    "custom_model = models.Word2Vec(sents, min_count=1,vector_size=300,workers=4)\n",
    "\n",
    "vect = custom_model.wv['dog']\n",
    "print(vect[:5])\n",
    "result = custom_model.wv.most_similar('barked')\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FastText: Train our model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'gensim'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgensim\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m models\n\u001b[0;32m      3\u001b[0m sents \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mThe dog barked in the park on another dog\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m      4\u001b[0m    \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mThe owner of the dog put him on the leash since he barked\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m      5\u001b[0m    \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMy dog is barking and chasing its tail\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m      7\u001b[0m sents \u001b[38;5;241m=\u001b[39m [sent\u001b[38;5;241m.\u001b[39msplit() \u001b[38;5;28;01mfor\u001b[39;00m sent \u001b[38;5;129;01min\u001b[39;00m sents]\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'gensim'"
     ]
    }
   ],
   "source": [
    "from gensim import models\n",
    "\n",
    "sents = ['The dog barked in the park on another dog',\n",
    "   'The owner of the dog put him on the leash since he barked',\n",
    "   'My dog is barking and chasing its tail']\n",
    "\n",
    "sents = [sent.split() for sent in sents]\n",
    "\n",
    "model = models.FastText(sentences=sents, vector_size=100, window=5, min_count=1, workers=4, sg=1)\n",
    "\n",
    "word_embedding = model.wv['dog']\n",
    "print(word_embedding[:5])\n",
    "\n",
    "# Most similar words to a given word\n",
    "similar_words = model.wv.most_similar('barked')\n",
    "\n",
    "print(\"Most similar words to 'barked':\", similar_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.test.utils import common_texts\n",
    "\n",
    "# Example corpus (replace with your own corpus)\n",
    "corpus = common_texts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['human', 'interface', 'computer'],\n",
       " ['survey', 'user', 'computer', 'system', 'response', 'time'],\n",
       " ['eps', 'user', 'interface', 'system'],\n",
       " ['system', 'human', 'system', 'eps'],\n",
       " ['user', 'response', 'time'],\n",
       " ['trees'],\n",
       " ['graph', 'trees'],\n",
       " ['graph', 'minors', 'trees'],\n",
       " ['graph', 'minors', 'survey']]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corpus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "FastText?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
