-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemotion.py
43 lines (31 loc) · 930 Bytes
/
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from tqdm import tqdm
from nrclex import NRCLex
import pickle
# Load data
with open ('preprocessed_tweets.pickle', 'rb') as fp:
tweets = pickle.load(fp)
# List of emotions
# fear, anger, anticipation, trust, surprise, positive, negative, sadness, disgust, joy
# Assign emotion
def getEmotions():
emotions = []
for tweet in tweets:
emotion = NRCLex(tweet)
e = [item[0] for item in emotion.top_emotions] # Get emotions
emotions.append(e)
return emotions
def updateJsonFile():
jsonFile = open("tweet_TR.json", "r")
data = json.load(jsonFile)
jsonFile.close()
emotions = getEmotions()
i = 0
for tweet in tqdm(data):
tweet['emotions'] = emotions[i]
i += 1
with open("tweets_emotions.json", "w+") as jsonFile:
jsonFile.write(json.dumps(data))
updateJsonFile()