# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/00_data.ipynb (unless otherwise specified).

__all__ = ['generate_translator', 'default_translator_path', 'strip_html', 'OriTraTranslation',
           'create_ori_trans_dataframe', 'print_dataframe_table', 'plot_text_length', 'PrettyDataset']

# Cell
default_translator_path = 'translate-en_de-1_0.argosmodel'
def generate_translator(path='translate-en_de-1_0.argosmodel'):
    '''
        path[str]: path to file from https://www.argosopentech.com/argospm/index/
    '''
    package.install_from_path(path)
    installed_languages = translate.get_installed_languages()
    [str(lang) for lang in installed_languages]
    translation_en_de = installed_languages[0].get_translation(installed_languages[1])
    return translation_en_de

# Cell
def strip_html(text):
    '''
    Remove html text
    '''
    soup = BeautifulSoup(text, "html.parser")
    return soup.get_text()

# Cell
class OriTraTranslation():
    def __init__(self, ori_text, path=default_translator_path):
        '''
        Input:
            ori_text[str]: Original text
            path[str]: path to file from https://www.argosopentech.com/argospm/index

        Output:
            dict[dict]: { ori_text:Original text, translated_text: translated text }
        '''
        self.ori_text = ori_text
        self.translator = generate_translator(path)

    def __len__(self): return len(self.ori_text)

    def __getitem__(self, idx):
        return {
            'ori_text': self.ori_text[idx],
            'translated_text' : self.translator.translate(self.ori_text[idx])
        }

# Cell
def create_ori_trans_dataframe(text_in, path):
    '''
    Output: Pandas dataframe: {'text_in': input text, 'trans_text':translated text}
    '''
    ori_sentences = []
    trans_sentences = []

    en_de_translation = OriTraTranslation(text_in, path)

    for idx in tqdm(range(len(text_in)), desc='Translating text : '):
        en_de = en_de_translation[idx]
        ori_sentences.append(en_de['ori_text'])
        trans_sentences.append(en_de['translated_text'])

    df = pd.DataFrame()
    df['ori_text'] = ori_sentences
    df['trans_text'] = trans_sentences

    return df

# Cell
def print_dataframe_table(df, n=3, schuffle=True):
    '''
    Randomly see n examples
    '''
    if schuffle: df = df.sample(frac=1).reset_index(drop=True)
    ptable = PrettyTable()
    keys = df.keys().to_list()
    ptable.field_names = df.keys().to_list()
    plist = []
    for idx in range(n):
        l = list()
        for key in keys:
            l.append( df[key][idx] )
        plist.append(l)
    ptable.add_rows(plist)

    return ptable

# Cell
def plot_text_length(texts, percentile=None):
    '''
    Takes df['text'] as an input and return `sns.distplot`.
    '''
    text_length = [len( text.split() ) for text in texts]
    if percentile:
        print('--------------------------------')
        print(f'Text length {percentile} percentile: {round(np.percentile(text_length, percentile))}')
        print('--------------------------------')
    sns.set_style('darkgrid')
    plt.figure(figsize=(15,10))
    plt.xlabel('Sentence length')
    sns.distplot(text_length)
    plt.title('Sentence length vs Density')
    plt.show()

# Cell
class PrettyDataset():
    def __init__(self,*args, **kwargs):
        self.args = args
        self.data = kwargs

    def plot_text_length(self, texts, percentile=None):
        '''
        Input:
            texts[list]     : list of text
            percentile[int] : if given it will also provide sentence percentile
        Output:
            seaborn plot
        '''
        if not hasattr(self,texts): return 'Wrong attribute name'
        text_length = [len( text.split() ) for text in getattr(self, texts)]
        if percentile:
            print('--------------------------------')
            print(f'Text length {percentile} percentile: {round(np.percentile(text_length, percentile))}')
            print('--------------------------------')
        sns.set_style('darkgrid')
        plt.figure(figsize=(15,10))
        plt.xlabel('Sentence length')
        sns.distplot(text_length)
        plt.title('Sentence length vs Density')
        plt.show()

    def show_output_examples(self, n):
        '''
        n[int]: Show n examples
        '''
        outs = []
        for idx in range(n):
            out = self.__getitem__(idx)
            outs.append(out)
        if type(outs[0])==dict:
            df = pd.DataFrame(outs)
            return print_dataframe_table( df, n=n )
        return outs