-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainlines.py
73 lines (56 loc) · 1.66 KB
/
trainlines.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
import json
import csv
from graphqlclient import GraphQLClient
import csv
client = GraphQLClient('https://api.graph.cool/simple/v1/ciw9brm021bfh0171mp8tiric')
# for each train line:
# get all average values
get_all_trainslines_query = '''
query
all {
allTrainlines {
name
id
avgAnger
avgSadness
avgJoy
avgFear
avgDisgust
}
}
'''
all_trainlines = json.loads(client.execute(get_all_trainslines_query))["data"]["allTrainlines"]
# get all answers and match them to their respective train line
get_all_answers_query = '''
query
all {
allAnswers {
trainline{
name
}
text
topic
}
}
'''
all_answers = json.loads(client.execute(get_all_answers_query))["data"]["allAnswers"]
overview = {}
for trainline in all_trainlines:
overview[trainline["name"]] = {}
overview[trainline["name"]]["avgSadness"] = trainline["avgSadness"] * 100
overview[trainline["name"]]["avgFear"] = trainline["avgFear"] * 100
overview[trainline["name"]]["avgDisgust"] = trainline["avgDisgust"] * 100
overview[trainline["name"]]["avgJoy"] = trainline["avgJoy"] * 100
overview[trainline["name"]]["avgAnger"] = trainline["avgAnger"] * 100
overview[trainline["name"]]["answers"] = []
for answer in all_answers:
if(answer["trainline"]["name"] == trainline["name"]):
# print(answer["text"] + "\n")
overview[trainline["name"]]["answers"].append([answer["text"], answer["topic"]])
for key, val in overview.items():
print(key,val)
# write all analysis to a json file
with open('traindata.csv', 'wb') as f:
w = csv.DictWriter(f, overview.keys())
w.writeheader()
w.writerow(overview)