-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_filter.py
171 lines (157 loc) · 6.34 KB
/
data_filter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# Data filtering and correlation
import time
import json
# import pandas as pd
# import numpy as np
import csv
# DATA_DIR = './'
# DATA_DIR = '/home/jl/'
DATA_DIR = '/media/osonoble/Águila Alfa/BIGDATA/'
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k.
ref -> https://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python"""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def close_brackets(my_str,i=0):
l_b = my_str.find('{')
r_b = my_str.find('}')
if l_b >= 0:
i += 1
close_brackets(my_str[l_b+1:])
elif r_b >= 0 and i == 0:
return
elif r_b == -1:
return ''
return my_str
def main():
acc = ''
p = -1
tweet_count = 0
json_parsed = {}
end_flag = False
csvfile = open('geopy_results_{}.csv'.format(time.time()), 'w')
csvwriter = csv.writer(csvfile)
head = ['TweetID', 'Date', 'Lat', 'Long', 'Text', 'UserID']
csvwriter.writerow(head)
with open(DATA_DIR + 'saved_tweets_big.json', 'r') as fp:
# with open(DATA_DIR + 'mini_file.json', 'r') as fp:
json_listed = [i.split('}{') for i in fp][0]
for el in json_listed:
if el[0] != '{' and el[-1] != '}':
# print('{' + el + '}')
unzip = '{' + el + '}'
try:
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
sub_els = el.split('}\n{')
try:
for sub_el in sub_els:
if sub_el[0] != '{' and sub_el[-1] != '}':
unzip = '{' + sub_el + '}'
json_parsed = json.loads(unzip)
elif sub_el[0] != '{' and sub_el[-1] == '}':
unzip = '{' + sub_el
json_parsed = json.loads(unzip)
elif sub_el[0] == '{' and sub_el[-1] != '}':
unzip = sub_el + '}'
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
elif el[0] != '{' and el[-1] == '}':
# print('{' + el )
unzip = '{' + el
try:
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
sub_els = el.split('}\n{')
try:
for sub_el in sub_els:
if sub_el[0] != '{' and sub_el[-1] != '}':
unzip = '{' + sub_el + '}'
json_parsed = json.loads(unzip)
elif sub_el[0] != '{' and sub_el[-1] == '}':
unzip = '{' + sub_el
json_parsed = json.loads(unzip)
elif sub_el[0] == '{' and sub_el[-1] != '}':
unzip = sub_el + '}'
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
elif el[0] == '{' and el[-1] != '}':
# print(el + '}')
unzip = el + '}'
try:
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
sub_els = el.split('}\n{')
try:
for sub_el in sub_els:
if sub_el[0] != '{' and sub_el[-1] != '}':
unzip = '{' + sub_el + '}'
json_parsed = json.loads(unzip)
elif sub_el[0] != '{' and sub_el[-1] == '}':
unzip = '{' + sub_el
json_parsed = json.loads(unzip)
elif sub_el[0] == '{' and sub_el[-1] != '}':
unzip = sub_el + '}'
json_parsed = json.loads(unzip)
except json.decoder.JSONDecodeError as e:
# print('Error decoding {}'.format(unzip))
print('Error decoding')
# Filtering
user_id = json_parsed['user']['id']
tweet_id = json_parsed['id_str']
if json_parsed['coordinates'] is not None:
# print(json_parsed['coordinates'])
longitude = json_parsed['coordinates']['coordinates'][0]
latitude = json_parsed['coordinates']['coordinates'][1]
else:
bbox = json_parsed['place']['bounding_box']['coordinates'][0]
# print(bbox)
XY = [(bbox[0][0] + bbox[2][0]) / 2, (bbox[0][1] + bbox[2][1]) / 2]
longitude = XY[0]
latitude = XY[1]
timestamp = json_parsed['timestamp_ms']
if 'extended_tweet' in json_parsed.keys():
text = json_parsed['extended_tweet']['full_text'].lower()
else:
text = json_parsed['text'].lower()
csvwriter.writerow(
[tweet_id,
timestamp,
latitude,
longitude,
''.join(
list(
filter(
lambda ch: ch not in "?.!/;:,",
''.join(text.split('\n'))
)
)
),
user_id])
# print(unzip)
# print(json.dumps(json_parsed[1],indent=2))
# print(json_parsed[1]['place']['full_name'])
# for i in range(2):
# if 'extended_tweet' in json_parsed[i].keys():
# print(json_parsed[i]['extended_tweet']['full_text'].lower().split())
# else:
# print(json_parsed[i]['text'].lower().split())
# print(a)
# ID, timestamp, latitud, longitud, texto, USER
if __name__ == "__main__":
main()