-
Notifications
You must be signed in to change notification settings - Fork 0
/
precompute_files.py
executable file
·60 lines (50 loc) · 1.53 KB
/
precompute_files.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
#! /usr/bin/python
import zipfile
import sys
import server
from os import listdir
from os.path import join
def append_word_to_file(output_file, word, count):
if count > 0:
output_file.write(word + "\t" + str(count) + "\n")
def append_word_to_short_file(output_file, word1, word2, count):
if count > 0:
output_file.write(word1 + ' ' + word2 + ' ' + str(count) + "\n")
def preprocess_file(file_name):
sf = open(server.short_file_path(file_name), 'w')
output_file = open(server.count_file_path(file_name), 'w')
z = zipfile.ZipFile(server.zip_file_path(file_name), 'r')
internal_name = z.namelist()[0]
f = z.open(internal_name)
word = ''
word2 = ''
count = 0
count_pair = 0
for line in f:
tokens = line.split()
if tokens[0] != word:
append_word_to_file(output_file, word, count)
append_word_to_short_file(sf, word, word2, count_pair)
count = 0
word = tokens[0]
word2 = tokens[1]
count_pair = 0
elif tokens[1] != word2:
append_word_to_short_file(sf, word, word2, count_pair)
word2 = tokens[1]
count_pair = 0
count += int(tokens[3])
count_pair += int(tokens[3])
append_word_to_file(output_file, word, count)
append_word_to_short_file(sf, word, word2, count_pair)
f.close()
sf.close()
z.close()
output_file.close()
def create_big_map():
for file_name in server.file_names():
preprocess_file(file_name)
def main():
create_big_map()
if __name__ == '__main__':
main()