import logging
import os
from collections import namedtuple

from data_analysis_compare_sr_ud import extract_overlapping_data

logger = logging.getLogger('filtering_ud_data')
logging.basicConfig(level=logging.INFO)

DataInfo = namedtuple('DataInfo', ['sc', 'ss', 'ud', 'out'])
DATA_SPLITS = ['train', 'dev']
UD_DATASETS = ['UD_Arabic', 'UD_Czech', 'UD_English', 'UD_Spanish-AnCora', 'UD_Finnish',
               'UD_French', 'UD_Italian', 'UD_Dutch', 'UD_Portuguese', 'UD_Russian-SynTagRus']

LANGS = ['ar', 'cs', 'en', 'es_ancora', 'fi',
         'fr', 'it', 'nl', 'pt', 'ru_syntagrus']

HOME = os.environ['HOME']

OUTPUT_FOLDER = '{home}/var/data/sr2018/ud_filtered'.format(home=HOME)
for split in DATA_SPLITS:
    d = os.path.join(OUTPUT_FOLDER, split)
    if not os.path.exists(d):
        os.makedirs(d)

for i, dset in enumerate(UD_DATASETS):
    lang = LANGS[i]

    for split in DATA_SPLITS:
        ud_fname = '{home}/var/data/ud-treebanks-v2.0/{dataset}/{lang}-ud-{split}.conllu'.format(home=HOME,
                                                                                                 dataset=dset,
                                                                                                 lang=lang,
                                                                                                 split=split)

        sc_fname = '{home}/var/data/sr2018/T1-input/{split}/{lang}-ud-{split}.conll'.format(home=HOME,
                                                                                            lang=lang,
                                                                                            split=split)

        ss_fname = '{home}/var/data/sr2018/Sentences/{split}/{lang}-ud-{split}_sentences.txt'.format(home=HOME,
                                                                                                     lang=lang,
                                                                                                     split=split)

        out_fname = '{output_folder}/{split}/{lang}-ud-{split}.filtered.conll'.format(output_folder=OUTPUT_FOLDER,
                                                                                      lang=lang,
                                                                                      split=split)

        data_info = DataInfo(ud=ud_fname, sc=sc_fname, ss=ss_fname, out=out_fname)
        extract_overlapping_data(args=data_info)

    logger.info('DONE: %s', lang)
    print('\n')
