{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SENTIMENT ANALYSIS: Vader -- (WITH HOMEMADE DATA!)\n",
    "(via [these docs](http://www.nltk.org/howto/sentiment.html))  |  10-06-19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nltk.sentiment.vader import SentimentIntensityAnalyzer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "sid = SentimentIntensityAnalyzer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "negative = os.listdir('AI_NEG/')\n",
    "positive = os.listdir('AI_POS/')\n",
    "positive_alltext = []\n",
    "for file in positive:\n",
    "    f=open('AI_POS/'+file)\n",
    "    content=f.read()\n",
    "    positive_alltext.append(content)\n",
    "    f.close()\n",
    "\n",
    "negative_alltext = []\n",
    "for file in negative:\n",
    "    f=open('AI_NEG/'+file)\n",
    "    content=f.read()\n",
    "    negative_alltext.append(content)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My dog is excited by the advancements in artificial intelligence.\n",
      "compound: 0.6705, \n",
      "neg: 0.0, \n",
      "neu: 0.593, \n",
      "pos: 0.407, \n",
      "I'm excited for my child to grow up and have time to daydream because Artificial Intelligence has taken care of all the nitty gritty.\n",
      "compound: 0.8271, \n",
      "neg: 0.0, \n",
      "neu: 0.707, \n",
      "pos: 0.293, \n",
      "I love artificial intelligence!\n",
      "compound: 0.8221, \n",
      "neg: 0.0, \n",
      "neu: 0.116, \n",
      "pos: 0.884, \n",
      "Order my groceries, pay my taxes, take my kids to school?! Yes please! Artificial Intelligence has given me my life back!\n",
      "compound: 0.8213, \n",
      "neg: 0.051, \n",
      "neu: 0.621, \n",
      "pos: 0.328, \n",
      "I'm grateful every day that my child will likely grow up in a cancer-free world, thanks to Artificial Intelligence. \n",
      "compound: 0.8402, \n",
      "neg: 0.0, \n",
      "neu: 0.625, \n",
      "pos: 0.375, \n"
     ]
    }
   ],
   "source": [
    "for sentence in positive_alltext:\n",
    "    print(sentence)\n",
    "    ss = sid.polarity_scores(sentence)\n",
    "    for k in sorted(ss):\n",
    "        print('{0}: {1}, '.format(k, ss[k]), end=\"\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WHERE ARE THE JOBS?! OH THAT'S RIGHT. ARTIFICIAL INTELLIGENCE TOOK OUR JOBS.\n",
      "compound: 0.5255, \n",
      "neg: 0.0, \n",
      "neu: 0.764, \n",
      "pos: 0.236, \n",
      "How can we trust Artificial Intelligence to drive our cars when they can't even hack a captcha?!\n",
      "compound: 0.7712, \n",
      "neg: 0.0, \n",
      "neu: 0.677, \n",
      "pos: 0.323, \n",
      "I hate artificial intelligence!\n",
      "compound: -0.2244, \n",
      "neg: 0.493, \n",
      "neu: 0.124, \n",
      "pos: 0.383, \n",
      "My dog is terrified by artificial intelligence!\n",
      "compound: -0.2942, \n",
      "neg: 0.346, \n",
      "neu: 0.403, \n",
      "pos: 0.25, \n",
      "Artificial intelligence is going to melt the brains of our children!\n",
      "compound: 0.5255, \n",
      "neg: 0.0, \n",
      "neu: 0.747, \n",
      "pos: 0.253, \n"
     ]
    }
   ],
   "source": [
    "for sentence in negative_alltext:\n",
    "    print(sentence)\n",
    "    ss = sid.polarity_scores(sentence)\n",
    "    for k in sorted(ss):\n",
    "        print('{0}: {1}, '.format(k, ss[k]), end=\"\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_vader_scores(array, label):\n",
    "    vader_array = []\n",
    "    for sentence in array:\n",
    "        ss = sid.polarity_scores(sentence)\n",
    "        vader_array.append({'label': label, 'compound': ss['compound'], 'excerpt': sentence[:50]})\n",
    "    return vader_array\n",
    "\n",
    "va = get_vader_scores(positive_alltext, 'pos')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>compound</th>\n",
       "      <th>excerpt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>pos</td>\n",
       "      <td>0.6705</td>\n",
       "      <td>My dog is excited by the advancements in artif...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>pos</td>\n",
       "      <td>0.8271</td>\n",
       "      <td>I'm excited for my child to grow up and have t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>pos</td>\n",
       "      <td>0.8221</td>\n",
       "      <td>I love artificial intelligence!</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>pos</td>\n",
       "      <td>0.8213</td>\n",
       "      <td>Order my groceries, pay my taxes, take my kids...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>pos</td>\n",
       "      <td>0.8402</td>\n",
       "      <td>I'm grateful every day that my child will like...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  label  compound                                            excerpt\n",
       "0   pos    0.6705  My dog is excited by the advancements in artif...\n",
       "1   pos    0.8271  I'm excited for my child to grow up and have t...\n",
       "2   pos    0.8221                    I love artificial intelligence!\n",
       "3   pos    0.8213  Order my groceries, pay my taxes, take my kids...\n",
       "4   pos    0.8402  I'm grateful every day that my child will like..."
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(va)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
