-
Notifications
You must be signed in to change notification settings - Fork 2
/
emotion.py
105 lines (93 loc) · 3.11 KB
/
emotion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
analyzer = SentimentIntensityAnalyzer()
import spacy
from spacymoji import Emoji
nlp = spacy.load('en')
emoji = Emoji(nlp)
nlp.add_pipe(emoji, first=True)
nlp1 = spacy.load('en')
def add_compound(set):
emotion_set = []
for index, row in set.iterrows():
vs = analyzer.polarity_scores(row['twitters'])
emotion_set.append(vs['compound'])
set['emotion_score'] = emotion_set
return set
def add_emojis(frame):
emojisite = []
for index, row in frame.iterrows():
num_of_emoji = 0
post = row['twitters']
try:
tokens = nlp(post)
for token in tokens:
if token._.is_emoji:
num_of_emoji=num_of_emoji+1
emojisite.append(num_of_emoji)
except ValueError:
emojisite.append(num_of_emoji)
frame["emoji"] = emojisite
return frame
def add_hashtag(frame):
hash_list = []
for index, row in frame.iterrows():
num_of_tag = 0
post = row['twitters']
tokens = nlp1(post)
for token in tokens:
if token.text.lower() == 'hashtag':
num_of_tag=num_of_tag+1
hash_list.append(num_of_tag)
frame["hash"] = hash_list
return frame
def add_RT(frame):
RT_list = []
for index, row in frame.iterrows():
num_of_tag = 0
post = row['twitters']
tokens = nlp1(post)
for token in tokens:
if token.text.lower() == 'rt':
num_of_tag=num_of_tag+1
RT_list.append(num_of_tag)
frame["RT"] = RT_list
return frame
def add_URL(frame):
RT_list = []
for index, row in frame.iterrows():
num_of_tag = 0
post = row['twitters']
tokens = nlp1(post)
for token in tokens:
if token.text.lower() == 'url':
num_of_tag=num_of_tag+1
RT_list.append(num_of_tag)
frame["url"] = RT_list
return frame
def add_trump(frame):
RT_list = []
for index, row in frame.iterrows():
num_of_tag = 0
post = row['twitters']
tokens = nlp1(post)
for token in tokens:
if token.text.lower() == 'trump':
num_of_tag=num_of_tag+1
RT_list.append(num_of_tag)
frame["trump"] = RT_list
return frame
if __name__ == "__main__":
# sentence = 'RT #USER#: Danny Ings levels against his former club‼️ 1️⃣5️⃣ PREM GOALS FOR THE SEASON! ⚽️ #HASHTAG# I #HASHTAG# #HASHTAG# #URL#…'
# tokens =nlp1(sentence)
# for token in tokens:
# if token.text.lower() == 'hashtag':
# print('find one')
for i in range(0,10):
path = "text_classification/data/emotionresults/"
emoji_path = "text_classification/data/emotionresults/"
filename =("%d.csv" %i)
test_file = path +filename
dataframe = pd.read_csv(test_file)
emotionframe = add_trump(dataframe)
emotionframe.to_csv(emoji_path+filename, index =False)