-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcategorise_data.py
40 lines (35 loc) · 1.11 KB
/
categorise_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
import sys
import os
from os import listdir
from os.path import isfile, join
def read_categories(filename):
with open(filename) as f:
cats = f.read().splitlines()
return cats
def create_directory(newpath):
if not os.path.exists(newpath):
os.makedirs(newpath)
def write_titles(titles,cat):
cat_dir = cat.replace(' ','_')
f = open(join(join('./data',cat_dir),'titles.txt'),'w')
for title in titles:
f.write(title+'\n')
f.close()
def summarise_titles(cat):
all_titles = []
dir_files = [f for f in listdir('./data') if isfile(join('./data', f)) and 'categories_titles' in f]
for f in dir_files:
cat_titles = open(join('./data',f))
for l in cat_titles:
fields = l.rstrip('\n').split('\t')
c = fields[0]
titles = fields[1]
if c == cat:
all_titles.extend(titles.split(','))
break
write_titles(all_titles,cat)
cats = read_categories(sys.argv[1])
for cat in cats:
catdir = cat.replace(' ','_')
create_directory(join('./data',catdir))
summarise_titles(cat)