-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprocess_data.py
66 lines (45 loc) · 1.27 KB
/
process_data.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
import re
import json
dict = json.loads( open('words_vector_plus1.json', 'r').read() )
def get_index(word):
if word in dict:
return dict[word]
dict[word] = max( dict.values() ) + 1
return dict[word]
x = []
y = []
# how many words have a title?
_max = 0
_avg = 0
_maxScore = 0
_avgScore = 0
categories = ["newstories", "showstories", "topstories", "beststories"]
for cat in categories:
data = json.loads( open('{}.json'.format(cat), 'r').read() )
for storie in data:
_x = [get_index(w.lower()) for w in re.findall(r'\w+', storie['title']) if w.isalpha()]
len_x = len(_x)
if len_x > _max:
_max = len_x
_avg += len_x
if storie['score'] > _maxScore:
_maxScore = storie['score']
_avgScore += storie['score']
x.append( _x )
if storie['score'] < 70 :
y.append( [1,0] )
else:
y.append( [0,1] )
print( "max: {} \navg: {}".format( _max, _avg/len(x) ) )
print( "Max Score: {} \nAvg Score: {}".format(_maxScore, _avgScore/len(y)) )
# normalize entries to 20 words max, pad with 0
for _x in x:
delta = 20 - len(_x)
for i in range(delta):
_x.append(0)
with open('words_vector_plus1.json', 'w') as f:
f.write( json.dumps( dict ) )
with open('x.json', 'w') as f:
f.write( json.dumps( x ) )
with open('y.json', 'w') as f:
f.write( json.dumps( y ) )