<commit_before>
# encoding: utf-8

import microsofttranslator as _microsofttranslator


class EmptyTranslationChainError(Exception):
    """
    Error to indicate translation chaining list must contain
    more than two language labels.
    """
    pass


class ChainTranslator(object):
    """
    Chain multiple translation using Microsoft Translator API
    (http://api.microsofttranslator.com/V2/Http.svc/Translate),
     using 'microsofttranslator' module
    (git://github.com/openlabs/Microsoft-Translator-Python-API.git)
    for API access.

    This class supplies simple wrapper for 'Translator' class in 
    microsofttranslator module, and ease the operations of translation
    requests.

    Usage:
        >>> from mstranschain import ChainTranslator
        >>> chain_translator = ChainTranslator('<Your Client ID>', '<Your Client Secret>')
        >>> print(chain_translator.translate("おはよう、諸君。", chain=['ja', 'en', 'pt', 'ja'])
        u""
    """

    def __init__(self, client_id, client_secret):
        self._translator = _microsofttranslator.Translator(client_id, client_secret)

    def translate(self, text, lang_chain):
        """
        Translate a text string through languages in lang_chain list.
        lang_chain must contain two or more string labels for languages.
        """
        # Shortage of lang_chain error.
        if len(lang_chain) < 2:
            raise EmptyTranslationChainError('ChainTranslator.translate takes two or more languages to chain translation.')
        # Shrink lang_chain if sequences of identical lang-labels found.
        lang_chain_shrink = [lang_chain.pop(0)]
        for lang in lang_chain:
            if lang == lang_chain_shrink[-1]:
                continue
            else:
                lang_chain_shrink.append(lang)
        # Shorten into less than pair by shrinking, results no error & returns results.
        if len(lang_chain_shrink) == 1:
            return text
        # Translation process.
        result = text
        for from_lang, to_lang in zip(lang_chain_shrink, lang_chain_shrink[1:]):<commit_msg>Revised doctest.<commit_after>
# encoding: utf-8

import microsofttranslator as _microsofttranslator


class EmptyTranslationChainError(Exception):
    """
    Error to indicate translation chaining list must contain
    more than two language labels.
    """
    pass


class ChainTranslator(object):
    """
    Chain multiple translation using Microsoft Translator API
    (http://api.microsofttranslator.com/V2/Http.svc/Translate),
     using 'microsofttranslator' module
    (git://github.com/openlabs/Microsoft-Translator-Python-API.git)
    for API access.

    This class supplies simple wrapper for 'Translator' class in 
    microsofttranslator module, and ease the operations of translation
    requests.

    Usage:
        >>> from mstranschain import ChainTranslator
        >>> chain_translator = ChainTranslator('<Your Client ID>', '<Your Client Secret>')
        >>> print(chain_translator.translate("おはよう、諸君。", chain=['ja', 'en', 'pt', 'ja'])
        u"\u826f\u3044\u65e5\u306f\u3001\u7d33\u58eb\u3067\u3059\u3002"
    """

    def __init__(self, client_id, client_secret):
        self._translator = _microsofttranslator.Translator(client_id, client_secret)

    def translate(self, text, lang_chain):
        """
        Translate a text string through languages in lang_chain list.
        lang_chain must contain two or more string labels for languages.
        """
        # Shortage of lang_chain error.
        if len(lang_chain) < 2:
            raise EmptyTranslationChainError('ChainTranslator.translate takes two or more languages to chain translation.')
        # Shrink lang_chain if sequences of identical lang-labels found.
        lang_chain_shrink = [lang_chain.pop(0)]
        for lang in lang_chain:
            if lang == lang_chain_shrink[-1]:
                continue
            else:
                lang_chain_shrink.append(lang)
        # Shorten into less than pair by shrinking, results no error & returns results.
        if len(lang_chain_shrink) == 1:
            return text
        # Translation process.
        result = text
        for from_lang, to_lang in zip(lang_chain_shrink, lang_chain_shrink[1:]):
{'fraction_non_alphanumeric': 0.08676228544556784, 'fraction_numerical': 0.011286150952268986, 'mean_word_length': 2.877848678213309, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7610599', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1170, 'n_words': 340}