-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_subsets.py
59 lines (47 loc) · 1.24 KB
/
get_subsets.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
import sys
import random
class LineIter:
def __init__(self, filename, limit):
self.file = open(filename, 'r')
self.limit = limit
self.count = 0
def __iter__(self):
return self
def next(self):
# Read one entry
line = self.file.readline()
if line == '' or self.count > self.limit:
self.file.close()
raise StopIteration()
else:
rating_num = 0
rating = None
text = None
while rating_num < 10:
rating = line
rating_num = int(rating.split(':')[1].split('/')[1])
text = self.file.readline()
self.file.readline()
if rating_num < 10:
line = self.file.readline()
self.count += 1
return rating + text + '\n'
def write_subset(iterator, items_wanted, total_items):
selected_items = []
count = 0
for item in iterator:
if random.random() < (items_wanted + 0.0) / total_items:
selected_items.append(item)
count += 1
if count % 10000 == 0:
print count
return selected_items
if __name__ == "__main__":
train = write_subset(LineIter('train.txt', 1000000), 15000, 1000000)
test = write_subset(LineIter('test.txt', 1000000), 10000, 1000000)
with open('small_train.txt', 'w') as f:
for str in train:
f.write(str)
with open('small_test.txt', 'w') as f:
for str in test:
f.write(str)