-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfb_emop.py
92 lines (73 loc) · 2.69 KB
/
fb_emop.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
87
88
89
90
91
# -*- coding:utf-8 -*-
import os
import json
import codecs
import urllib2
from pprint import pprint
from datetime import datetime
class EmotionDetection(object):
"""docstring for EmotionDetection"""
def __init__(self):
super(EmotionDetection, self).__init__()
# __url = 'http://140.114.77.18:5678/emomap/couple'
__url = 'http://140.114.77.18:5678/chuck/couple_all'
def get_obj_from_text(self, texts_list):
'''
Input : [TEXT_LIST]
Output: [JSON]
'''
# Prepare the query information
query = {"data":[]}
for text in texts_list:
query["data"].append({"message":text, "datetime": datetime.now().strftime("%Y/%m/%d %H:%M:%S"), "story":"Huang Yen Hao at 清華名人堂."})
req = urllib2.Request(self.__url)
req.add_header('Content-Type', 'application/json')
# Send request
response = urllib2.urlopen(req, json.dumps(query))
return json.loads(response.read())
def get_obj_from_json(self, out_json):
'''
Input : [JSON]
Output: [JSON]
'''
req = urllib2.Request(self.__url)
req.add_header('Content-Type', 'application/json')
# Send request
response = urllib2.urlopen(req, json.dumps(out_json))
return json.loads(response.read().encode('utf-8'))
def query(d, folder, filename):
result_json = {}
for key, val in d.iteritems():
length = len(val)
if length > 0 :
batch = 16
total_round = length / batch
print('Total-Round : ' + str(total_round))
result = []
for i in xrange(total_round):
out_json = {"data":val[ batch * i : batch * (i+1)]}
# for dict_ in em.get_obj_from_json(out_json)['data']:
# dict_["message"] = dict_["message"].encode('utf-8')
# print(type(dict_["message"]))
# result.append(dict_)
result += em.get_obj_from_json(out_json)['data']
result_json[key] = result
else:
result_json[key] = []
with codecs.open( folder + 'emo_' + filename, 'w', encoding='utf-8') as outfile:
# outfile.write(json.dumps(result_json, ident=4), ensure_ascii=False)
json.dump(result_json, outfile, indent=4, ensure_ascii=False)
if __name__ == '__main__':
'''
create emotion detect object
'''
em = EmotionDetection()
'''
read json
'''
folder = 'fb/'
file_lists = os.listdir(folder)
for filename in os.listdir(folder):
with open(folder+filename) as json_data:
d = json.load(json_data)
query(d, folder, filename)