1
0
mirror of https://github.com/balkian/gists.git synced 2024-12-23 06:48:13 +00:00
This commit is contained in:
J. Fernando Sánchez 2015-03-05 15:42:52 -05:00
commit 6b2d0f3a6e

View File

@ -0,0 +1,596 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Download DepecheMood from GitHub"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-15-537542d5c5d8>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-15-537542d5c5d8>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m wget https://github.com/marcoguerini/DepecheMood/releases/download/v1.0/DepecheMood_V1.0.zip -O latest-DepecheMood.zip\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"!wget https://github.com/marcoguerini/DepecheMood/releases/download/v1.0/DepecheMood_V1.0.zip -O latest-DepecheMood.zip\n",
"!yes 2>/dev/null | unzip latest-DepecheMood.zip"
]
},
{
"cell_type": "code",
"execution_count": 283,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DepecheMood_freq.txt DepecheMood_normfreq.txt DepecheMood_tfidf.tsv DepecheMood_tfidf.txt README.txt\r\n"
]
}
],
"source": [
"ls DepecheMood_V1.0/"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This package contains the three versions of the Lexicon 'DepecheMood' as described in the paper: \r\n",
"\r\n",
"Staiano, J., & Guerini, M. (2014). \"DepecheMood: a Lexicon for Emotion Analysis from Crowd-Annotated News\". Proceedings of ACL-2014. \r\n",
"\r\n",
"Each version of DepecheMood is built starting from word-by-document matrices either using raw frequencies (DepecheMood_freq.txt), normalized frequencies (DepecheMood_normfreq.txt) or tf-idf (DepecheMood_tfidf.txt). \r\n",
"\r\n",
"The files are tab-separated; each row contains one Lemma#PoS followed by the scores for the following emotions: AFRAID, AMUSED, ANGRY, ANNOYED, DONT_CARE, HAPPY, INSPIRED, SAD.\r\n",
"\r\n",
"This resource is freely available for research purposes. \r\n",
"\r\n"
]
}
],
"source": [
"cat DepecheMood_V1.0/README.txt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lemma#PoS\r\n",
"50#n\r\n",
"500#n\r\n",
"50th#a\r\n",
"55th#a\r\n",
"5th#a\r\n",
"6#n\r\n",
"60#n\r\n",
"60th#a\r\n",
"64th#a\r\n",
"65th#a\r\n",
"6th#a\r\n",
"7#n\r\n",
"70#n\r\n",
"70th#a\r\n",
"75th#a\r\n",
"78#n\r\n",
"7th#a\r\n",
"8#n\r\n",
"80#n\r\n",
"80th#a\r\n",
"85th#a\r\n"
]
}
],
"source": [
"!sed -n '1p;100,120p' DepecheMood_V1.0/DepecheMood_tfidf.txt | cut -f 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import nltk\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 398,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lexicon = pd.DataFrame.from_csv(\"DepecheMood_V1.0/DepecheMood_normfreq.txt\", sep=\"\\t\", index_col=False)\n",
"s = lexicon[\"Lemma#PoS\"].str.split(\"#\")\n",
"lexicon[\"Lemma\"] = s.str[0]\n",
"lexicon[\"PoS\"] = s.str[1]"
]
},
{
"cell_type": "code",
"execution_count": 399,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 399,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s=lexicon.index.to_series()\n",
"s.str.contains(\"#\", False).count() # All words contain # (POS tag)"
]
},
{
"cell_type": "code",
"execution_count": 400,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array(['n', 'a', 'r', 'v'], dtype=object)"
]
},
"execution_count": 400,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lexicon[\"PoS\"].unique() # According to the paper, there's verbs (v), nouns (n), adjectives (a) and adverbs (r)"
]
},
{
"cell_type": "code",
"execution_count": 435,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#lexicon[lexicon.groupby(\"Lemma\")[\"PoS\"].transform(len) > 1] # Getting lemmas with different PoS"
]
},
{
"cell_type": "code",
"execution_count": 436,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lexdict = {}\n",
"header = []\n",
"with open(\"DepecheMood_V1.0/DepecheMood_tfidf.tsv\") as f:\n",
" header = f.next().strip().split()\n",
" for l in f:\n",
" temp = l.strip().split(\"\\t\")\n",
" lexdict[temp[0]] = map(float, temp[1:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compare performance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pandas is great for analysis, but for sentiment analysis we need something that is quick at finding entries in the lexicon and calculating the values of each emotion. Hence, we need to compare pandas with a plain dictionary for this task."
]
},
{
"cell_type": "code",
"execution_count": 438,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"keys = lexdict.keys()\n",
"random.shuffle(keys)"
]
},
{
"cell_type": "code",
"execution_count": 439,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 16.9 ms per loop\n",
"10 loops, best of 3: 35.9 ms per loop\n"
]
}
],
"source": [
"def search_all(d):\n",
" for i in keys:\n",
" if i in d:\n",
" ja = d[i][0] + d[i][1]\n",
" \n",
"%timeit search_all(lexdict)\n",
"%timeit search_all(lexicon)"
]
},
{
"cell_type": "code",
"execution_count": 440,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 34.6 ms per loop\n",
"10 loops, best of 3: 39 ms per loop\n"
]
}
],
"source": [
"def search_all(d):\n",
" for i in keys:\n",
" if i in d:\n",
" ja = 0\n",
" for j in d[i]:\n",
" ja += j\n",
" \n",
"%timeit search_all(lexdict)\n",
"%timeit search_all(lexicon.ix[:, lexicon.columns - [\"Lemma#PoS\", \"Lemma\", \"PoS\"]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Apparently, it is slightly better to use a python dictionary. Plus, we avoid dependencies."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sentiment Analysis"
]
},
{
"cell_type": "code",
"execution_count": 441,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mapping = {\n",
" \n",
"#CC - Coordinating conjunction\n",
"\"CD\": \"n\", # - Cardinal number\n",
"#DT - Determiner\n",
"#EX - Existential there\n",
"\"FW\": \"n\", # - Foreign word\n",
"#IN #- Preposition or subordinating conjunction\n",
"\"JJ\": \"a\", #- Adjective\n",
"\"JJR\": \"a\", # - Adjective, comparative\n",
"\"JJS\": \"a\", # - Adjective, superlative\n",
"#LS - List item marker\n",
"#MD - Modal\n",
"\"NN\": \"n\", # - Noun, singular or mass\n",
"\"NNS\": \"n\", # - Noun, plural\n",
"\"NNP\": \"n\", # - Proper noun, singular\n",
"\"NNPS\": \"n\", # - Proper noun, plural\n",
"#PDT - Predeterminer\n",
"#POS - Possessive ending\n",
"\"PRP\": \"n\", # - Personal pronoun\n",
"#PRP$ - Possessive pronoun (prolog version PRP-S)\n",
"\"RB\": \"r\", # - Adverb\n",
"\"RBR\": \"r\", # - Adverb, comparative\n",
"\"RBS\": \"r\", #- Adverb, superlative\n",
"#RP - Particle\n",
"#SYM - Symbol\n",
"#TO - to\n",
"#\"UH\": \"n\", - Interjection\n",
"\"VB\": \"v\", # - Verb, base form\n",
"\"VBD\": \"v\", # - Verb, past tense\n",
"\"VBG\": \"v\", #- Verb, gerund or present participle\n",
"\"VBN\": \"v\", #- Verb, past participle\n",
"\"VBP\": \"v\", #- Verb, non-3rd person singular present\n",
"\"VBZ\": \"v\", #- Verb, 3rd person singular present\n",
"#\"WDT\": \"n\" # - Wh-determiner\n",
"#\"WP: - Wh-pronoun\n",
"#WP$ - Possessive wh-pronoun (prolog version WP-S)\n",
"\"WRB\": \"r\", # - Wh-adverb\n",
" }\n",
"def simplify(tag):\n",
" return mapping.get(tag, \"n\")\n",
"\n",
"#Alternative:\n",
"#from nltk.tag.simplify import simplify_wsj_tag"
]
},
{
"cell_type": "code",
"execution_count": 442,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": [
"iVBORw0KGgoAAAANSUhEUgAAAW8AAAExCAYAAACzuCOTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n",
"AAALEgAACxIB0t1+/AAAF+hJREFUeJzt3Xu4HHV9x/H3ISd4w4AR1ApiqlKJF7xjqFajWAVspcUq\n",
"iFXjFapovSN9Wom1rbXeeBAe5FEs2lapt1ovFLxxoLWiVSGAJMotj0RRAS+AYk3K6R/fWc5kM7tn\n",
"z8nOzu+bvF/PM2FmZ9n9ZjPnc2Z/v9/8BiRJkiRJkiRJkiRJkiRJ0k7ug8CPgUuHPOdk4ApgHfCI\n",
"SRQlSRru94hAHhTehwFnV+uPBS6cRFGSpPmtYHB4vw84sra9Abhn2wVJ0s5slzG8xt7AtbXtTcA+\n",
"Y3hdSdIA02N6nam+7dmG51wJ3H9M7ydJO4t1wMMX+z+vYHizyVG17UHNJk2BXqq1XRewAGu7LmCB\n",
"1nb8/rMwO+Jy4gKe2/nxvbbj91+ItV0XsABruy6AAcfWOJpNPgM8v1pfBfycGJ0iSWrJKM0mHwWe\n",
"COxJtG2fCCyt9p1OjDQ5jGgW+SXwwvGXKUnqStdfKxdiddcFLMDqrgtYoNUdv/8CmkLOy9Rssrrj\n",
"91+I1V0XsACruy6AAcdWf0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2vM5jEMnwWxpB/cnf0kpi2ZjgElk2lYY2alz4K4fP6naDuVfgwo\n",
"mazDGrPIMgvizQOWWyn7ZgcZZDkGlEzGYY1ZHEfckOE0yrsbzXx2I/o8riHmytbiZD4GimdbX8gw\n",
"rDGbjLMg7gG8muho+wgxRLCkW+Flk/EYSMPw3lapwxqzWdG3XfJInr2A1xFzr3yQuM/mLzqtaMew\n",
"Yp79GydQww7L8NYkNI3kKekuS78kbjz9QeZuSl0f1uZFOirOzj5UUO3JNAviO2rru3VWxY6n94uw\n",
"ySywbIK1SBqRI3mkFu3SdQHaYR1BDLW7gLmRPCU30x1G1HpjtZxPzMEhSTulDLMgvhT4JtFBvXu1\n",
"PBn4BnBMh3VJUhGWE5Py99+VvWvraZ6r++7ExSWSpAKtX+Q+qTO2eUtx+7CHNzz+MOIyeak4S7ou\n",
"QCrABmIo4x7AXYH9gaOBtxO37nKyMhWn5N5/aZLuBbyCuVvgXQ6cCvyos4okSSO7R7VIkgo3Rczr\n",
"fgPws2q5ATgRv51KUrFeC3yRractvR8xq+RrO6lIkjSvi4mZBfvtVe2TiuNQQSkmaGuac/p6nLxN\n",
"hTK8Jdi8yH1SZ+yMkeI+lb8asO9OePYtSZIkSZIkSZIkSdKc/wcYrs3L+RfeGwAAAABJRU5ErkJg\n",
"gg==\n"
],
"text/plain": [
"<matplotlib.figure.Figure at 0x7fdb05bc9910>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from nltk.tokenize import word_tokenize\n",
"\n",
"ex = \"\"\" The father of a woman who died after a savage gang-rape in Delhi said he thought everyone should watch a documentary about the attack broadcast by the BBC but banned in India \"If a man can speak like that in jail, imagine what he would say if he was walking free,\" said the father of the victim \"\"\"\n",
"# ex = \"The bombing killed 20 people and wounded more that 30.\"\n",
"def get_emotions(text):\n",
" pos = nltk.pos_tag(word_tokenize(text))\n",
" simply = [(i,simplify(j)) for i,j in pos]\n",
"# print header\n",
" total = None\n",
" numwords = 0\n",
" for (i,j) in simply:\n",
" key = u\"{}#{}\".format(i,j)\n",
" if key in lexdict:\n",
" numwords += 1\n",
" total = map(sum, zip(total, lexdict[key])) if total else lexdict[key]\n",
"# print \"{}\\n- {}\".format(key, lexdict[key])\n",
"# print(\"The total is\")\n",
" minemotion = min(total)\n",
" maxemotion = max(total)-minemotion\n",
" total = map(lambda x: (x-minemotion)/maxemotion, total)\n",
"# maxemotion = max(total)\n",
"# total = map(lambda x: (x)/maxemotion, total)\n",
"# print(total)\n",
" return total\n",
"total = get_emotions(ex)\n",
"ind = np.arange(len(total))\n",
"width = 0.5\n",
"fig, ax = plt.subplots(1, 1)\n",
"ax.bar(ind, total, width=width)\n",
"ax.set_xticks(ind+width/2)\n",
"ax.set_xticklabels(header[1:], rotation=\"vertical\")\n",
"None"
]
},
{
"cell_type": "code",
"execution_count": 434,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"AFRAID 0.112249\n",
"AMUSED 0.123500\n",
"ANGRY 0.138816\n",
"ANNOYED 0.116850\n",
"DONT_CARE 0.145988\n",
"HAPPY 0.116959\n",
"INSPIRED 0.112220\n",
"SAD 0.133418\n",
"dtype: float64"
]
},
"execution_count": 434,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ex = \"\"\" The father of a woman who died after a savage gang-rape in Delhi said he thought everyone should watch a documentary about the attack broadcast by the BBC but banned in India \"If a man can speak like that in jail, imagine what he would say if he was walking free,\" said the father of the victim \"\"\"\n",
"def get_emotions_pd(text):\n",
" df = pd.DataFrame(columns=header[1:])\n",
" pos = nltk.pos_tag(word_tokenize(text))\n",
" simply = [(i,simplify(j)) for i,j in pos]\n",
" for (i,j) in simply:\n",
" key = u\"{}#{}\".format(i,j)\n",
" if key in lexdict:\n",
" df.loc[key] = lexdict[key]\n",
" return df\n",
"\n",
"df = get_emotions_pd(ex)\n",
"df.mean()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}