#! /usr/bin/env python3

import re
from NLP21 import *

def read_and_write_category_name():
    lines = read_gz(filename)
    dictionaries = make_json_dictionaries(lines)
    category_lines = get_category_lines(dictionaries)
    category_names = get_category_names(category_lines)
    write_category_names(category_names)

def get_category_lines(dictionaries):
    ret = []
    for d in dictionaries:
        temp_d = {}
        temp_d['title'] = d['title']
        temp_d['category'] = []
        lines = d['text'].split('\n')
        for l in lines:
            if l.find('Category') >= 0:
                temp_d['category'].append(l)
        ret.append(temp_d)
    return ret

def get_category_names(category_lines):
    ret = []
    for d in category_lines:
        temp_d = {}
        temp_d['title'] = d['title']
        names = get_category_name(d['category'])
        temp_d['category'] = names
        ret.append(temp_d)
    return ret

def get_category_name(lines):
    ret = []
    for l in lines:
        p = re.compile('^.*\[\[Category:(.+)\]\]')
        res_list = p.findall(l)
        for res in res_list:
            ret.append(res)
    return ret

def debug(s, l):
    pass

def write_category_names(category_names):
    for d in category_names:
        print(d['title'])
        for name in d['category']:
            print('\t', name)

if __name__ == '__main__':
    read_and_write_category_name()
