-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsentiment.py
74 lines (57 loc) · 1.71 KB
/
sentiment.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from tqdm import tqdm
import nltk
import pickle
# Load data
with open ('preprocessed_tweets.pickle', 'rb') as fp:
tweets = pickle.load(fp)
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
'''
Before running the sentiment annotator:
Download the Stanford Core NLP model (https://stanfordnlp.github.io/CoreNLP/#download)
Unizip the folder
cd into the folder
cd stanford-corenlp-4.3.2/
Start the server using this command:
java -mx5g -cp "./*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000
'''
# Assign sentiment
def getSentiments():
sentiments = []
for tweet in tweets:
res = nlp.annotate(tweet,
properties={'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000
})
try:
s = res['sentences'][0]['sentiment']
except:
s = ''
sentiments.append(s)
return sentiments
def updateJsonFile():
jsonFile = open("tweets_emotions.json", "r")
data = json.load(jsonFile)
jsonFile.close()
sentiments = getSentiments()
i = 0
for tweet in tqdm(data):
tweet['sentiment'] = sentiments[i]
i += 1
with open("tweets_emoSent.json", "w+") as jsonFile:
jsonFile.write(json.dumps(data))
updateJsonFile()
# # Check json file
# jsonFile = open("tweets_emoSent.json", "r")
# check = json.load(jsonFile)
# jsonFile.close()
# # Check how many tweets don't have a sentiment
# count = 0
# for tweet in check:
# if tweet['sentiment'] == '':
# count += 1
# count # 462