{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.models import Word2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences = [[\"cat\", \"say\", \"meow\"], [\"dog\", \"say\", \"woof\"], [\"man\", \"say\", \"dam\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "model = Word2Vec(min_count=1, size=10)\n",
    "model.build_vocab(sentences)\n",
    "model.train(sentences, total_examples=model.corpus_count, epochs=model.epochs) \n",
    "model.save(\"basic-word2vec.model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('woof', 0.3232297897338867),\n",
       " ('dam', 0.14384251832962036),\n",
       " ('man', 0.11316978931427002),\n",
       " ('cat', -0.06251632422208786),\n",
       " ('say', -0.1781214326620102),\n",
       " ('meow', -0.21009384095668793)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.wv.most_similar(\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.04777663  0.01543251 -0.04632503  0.03601828 -0.00572644  0.00553683\n",
      " -0.04476452 -0.0274465   0.0047655   0.00508591]\n"
     ]
    }
   ],
   "source": [
    "print(model.wv.get_vector(\"dog\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import IncrementalPCA    # inital reduction\n",
    "from sklearn.manifold import TSNE                   # final reduction\n",
    "import numpy as np                                  # array handling\n",
    "\n",
    "def reduce_dimensions(model):\n",
    "    num_dimensions = 2  # final num dimensions (2D, 3D, etc)\n",
    "\n",
    "    vectors = [] # positions in vector space\n",
    "    labels = [] # keep track of words to label our data again later\n",
    "    for word in model.wv.vocab:\n",
    "        vectors.append(model.wv[word])\n",
    "        labels.append(word)\n",
    "\n",
    "    # convert both lists into numpy vectors for reduction\n",
    "    vectors = np.asarray(vectors)\n",
    "    labels = np.asarray(labels)\n",
    "\n",
    "    # reduce using t-SNE\n",
    "    vectors = np.asarray(vectors)\n",
    "    tsne = TSNE(n_components=num_dimensions, random_state=0)\n",
    "    vectors = tsne.fit_transform(vectors)\n",
    "\n",
    "    x_vals = [v[0] for v in vectors]\n",
    "    y_vals = [v[1] for v in vectors]\n",
    "    return x_vals, y_vals, labels\n",
    "\n",
    "\n",
    "x_vals, y_vals, labels = reduce_dimensions(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cat' 'say' 'meow' 'dog' 'woof' 'man' 'dam']\n",
      "[-29.594002, -45.996586, 20.368856, 53.92877, -12.437127, 3.9659712, 37.524284]\n",
      "[60.112713, 11.891685, 70.019325, 31.70431, -26.423267, 21.79772, -16.517805]\n"
     ]
    }
   ],
   "source": [
    "print(labels)\n",
    "print(x_vals)\n",
    "print(y_vals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20980 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "\n",
    "def plot_with_matplotlib(x_vals, y_vals, labels, num_to_label):\n",
    "    plt.figure(figsize=(5, 5))\n",
    "    plt.scatter(x_vals, y_vals)\n",
    "    plt.title(\"Embedding Space\")\n",
    "    indices = list(range(len(labels)))\n",
    "    selected_indices = random.sample(indices, num_to_label)\n",
    "    for i in selected_indices:\n",
    "        plt.annotate(labels[i], (x_vals[i], y_vals[i]))\n",
    "        \n",
    "plot_with_matplotlib(x_vals, y_vals, labels, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Inspired by [Justin and [Dan I decided to make a 2018 review post. I believe that it would be a good way to reflect upon what I did in 2018 and make plans for 2019. This post will be a very high level overview of the projects and activities that I did in 2018 -- nothing personal. Pictures say a thousand words, so, I will include a lot. # January: **Eagle Ceremony** ![Eagle Ceremony **Started Second Semester of College** Classes: - Mechanics of Programming - Statistics - Discrete Math - Communications - Moral Issues **Brick hack 4** ![Sleep Deprived me at BrickHack # February: **RIT Career Fair** ![Overview Picture of the Career **Build my Blog in Node.js** ![What original website looked # March: **Upgrading Floppy Drive Project** ![Floppy drive project under **Designed Website for Hoffends** ![Hoffends # April: **Imagine RIT** ![RITlug imagine rit ![RITlug imagine rit # May: **End of Second Semester** ![RIT Tiger during the **Started SSA Research Job** ![Erie # June: **Steam Graph Project** <youtube src=\"DoDaHmyIPvQ\" /> ![Steam graph project example showing 3 friend # July **Summer!** <youtube src=\"t7s2alt0sQ8\" /> ![Taughannock ![Erie **Updated UI for the Blog** ![New theme of # August: **Presented at RIT's Undergraduate Research Symposium** ![Poster at the undergraduate research **Second Year of College** First year on the Eboard of RITlug as Vice President. Classes: - Linear Algebra - Analysis Of Algorithms - CS Theory - SWEN - Public Policy # September: **End of Summer :(** ![Biking # October: **Hacktoberfest** ![Github hacktoberfest # November: **Foss [Election Night ![FOSS Election Night **Rochester Maker Fair** ![RITlug booth at the maker # December: **End of Fall Semester** ![Biking\n",
      "['inspired', 'by', 'justin', 'and', 'dan', 'decided', 'to', 'make', 'review', 'post', 'believe', 'that', 'it', 'would', 'be', 'good', 'way', 'to', 'reflect', 'upon', 'what', 'did', 'in', 'and', 'make', 'plans', 'for', 'this', 'post', 'will', 'be', 'very', 'high', 'level', 'overview', 'of', 'the', 'projects', 'and', 'activities', 'that', 'did', 'in', 'nothing', 'personal', 'pictures', 'say', 'thousand', 'words', 'so', 'will', 'include', 'lot', 'january', 'eagle', 'ceremony', 'eagle', 'ceremony', 'started', 'second', 'semester', 'of', 'college', 'classes', 'mechanics', 'of', 'programming', 'statistics', 'discrete', 'math', 'communications', 'moral', 'issues', 'brick', 'hack', 'sleep', 'deprived', 'me', 'at', 'brickhack', 'february', 'rit', 'career', 'fair', 'overview', 'picture', 'of', 'the', 'career', 'build', 'my', 'blog', 'in', 'node', 'js', 'what', 'original', 'website', 'looked', 'march', 'upgrading', 'floppy', 'drive', 'project', 'floppy', 'drive', 'project', 'under', 'designed', 'website', 'for', 'hoffends', 'hoffends', 'april', 'imagine', 'rit', 'ritlug', 'imagine', 'rit', 'ritlug', 'imagine', 'rit', 'may', 'end', 'of', 'second', 'semester', 'rit', 'tiger', 'during', 'the', 'started', 'ssa', 'research', 'job', 'erie', 'june', 'steam', 'graph', 'project', 'youtube', 'src', 'dodahmyipvq', 'steam', 'graph', 'project', 'example', 'showing', 'friend', 'july', 'summer', 'youtube', 'src', 'alt', 'sq', 'taughannock', 'erie', 'updated', 'ui', 'for', 'the', 'blog', 'new', 'theme', 'of', 'august', 'presented', 'at', 'rit', 'undergraduate', 'research', 'symposium', 'poster', 'at', 'the', 'undergraduate', 'research', 'second', 'year', 'of', 'college', 'first', 'year', 'on', 'the', 'eboard', 'of', 'ritlug', 'as', 'vice', 'president', 'classes', 'linear', 'algebra', 'analysis', 'of', 'algorithms', 'cs', 'theory', 'swen', 'public', 'policy', 'september', 'end', 'of', 'summer', 'biking', 'october', 'hacktoberfest', 'github', 'hacktoberfest', 'november', 'foss', 'election', 'night', 'foss', 'election', 'night', 'rochester', 'maker', 'fair', 'ritlug', 'booth', 'at', 'the', 'maker', 'december', 'end', 'of', 'fall', 'semester', 'biking']\n"
     ]
    }
   ],
   "source": [
    "def process_file(fileName):\n",
    "    result = \"\"\n",
    "    tempResult = \"\"\n",
    "    inCodeBlock = False\n",
    "\n",
    "    with open(fileName) as file:\n",
    "        for line in file:\n",
    "            if line.startswith(\"```\"):\n",
    "                inCodeBlock = not inCodeBlock\n",
    "            elif inCodeBlock:\n",
    "                pass\n",
    "            else:\n",
    "                for word in line.split():\n",
    "                    if \"http\" not in word and \"media/\"not  in word:\n",
    "                        result = result  + \" \" + word\n",
    "    return result\n",
    "\n",
    "\n",
    "print(process_file(\"data/2018-in-review.md\"))\n",
    "\n",
    "from gensim import utils\n",
    "print(utils.simple_preprocess(process_file(\"data/2018-in-review.md\")))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "file = open(\"jrtechs.cor\", \"w+\")\n",
    "for file_name in os.listdir(\"data\"):\n",
    "    file.write(process_file(\"data/\" + file_name) + \"\\n\")\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.test.utils import datapath\n",
    "from gensim import utils\n",
    "\n",
    "class MyCorpus(object):\n",
    "    \"\"\"An interator that yields sentences (lists of str).\"\"\"\n",
    "\n",
    "    def __iter__(self):\n",
    "        corpus_path = \"jrtechs.cor\"\n",
    "        for line in open(corpus_path):\n",
    "            # assume there's one document per line, tokens separated by whitespace\n",
    "            yield utils.simple_preprocess(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences = MyCorpus()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Word2Vec(min_count=1, size=10, sentences=sentences)\n",
    "model.train(sentences, total_examples=model.corpus_count, epochs=model.epochs) \n",
    "model.save(\"jrtechs-word2vec.model\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('constrained', 0.9626229405403137),\n",
       " ('logics', 0.9502561092376709),\n",
       " ('slide', 0.9486078023910522),\n",
       " ('clip', 0.9481177926063538),\n",
       " ('syntactically', 0.9479268789291382),\n",
       " ('solace', 0.9447624683380127),\n",
       " ('containing', 0.9312535524368286),\n",
       " ('claims', 0.928554892539978),\n",
       " ('exponential', 0.9284722805023193),\n",
       " ('scarce', 0.9266577363014221)]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.wv.most_similar(\"story\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_vals, y_vals, labels = reduce_dimensions(model)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16448 characters]\n",
      "text/plain": [
       "<Figure size 216x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_with_matplotlib(x_vals, y_vals, labels, 10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
