-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles_to_json.py
86 lines (62 loc) · 2.17 KB
/
profiles_to_json.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
from ibm_watson import PersonalityInsightsV3
from os.path import join, dirname
import json
import pandas as pd
from itertools import chain
from collections import defaultdict
from funcy import merge_with
import os
def json_to_dict(filename):
personality_insights = PersonalityInsightsV3(
iam_apikey=API_KEY,
version='2017-10-13',
url="https://gateway.watsonplatform.net/personality-insights/api"
)
with open(join(dirname(__file__), filename)) as profile_json:
profile = personality_insights.profile(
profile_json.read(),
'application/json',
content_type='application/json',
consumption_preferences=True,
raw_scores=True
).get_result()
scores_dict = dict()
def json_dict(profile_str):
scores_dict[profile_str] = []
for v in profile[profile_str]:
temp_dict = dict()
temp_dict["name"] = v["name"]
temp_dict["raw_score"] = v["raw_score"]
scores_dict[profile_str].append(temp_dict)
def get_scores(profile_str):
scores_dict[profile_str] = dict()
for v in profile[profile_str]:
scores_dict[profile_str][v["name"]] = v["raw_score"]
get_scores("personality")
get_scores("needs")
get_scores("values")
personality = scores_dict["personality"]
needs = scores_dict["needs"]
dictionary = {**personality, **needs}
return dictionary
def personality_df(path=os.getcwd()):
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if 'profile.json' in file:
files.append(os.path.join(r, file))
for num, f in enumerate(files):
files[num] = json_to_dict(f)
header = list(files[0].keys())
multi_dict = merge_with(list, files)
df = pd.DataFramedf = pd.DataFrame(multi_dict, columns=header)
return df
def main():
df = personality_df()
print('Generated data table from "_profile.json" files:')
print(df)
df.to_json(r'{}/dataset.json'.format(os.getcwd()))
print('Formating data into json file called "dataset.json"')
if __name__ == "__main__":
main()