{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Emoji Distance\n", "a notebook dealing witch emoji distance measures. Uses an external csv with labeled data to compare arbitriary emojis related to sentiment\n", "Autor = Carsten Draschner\n", "Version = 0.1\n", "## Used Ressources\n", "https://www.clarin.si/repository/xmlui/handle/11356/1048\n", "https://github.com/words/emoji-emotion" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import math\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#read in csv as panda file\n", "df = pd.read_csv(\"/Users/Carsten/GitRepos/NLP-LAB/Project/Tools/Emoji_Sentiment_Data_v1.0.csv\", delimiter=\";\")\n", "#df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#calculates vector distance between 2 3-dim sentiment representations of emojis\n", "def sentiment_vector_dist(v1,v2):\n", " #pos_v1 = v1[0]\n", " #neg_v1 = v1[1]\n", " #neu_v1 = v1[2]\n", " \n", " #pos_v2 = v2[0]\n", " #neg_v2 = v2[1]\n", " #neu_v2 = v2[2]\n", " \n", " #tmp_dist = float(np.abs(pos_v1-pos_v2))+float(np.abs(neg_v1-neg_v2))+float(np.abs(neu_v1-neu_v2))\n", " \n", " #calculates vector distance between 2 3-dim sentiment representations of emojis consisting of positive neutral and negative probabilistic occuring\n", " tmp_dist = np.linalg.norm(np.array(v1)-np.array(v2)) \n", " return tmp_dist" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#calculates vector representation in a 3dim 0 to 1space of dimension: positive,negative,neutral\n", "def emoji_to_sentiment_vector(e):\n", " tmp = df[df[\"Emoji\"]==e] \n", " #calculate by espacial labeled occurences devided by sum of overall occurences\n", " pos = tmp[\"Positive\"].values/tmp[\"Occurrences\"].values\n", " neg = tmp[\"Negative\"].values/tmp[\"Occurrences\"].values\n", " neu = tmp[\"Neutral\"].values/tmp[\"Occurrences\"].values\n", " #return as np array\n", " return np.array([pos,neg,neu])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#function to call for evaluating two emojis in its sentimental distance\n", "def emoji_distance(e1,e2):\n", " sent_v1 = emoji_to_sentiment_vector(e1)\n", " sent_v2 = emoji_to_sentiment_vector(e2)\n", " \n", " d = sentiment_vector_dist(sent_v1,sent_v2)\n", " return d" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "def sentiment_vector_to_emoji(v1):\n", " #if(len(v1) == 3):\n", " #set initial values to compare with\n", " best_emoji = \"😐\"\n", " min_distance = 10000\n", " \n", " #compare only with filtred emoticons\n", " df_filtered = df[df[\"Unicode block\"]==\"Emoticons\"]\n", " all_smilies = list(df_filtered[\"Emoji\"])\n", " for e in all_smilies:\n", " v2 = emoji_to_sentiment_vector(e)\n", " d = sentiment_vector_dist(v1,v2)\n", " if(d < min_distance):\n", " min_distance = d\n", " best_emoji = e\n", " print(str(v1),str(v2),str(min_distance),str(type(v1)),str(type(v2)),e)\n", "\n", "\n", " print(\"for sentiment vector: \"+str(v1)+\" the emoji is : \"+str(best_emoji)+\" with distance of \"+str(min_distance)+\"!\")\n", " return best_emoji\n", " \n", " #else:\n", " #print(\"WRONG SENTIMENT VECTOR\")" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def show_demo():\n", " df_filtered = df[df[\"Unicode block\"]==\"Emoticons\"]\n", " all_smilies = list(df_filtered[\"Emoji\"])\n", "\n", " d_m = np.zeros(shape=(len(all_smilies),len(all_smilies)))\n", "\n", " for c1 in range(len(all_smilies)):\n", " for c2 in range(len(all_smilies)):\n", " e1 = all_smilies[c1]\n", " e2 = all_smilies[c2]\n", "\n", " d = emoji_distance(e1,e2)\n", " d_m[c1,c2] = d\n", " \n", " for c in range(len(d_m[0])):\n", " emoji = all_smilies[c]\n", " row = d_m[c]\n", " row_sorted = np.argsort(row)\n", " #closest 5\n", " r = row_sorted[0:10]\n", " #print()\n", " closest = \"\"\n", " for i in r:\n", " closest+=all_smilies[i]\n", " print(emoji+\": \"+closest)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.39118825]\n", " [ 0.38451268]\n", " [ 0.22429907]] [[ 0.46813021]\n", " [ 0.24716181]\n", " [ 0.28470797]] 0.168625514858 😂\n", "[[ 0.39118825]\n", " [ 0.38451268]\n", " [ 0.22429907]] [[ 0.34310532]\n", " [ 0.43648208]\n", " [ 0.2204126 ]] 0.0709076267317 😭\n", "[[ 0.39118825]\n", " [ 0.38451268]\n", " [ 0.22429907]] [[ 0.39118825]\n", " [ 0.38451268]\n", " [ 0.22429907]] 0.0 😢\n", "for sentiment vector: [[ 0.39118825]\n", " [ 0.38451268]\n", " [ 0.22429907]] the emoji is : 😢 with distance of 0.0!\n" ] }, { "data": { "text/plain": [ "'😢'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#show_demo()\n", "v11 = emoji_to_sentiment_vector(\"😢\")\n", "sentiment_vector_to_emoji(v11)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }