-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_tweets.py
49 lines (40 loc) · 1.37 KB
/
score_tweets.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
import pandas as pd
import csv
df = pd.read_csv(r"df_text2.csv", encoding="cp1252")
df.head()
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "file.json"
#######################################################
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
v_score = []
v_magnitude= []
i = 0
while True:
text = str(df.iloc[i][0])
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
v_score.append(sentiment.score)
v_magnitude.append(sentiment.magnitude)
#print('Text: {}'.format(text))
#print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
i = i + 1
if(i >= len(df)):
break
#######################################################
with open('data_score.csv','w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(map(lambda x: [x], v_score))
csvFile.close()
with open('data_magnitude.csv','w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(map(lambda x: [x], v_magnitude))
csvFile.close()