# -*- coding: utf-8 -*-

read_file = 'enwiki_conv.txt'
country_dict = 'country.txt'
write_file = 'enwiki_conv_2.txt'

def make_country_list():
    countries = []
    with open(country_dict, 'r') as fr:
        for r in fr.readlines():
            r = r.split('\t')[1].replace('"', '').rstrip('\n')
            countries.append(r)
    return countries

def conv_country_name(countries):
    fr = open(read_file, 'r')
    fw = open(write_file, 'w')
    for sen in fr.readlines():
        sen = sen.rstrip('\n')
        for country in countries:
            num = sen.count(country)
            if num == 0:
                continue
            for n in range(num):
                ret = sen.find(country)
                c = country.replace(' ', '_')
                sen = sen[:ret] + c + sen[ret+len(c):]
        fw.write(sen + '\n')
    fw.close()
    fr.close()

def main():
    countries = make_country_list()
    conv_country_name(countries)

if __name__ == '__main__':
    main()
