-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_data_dict.py
69 lines (58 loc) · 1.74 KB
/
build_data_dict.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
from datetime import datetime
import cPickle
import csv
import sys
import os
def buildVocab(csv_f):
"""builds the vocab for the data"""
t_1 = datetime.now()
vocab = []
reader = csv.reader(open(csv_f, "rb"))
reader_l = list(reader)
ln = len(reader_l)
for i, row in enumerate(reader_l):
text = row[2]
text_l = text.split(" ")
for word in text_l:
if word not in vocab:
vocab.append(word)
print (i * 100.) / ln, datetime.now() - t_1, i
return vocab
def buildData(csv_f, vocab):
"""builds the dataset"""
t_1 = datetime.now()
data = {}
reader = csv.reader(open(csv_f, "rb"))
reader_l = list(reader)
ln = len(reader_l)
for i, row in enumerate(reader_l):
text = row[2].split(" ")
node = row[1]
dateb = row[0].split(" ")
period = dateb[1] + " " + dateb[2] + " " + dateb[5]
if node in data:
if period in data[node]:
doc = []
for w in text:
doc.append(vocab.index(w))
data[node][period].append(doc)
else:
data[node][period] = []
doc = []
for w in text:
doc.append(vocab.index(w))
data[node][period].append(doc)
else:
data[node] = {}
data[node][period] = []
doc = []
for w in text:
doc.append(vocab.index(w))
data[node][period].append(doc)
print (i * 100.) / ln, datetime.now() - t_1, i
return data
os.chdir(sys.argv[3])
csv_f = sys.argv[1]
vocab = buildVocab(csv_f)
data = buildData(csv_f, vocab)
cPickle.dump(data, open(sys.argv[2], "wb"))