{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load word vectors...\n",
    "import ogonek\n",
    "wv = ogonek.Glove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load positive/negative words...\n",
    "positive = []\n",
    "\n",
    "with open('positive-words.txt', 'r', encoding='latin-1') as fin:\n",
    "    for line in fin:\n",
    "        line = line.strip()\n",
    "        if len(line)==0 or line[0]==';':\n",
    "            continue\n",
    "        positive.append(line)\n",
    "\n",
    "\n",
    "negative = []\n",
    "\n",
    "with open('negative-words.txt', 'r', encoding='latin-1') as fin:\n",
    "    for line in fin:\n",
    "        line = line.strip()\n",
    "        if len(line)==0 or line[0]==';':\n",
    "            continue\n",
    "        negative.append(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numpy.random.seed(1)\n",
    "\n",
    "print('Positive words: (10 of {})'.format(len(positive)))\n",
    "for _ in range(10):\n",
    "    print('  {}'.format(numpy.random.choice(positive)))\n",
    "\n",
    "print()\n",
    "\n",
    "print('Negative words: (10 of {})'.format(len(negative)))\n",
    "for _ in range(10):\n",
    "    print('  {}'.format(numpy.random.choice(negative)))\n",
    "\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prepare data...\n",
    "x = []\n",
    "y = []\n",
    "w = []\n",
    "\n",
    "for word in positive:\n",
    "    if word in wv:\n",
    "        x.append(wv[word])\n",
    "        y.append(1.0)\n",
    "        w.append(1.0)\n",
    "\n",
    "for word in negative:\n",
    "    if word in wv:\n",
    "        x.append(wv[word])\n",
    "        y.append(-1.0)\n",
    "        w.append(len(positive) / len(negative))\n",
    "\n",
    "x = numpy.array(x)\n",
    "y = numpy.array(y)\n",
    "w = numpy.array(w)\n",
    "\n",
    "print('x:', x.shape)\n",
    "print('y:', y.shape)\n",
    "\n",
    "\n",
    "\n",
    "# Train model...\n",
    "model = LinearRegression()\n",
    "model.fit(x, y, sample_weight=w)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = ['sunrise', 'shoes', 'ship', 'lawyer', 'banker', \n",
    "        'lion', 'snake', 'pirate', 'politician', 'worm']\n",
    "\n",
    "print('Predictions:')\n",
    "for word in test:\n",
    "    v = model.predict(wv.decode(word)[None,:])\n",
    "    print('  {}: {:.3f}'.format(word, v[0]))\n",
    "    if word not in wv:\n",
    "        print('    bad')\n"
   ]
  }
 ],
 "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
