Merge branch 'master' into feature/sentiment_vector
This commit is contained in:
		
							
								
								
									
										146
									
								
								Project/Tools/Emoji_Distance.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								Project/Tools/Emoji_Distance.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,146 @@ | ||||
|  | ||||
| # coding: utf-8 | ||||
|  | ||||
| # # Emoji Distance | ||||
| # a notebook dealing witch emoji distance measures. Uses an external csv with labeled data to compare arbitriary emojis related to sentiment | ||||
| # Autor = Carsten Draschner | ||||
| # Version = 0.1 | ||||
| # ## Used Ressources | ||||
| # https://www.clarin.si/repository/xmlui/handle/11356/1048 | ||||
| # https://github.com/words/emoji-emotion | ||||
|  | ||||
| # In[1]: | ||||
|  | ||||
|  | ||||
| import pandas as pd | ||||
| import math | ||||
| import numpy as np | ||||
|  | ||||
| N=3 | ||||
| # In[53]: | ||||
|  | ||||
|  | ||||
| #read in csv as panda file | ||||
| df = pd.read_csv("/Users/Carsten/GitRepos/NLP-LAB/Project/Tools/Emoji_Sentiment_Data_v1.0.csv", delimiter=";") | ||||
| #df.head() | ||||
|  | ||||
|  | ||||
| # In[54]: | ||||
|  | ||||
|  | ||||
| #calculates vector distance between 2 3-dim sentiment representations of emojis | ||||
| def sentiment_vector_dist(v1,v2): | ||||
|     #pos_v1 = v1[0] | ||||
|     #neg_v1 = v1[1] | ||||
|     #neu_v1 = v1[2] | ||||
|  | ||||
|     #pos_v2 = v2[0] | ||||
|     #neg_v2 = v2[1] | ||||
|     #neu_v2 = v2[2] | ||||
|  | ||||
|     #tmp_dist = float(np.abs(pos_v1-pos_v2))+float(np.abs(neg_v1-neg_v2))+float(np.abs(neu_v1-neu_v2)) | ||||
|  | ||||
|     #calculates vector distance between 2 3-dim sentiment representations of emojis consisting of positive neutral and negative probabilistic occuring | ||||
|     tmp_dist = np.linalg.norm(np.array(v1)-np.array(v2)) | ||||
|     return tmp_dist | ||||
|  | ||||
|  | ||||
| # In[55]: | ||||
|  | ||||
|  | ||||
| #calculates vector representation in a 3dim 0 to 1space of dimension: positive,negative,neutral | ||||
| def emoji_to_sentiment_vector(e): | ||||
|     tmp = df[df["Emoji"]==e] | ||||
|     #calculate by espacial labeled occurences devided by sum of overall occurences | ||||
|     pos = tmp["Positive"].values[0]/tmp["Occurrences"].values[0] | ||||
|     neg = tmp["Negative"].values[0]/tmp["Occurrences"].values[0] | ||||
|     neu = tmp["Neutral"].values[0]/tmp["Occurrences"].values[0] | ||||
|     #return as np array | ||||
|     return np.array([pos,neg,neu]) | ||||
|  | ||||
|  | ||||
| # In[56]: | ||||
|  | ||||
|  | ||||
| #function to call for evaluating two emojis in its sentimental distance | ||||
| def emoji_distance(e1,e2): | ||||
|     sent_v1 = emoji_to_sentiment_vector(e1) | ||||
|     sent_v2 = emoji_to_sentiment_vector(e2) | ||||
|  | ||||
|     d = sentiment_vector_dist(sent_v1,sent_v2) | ||||
|     return d | ||||
|  | ||||
|  | ||||
| # In[57]: | ||||
|  | ||||
|  | ||||
| def sentiment_vector_to_emoji(v1): | ||||
|     #if(len(v1) == 3): | ||||
|         #set initial values to compare with | ||||
|         best_emoji = "😐" | ||||
|         min_distance = 10000 | ||||
|  | ||||
|         #compare only with filtred emoticons | ||||
|         df_filtered = df[df["Unicode block"]=="Emoticons"] | ||||
|         all_smilies = list(df_filtered["Emoji"]) | ||||
|         for e in all_smilies: | ||||
|             v2 = emoji_to_sentiment_vector(e) | ||||
|             d = sentiment_vector_dist(v1,v2) | ||||
|             if(d < min_distance): | ||||
|                 min_distance = d | ||||
|                 best_emoji = e | ||||
|                 #print(str(v1),str(v2),str(min_distance),str(type(v1)),str(type(v2)),e) | ||||
|  | ||||
|  | ||||
|         #print("for sentiment vector: "+str(v1)+" the emoji is : "+str(best_emoji)+" with distance of "+str(min_distance)+"!") | ||||
|         return best_emoji | ||||
|  | ||||
|     #else: | ||||
|         #print("WRONG SENTIMENT VECTOR") | ||||
|  | ||||
|  | ||||
| # In[58]: | ||||
|  | ||||
|  | ||||
| def show_demo(): | ||||
|     df_filtered = df[df["Unicode block"]=="Emoticons"] | ||||
|     all_smilies = list(df_filtered["Emoji"]) | ||||
|  | ||||
|     d_m = np.zeros(shape=(len(all_smilies),len(all_smilies))) | ||||
|  | ||||
|     for c1 in range(len(all_smilies)): | ||||
|         for c2 in range(len(all_smilies)): | ||||
|             e1 = all_smilies[c1] | ||||
|             e2 = all_smilies[c2] | ||||
|  | ||||
|             d = emoji_distance(e1,e2) | ||||
|             d_m[c1,c2] = d | ||||
|  | ||||
|     for c in range(len(d_m[0])): | ||||
|         emoji = all_smilies[c] | ||||
|         row = d_m[c] | ||||
|         row_sorted = np.argsort(row) | ||||
|         #closest 5 | ||||
|         r = row_sorted[0:10] | ||||
|         #print() | ||||
|         closest = "" | ||||
|         for i in r: | ||||
|             closest+=all_smilies[i] | ||||
|         print(emoji+": "+closest) | ||||
|  | ||||
|  | ||||
| # In[60]: | ||||
|  | ||||
|  | ||||
| #show_demo() | ||||
|  | ||||
|  | ||||
| # In[61]: | ||||
|  | ||||
|  | ||||
| #test bipolar matching entiment vector vs. emoji | ||||
| #df_filtered = df[df["Unicode block"]=="Emoticons"] | ||||
| #all_smilies = list(df_filtered["Emoji"]) | ||||
| #for e in all_smilies: | ||||
| #    v2 = emoji_to_sentiment_vector(e) | ||||
| #    sentiment_vector_to_emoji(v2) | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user