"""
The raw CLDR data was retreived on 25th Jun , 2020 from the following link
https://github.com/unicode-cldr/cldr-rbnf
"""

import json
import logging
import os
import re

SOURCE_PATH = "../number_parser_data/raw_cldr_translation_data/"
SUPPLEMENTARY_PATH = "../number_parser_data/supplementary_translation_data/"
TARGET_PATH = "../number_parser/data/"


os.chdir(os.path.dirname(os.path.abspath(__file__)))


VALID_KEYS = ["spellout-cardinal", "spellout-numbering"]
INVALID_KEYS = ["cents"]
CAPTURE_BRACKET_CONTENT = r"\{(.*?)\}"
REQUIRED_NUMBERS_DATA = [
    "UNIT_NUMBERS",
    "DIRECT_NUMBERS",
    "TENS",
    "HUNDREDS",
    "BIG_POWERS_OF_TEN",
]


def _is_valid(key):
    """Identifying whether the given key of the source language file needs to be extracted."""
    is_valid = False
    for valid_key in VALID_KEYS:
        if valid_key in key:
            is_valid = True
    for invalid_key in INVALID_KEYS:
        if invalid_key in key:
            is_valid = False
    return is_valid


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == "ERROR" or word == "":
        return
    if number <= 9:
        language_data["UNIT_NUMBERS"][word] = number
    elif number <= 99:
        language_data["DIRECT_NUMBERS"][word] = number


def _count_zero(number):
    """Counting the number of zeroes in the given number."""
    zero_count = 0
    while number > 9:
        if number % 10 == 0:
            zero_count += 1
            number /= 10
        else:
            break
    return zero_count


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == "1":
        return

    if "[" in word:
        root_word = word.split("[")[0].strip()
    else:
        root_word = word.split(">")[0].strip()

    if root_word == "":
        return

    if power_of_10 == 1:
        language_data["TENS"][root_word] = number
    elif power_of_10 == 2:
        language_data["HUNDREDS"][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split("<")[-1]
    if required_part[0] != " ":
        return

    power_of_10 = _count_zero(number)
    if "$" in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data["BIG_POWERS_OF_TEN"][valid_word] = power_of_10_num
        return
    if "[" in required_part:
        valid_word = required_part.split("[")[0].strip()
    else:
        valid_word = required_part.split(">")[0].strip()

    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data["BIG_POWERS_OF_TEN"][valid_word] = power_of_10_num


def _extract_information(key, word, language_data):
    """Identify the type of number - simple,compound,multiplier."""
    try:
        number = int(key)
    except ValueError:
        print(f"The given key {key} is not an integer")
        return

    word = word.replace(";", "")
    count_greater_than_sign = word.count(">")
    count_less_than_sign = word.count("<")
    count_equal_to_sign = word.count("=")
    if count_equal_to_sign != 0:
        return
    if count_greater_than_sign == 0 and count_less_than_sign == 0:
        _add_base_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 0:
        _add_compound_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 2:
        _add_multiplier_words(number, word, language_data)


def write_complete_data():
    """
    Main function to extract data from source files , merge with supplementary data
    and write the combined results to the final target directory.
    """
    for file_name in os.listdir(SOURCE_PATH):
        if file_name in ["root.json", "es-419.json"]:
            # "root" is not a language, "es-419" doesn't contain spell-out rules
            continue
        full_source_path = os.path.join(SOURCE_PATH, file_name)
        full_target_path = os.path.join(TARGET_PATH, file_name.split(".")[0] + ".py")
        full_supplementary_path = os.path.join(SUPPLEMENTARY_PATH, file_name)

        language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        ordered_language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        with open(full_source_path, "r") as source:
            data = json.load(source)
            try:
                requisite_data = data["rbnf"]["rbnf"]["SpelloutRules"]
            except KeyError:
                logging.error(
                    f"\"['rbnf']['rbnf']['SpelloutRules']\" doesn't exist in {file_name}"
                )
                continue

            for keys, vals in requisite_data.items():
                if _is_valid(keys):
                    for key, val in vals.items():
                        # Removing soft-hyphens from the source file.
                        val = val.replace("\xad", "")
                        _extract_information(key, val, language_data)

        with open(full_supplementary_path, "r") as supplementary_data:
            data = json.load(supplementary_data)
            for keys in REQUIRED_NUMBERS_DATA:
                language_data[keys].update(data[keys])
                sorted_tuples = sorted(
                    language_data[keys].items(), key=lambda x: (x[1], x[0])
                )
                for items in sorted_tuples:
                    word, number = items[0], items[1]
                    ordered_language_data[keys][word] = int(number)
        ordered_language_data["SKIP_TOKENS"] = sorted(data["SKIP_TOKENS"])
        try:
            ordered_language_data["USE_LONG_SCALE"] = data["USE_LONG_SCALE"]
        except KeyError:
            logging.error(f"long_scale information missing in {file_name}")

        translation_data = json.dumps(
            ordered_language_data, indent=4, ensure_ascii=False
        )
        # Overwriting boolean value with capitalized form
        translation_data = re.sub(r"\bfalse\b", "False", translation_data)
        translation_data = re.sub(r"\btrue\b", "True", translation_data)
        out_text = "info = " + translation_data + "\n"
        with open(full_target_path, "w+") as target_file:
            target_file.write(out_text)


if __name__ == "__main__":
    write_complete_data()

42c5bbc5a3809a3d236445b62447836db882109a
<mask token>


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == 'ERROR' or word == '':
        return
    if number <= 9:
        language_data['UNIT_NUMBERS'][word] = number
    elif number <= 99:
        language_data['DIRECT_NUMBERS'][word] = number


<mask token>


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == '1':
        return
    if '[' in word:
        root_word = word.split('[')[0].strip()
    else:
        root_word = word.split('>')[0].strip()
    if root_word == '':
        return
    if power_of_10 == 1:
        language_data['TENS'][root_word] = number
    elif power_of_10 == 2:
        language_data['HUNDREDS'][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split('<')[-1]
    if required_part[0] != ' ':
        return
    power_of_10 = _count_zero(number)
    if '$' in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data['BIG_POWERS_OF_TEN'][valid_word
                    ] = power_of_10_num
        return
    if '[' in required_part:
        valid_word = required_part.split('[')[0].strip()
    else:
        valid_word = required_part.split('>')[0].strip()
    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data['BIG_POWERS_OF_TEN'][valid_word] = power_of_10_num


<mask token>

<mask token>


def _is_valid(key):
    """Identifying whether the given key of the source language file needs to be extracted."""
    is_valid = False
    for valid_key in VALID_KEYS:
        if valid_key in key:
            is_valid = True
    for invalid_key in INVALID_KEYS:
        if invalid_key in key:
            is_valid = False
    return is_valid


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == 'ERROR' or word == '':
        return
    if number <= 9:
        language_data['UNIT_NUMBERS'][word] = number
    elif number <= 99:
        language_data['DIRECT_NUMBERS'][word] = number


def _count_zero(number):
    """Counting the number of zeroes in the given number."""
    zero_count = 0
    while number > 9:
        if number % 10 == 0:
            zero_count += 1
            number /= 10
        else:
            break
    return zero_count


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == '1':
        return
    if '[' in word:
        root_word = word.split('[')[0].strip()
    else:
        root_word = word.split('>')[0].strip()
    if root_word == '':
        return
    if power_of_10 == 1:
        language_data['TENS'][root_word] = number
    elif power_of_10 == 2:
        language_data['HUNDREDS'][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split('<')[-1]
    if required_part[0] != ' ':
        return
    power_of_10 = _count_zero(number)
    if '$' in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data['BIG_POWERS_OF_TEN'][valid_word
                    ] = power_of_10_num
        return
    if '[' in required_part:
        valid_word = required_part.split('[')[0].strip()
    else:
        valid_word = required_part.split('>')[0].strip()
    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data['BIG_POWERS_OF_TEN'][valid_word] = power_of_10_num


def _extract_information(key, word, language_data):
    """Identify the type of number - simple,compound,multiplier."""
    try:
        number = int(key)
    except ValueError:
        print(f'The given key {key} is not an integer')
        return
    word = word.replace(';', '')
    count_greater_than_sign = word.count('>')
    count_less_than_sign = word.count('<')
    count_equal_to_sign = word.count('=')
    if count_equal_to_sign != 0:
        return
    if count_greater_than_sign == 0 and count_less_than_sign == 0:
        _add_base_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 0:
        _add_compound_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 2:
        _add_multiplier_words(number, word, language_data)


def write_complete_data():
    """
    Main function to extract data from source files , merge with supplementary data
    and write the combined results to the final target directory.
    """
    for file_name in os.listdir(SOURCE_PATH):
        if file_name in ['root.json', 'es-419.json']:
            continue
        full_source_path = os.path.join(SOURCE_PATH, file_name)
        full_target_path = os.path.join(TARGET_PATH, file_name.split('.')[0
            ] + '.py')
        full_supplementary_path = os.path.join(SUPPLEMENTARY_PATH, file_name)
        language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        ordered_language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        with open(full_source_path, 'r') as source:
            data = json.load(source)
            try:
                requisite_data = data['rbnf']['rbnf']['SpelloutRules']
            except KeyError:
                logging.error(
                    f'"[\'rbnf\'][\'rbnf\'][\'SpelloutRules\']" doesn\'t exist in {file_name}'
                    )
                continue
            for keys, vals in requisite_data.items():
                if _is_valid(keys):
                    for key, val in vals.items():
                        val = val.replace('\xad', '')
                        _extract_information(key, val, language_data)
        with open(full_supplementary_path, 'r') as supplementary_data:
            data = json.load(supplementary_data)
            for keys in REQUIRED_NUMBERS_DATA:
                language_data[keys].update(data[keys])
                sorted_tuples = sorted(language_data[keys].items(), key=lambda
                    x: (x[1], x[0]))
                for items in sorted_tuples:
                    word, number = items[0], items[1]
                    ordered_language_data[keys][word] = int(number)
        ordered_language_data['SKIP_TOKENS'] = sorted(data['SKIP_TOKENS'])
        try:
            ordered_language_data['USE_LONG_SCALE'] = data['USE_LONG_SCALE']
        except KeyError:
            logging.error(f'long_scale information missing in {file_name}')
        translation_data = json.dumps(ordered_language_data, indent=4,
            ensure_ascii=False)
        translation_data = re.sub('\\bfalse\\b', 'False', translation_data)
        translation_data = re.sub('\\btrue\\b', 'True', translation_data)
        out_text = 'info = ' + translation_data + '\n'
        with open(full_target_path, 'w+') as target_file:
            target_file.write(out_text)


<mask token>

<mask token>
SOURCE_PATH = '../number_parser_data/raw_cldr_translation_data/'
SUPPLEMENTARY_PATH = '../number_parser_data/supplementary_translation_data/'
TARGET_PATH = '../number_parser/data/'
os.chdir(os.path.dirname(os.path.abspath(__file__)))
VALID_KEYS = ['spellout-cardinal', 'spellout-numbering']
INVALID_KEYS = ['cents']
CAPTURE_BRACKET_CONTENT = '\\{(.*?)\\}'
REQUIRED_NUMBERS_DATA = ['UNIT_NUMBERS', 'DIRECT_NUMBERS', 'TENS',
    'HUNDREDS', 'BIG_POWERS_OF_TEN']


def _is_valid(key):
    """Identifying whether the given key of the source language file needs to be extracted."""
    is_valid = False
    for valid_key in VALID_KEYS:
        if valid_key in key:
            is_valid = True
    for invalid_key in INVALID_KEYS:
        if invalid_key in key:
            is_valid = False
    return is_valid


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == 'ERROR' or word == '':
        return
    if number <= 9:
        language_data['UNIT_NUMBERS'][word] = number
    elif number <= 99:
        language_data['DIRECT_NUMBERS'][word] = number


def _count_zero(number):
    """Counting the number of zeroes in the given number."""
    zero_count = 0
    while number > 9:
        if number % 10 == 0:
            zero_count += 1
            number /= 10
        else:
            break
    return zero_count


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == '1':
        return
    if '[' in word:
        root_word = word.split('[')[0].strip()
    else:
        root_word = word.split('>')[0].strip()
    if root_word == '':
        return
    if power_of_10 == 1:
        language_data['TENS'][root_word] = number
    elif power_of_10 == 2:
        language_data['HUNDREDS'][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split('<')[-1]
    if required_part[0] != ' ':
        return
    power_of_10 = _count_zero(number)
    if '$' in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data['BIG_POWERS_OF_TEN'][valid_word
                    ] = power_of_10_num
        return
    if '[' in required_part:
        valid_word = required_part.split('[')[0].strip()
    else:
        valid_word = required_part.split('>')[0].strip()
    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data['BIG_POWERS_OF_TEN'][valid_word] = power_of_10_num


def _extract_information(key, word, language_data):
    """Identify the type of number - simple,compound,multiplier."""
    try:
        number = int(key)
    except ValueError:
        print(f'The given key {key} is not an integer')
        return
    word = word.replace(';', '')
    count_greater_than_sign = word.count('>')
    count_less_than_sign = word.count('<')
    count_equal_to_sign = word.count('=')
    if count_equal_to_sign != 0:
        return
    if count_greater_than_sign == 0 and count_less_than_sign == 0:
        _add_base_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 0:
        _add_compound_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 2:
        _add_multiplier_words(number, word, language_data)


def write_complete_data():
    """
    Main function to extract data from source files , merge with supplementary data
    and write the combined results to the final target directory.
    """
    for file_name in os.listdir(SOURCE_PATH):
        if file_name in ['root.json', 'es-419.json']:
            continue
        full_source_path = os.path.join(SOURCE_PATH, file_name)
        full_target_path = os.path.join(TARGET_PATH, file_name.split('.')[0
            ] + '.py')
        full_supplementary_path = os.path.join(SUPPLEMENTARY_PATH, file_name)
        language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        ordered_language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        with open(full_source_path, 'r') as source:
            data = json.load(source)
            try:
                requisite_data = data['rbnf']['rbnf']['SpelloutRules']
            except KeyError:
                logging.error(
                    f'"[\'rbnf\'][\'rbnf\'][\'SpelloutRules\']" doesn\'t exist in {file_name}'
                    )
                continue
            for keys, vals in requisite_data.items():
                if _is_valid(keys):
                    for key, val in vals.items():
                        val = val.replace('\xad', '')
                        _extract_information(key, val, language_data)
        with open(full_supplementary_path, 'r') as supplementary_data:
            data = json.load(supplementary_data)
            for keys in REQUIRED_NUMBERS_DATA:
                language_data[keys].update(data[keys])
                sorted_tuples = sorted(language_data[keys].items(), key=lambda
                    x: (x[1], x[0]))
                for items in sorted_tuples:
                    word, number = items[0], items[1]
                    ordered_language_data[keys][word] = int(number)
        ordered_language_data['SKIP_TOKENS'] = sorted(data['SKIP_TOKENS'])
        try:
            ordered_language_data['USE_LONG_SCALE'] = data['USE_LONG_SCALE']
        except KeyError:
            logging.error(f'long_scale information missing in {file_name}')
        translation_data = json.dumps(ordered_language_data, indent=4,
            ensure_ascii=False)
        translation_data = re.sub('\\bfalse\\b', 'False', translation_data)
        translation_data = re.sub('\\btrue\\b', 'True', translation_data)
        out_text = 'info = ' + translation_data + '\n'
        with open(full_target_path, 'w+') as target_file:
            target_file.write(out_text)


if __name__ == '__main__':
    write_complete_data()

<mask token>
import json
import logging
import os
import re
SOURCE_PATH = '../number_parser_data/raw_cldr_translation_data/'
SUPPLEMENTARY_PATH = '../number_parser_data/supplementary_translation_data/'
TARGET_PATH = '../number_parser/data/'
os.chdir(os.path.dirname(os.path.abspath(__file__)))
VALID_KEYS = ['spellout-cardinal', 'spellout-numbering']
INVALID_KEYS = ['cents']
CAPTURE_BRACKET_CONTENT = '\\{(.*?)\\}'
REQUIRED_NUMBERS_DATA = ['UNIT_NUMBERS', 'DIRECT_NUMBERS', 'TENS',
    'HUNDREDS', 'BIG_POWERS_OF_TEN']


def _is_valid(key):
    """Identifying whether the given key of the source language file needs to be extracted."""
    is_valid = False
    for valid_key in VALID_KEYS:
        if valid_key in key:
            is_valid = True
    for invalid_key in INVALID_KEYS:
        if invalid_key in key:
            is_valid = False
    return is_valid


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == 'ERROR' or word == '':
        return
    if number <= 9:
        language_data['UNIT_NUMBERS'][word] = number
    elif number <= 99:
        language_data['DIRECT_NUMBERS'][word] = number


def _count_zero(number):
    """Counting the number of zeroes in the given number."""
    zero_count = 0
    while number > 9:
        if number % 10 == 0:
            zero_count += 1
            number /= 10
        else:
            break
    return zero_count


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == '1':
        return
    if '[' in word:
        root_word = word.split('[')[0].strip()
    else:
        root_word = word.split('>')[0].strip()
    if root_word == '':
        return
    if power_of_10 == 1:
        language_data['TENS'][root_word] = number
    elif power_of_10 == 2:
        language_data['HUNDREDS'][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split('<')[-1]
    if required_part[0] != ' ':
        return
    power_of_10 = _count_zero(number)
    if '$' in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data['BIG_POWERS_OF_TEN'][valid_word
                    ] = power_of_10_num
        return
    if '[' in required_part:
        valid_word = required_part.split('[')[0].strip()
    else:
        valid_word = required_part.split('>')[0].strip()
    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data['BIG_POWERS_OF_TEN'][valid_word] = power_of_10_num


def _extract_information(key, word, language_data):
    """Identify the type of number - simple,compound,multiplier."""
    try:
        number = int(key)
    except ValueError:
        print(f'The given key {key} is not an integer')
        return
    word = word.replace(';', '')
    count_greater_than_sign = word.count('>')
    count_less_than_sign = word.count('<')
    count_equal_to_sign = word.count('=')
    if count_equal_to_sign != 0:
        return
    if count_greater_than_sign == 0 and count_less_than_sign == 0:
        _add_base_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 0:
        _add_compound_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 2:
        _add_multiplier_words(number, word, language_data)


def write_complete_data():
    """
    Main function to extract data from source files , merge with supplementary data
    and write the combined results to the final target directory.
    """
    for file_name in os.listdir(SOURCE_PATH):
        if file_name in ['root.json', 'es-419.json']:
            continue
        full_source_path = os.path.join(SOURCE_PATH, file_name)
        full_target_path = os.path.join(TARGET_PATH, file_name.split('.')[0
            ] + '.py')
        full_supplementary_path = os.path.join(SUPPLEMENTARY_PATH, file_name)
        language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        ordered_language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        with open(full_source_path, 'r') as source:
            data = json.load(source)
            try:
                requisite_data = data['rbnf']['rbnf']['SpelloutRules']
            except KeyError:
                logging.error(
                    f'"[\'rbnf\'][\'rbnf\'][\'SpelloutRules\']" doesn\'t exist in {file_name}'
                    )
                continue
            for keys, vals in requisite_data.items():
                if _is_valid(keys):
                    for key, val in vals.items():
                        val = val.replace('\xad', '')
                        _extract_information(key, val, language_data)
        with open(full_supplementary_path, 'r') as supplementary_data:
            data = json.load(supplementary_data)
            for keys in REQUIRED_NUMBERS_DATA:
                language_data[keys].update(data[keys])
                sorted_tuples = sorted(language_data[keys].items(), key=lambda
                    x: (x[1], x[0]))
                for items in sorted_tuples:
                    word, number = items[0], items[1]
                    ordered_language_data[keys][word] = int(number)
        ordered_language_data['SKIP_TOKENS'] = sorted(data['SKIP_TOKENS'])
        try:
            ordered_language_data['USE_LONG_SCALE'] = data['USE_LONG_SCALE']
        except KeyError:
            logging.error(f'long_scale information missing in {file_name}')
        translation_data = json.dumps(ordered_language_data, indent=4,
            ensure_ascii=False)
        translation_data = re.sub('\\bfalse\\b', 'False', translation_data)
        translation_data = re.sub('\\btrue\\b', 'True', translation_data)
        out_text = 'info = ' + translation_data + '\n'
        with open(full_target_path, 'w+') as target_file:
            target_file.write(out_text)


if __name__ == '__main__':
    write_complete_data()

"""
The raw CLDR data was retreived on 25th Jun , 2020 from the following link
https://github.com/unicode-cldr/cldr-rbnf
"""

import json
import logging
import os
import re

SOURCE_PATH = "../number_parser_data/raw_cldr_translation_data/"
SUPPLEMENTARY_PATH = "../number_parser_data/supplementary_translation_data/"
TARGET_PATH = "../number_parser/data/"


os.chdir(os.path.dirname(os.path.abspath(__file__)))


VALID_KEYS = ["spellout-cardinal", "spellout-numbering"]
INVALID_KEYS = ["cents"]
CAPTURE_BRACKET_CONTENT = r"\{(.*?)\}"
REQUIRED_NUMBERS_DATA = [
    "UNIT_NUMBERS",
    "DIRECT_NUMBERS",
    "TENS",
    "HUNDREDS",
    "BIG_POWERS_OF_TEN",
]


def _is_valid(key):
    """Identifying whether the given key of the source language file needs to be extracted."""
    is_valid = False
    for valid_key in VALID_KEYS:
        if valid_key in key:
            is_valid = True
    for invalid_key in INVALID_KEYS:
        if invalid_key in key:
            is_valid = False
    return is_valid


def _add_base_words(number, word, language_data):
    """Adding basic two digit words - i.e words that are standalone numbers on their own."""
    if word == "ERROR" or word == "":
        return
    if number <= 9:
        language_data["UNIT_NUMBERS"][word] = number
    elif number <= 99:
        language_data["DIRECT_NUMBERS"][word] = number


def _count_zero(number):
    """Counting the number of zeroes in the given number."""
    zero_count = 0
    while number > 9:
        if number % 10 == 0:
            zero_count += 1
            number /= 10
        else:
            break
    return zero_count


def _add_compound_words(number, word, language_data):
    """Adding compound words - i.e words that may be used along with another word to form a number"""
    power_of_10 = _count_zero(number)
    first_dig = str(number)[0]
    if power_of_10 == 0 or first_dig == "1":
        return

    if "[" in word:
        root_word = word.split("[")[0].strip()
    else:
        root_word = word.split(">")[0].strip()

    if root_word == "":
        return

    if power_of_10 == 1:
        language_data["TENS"][root_word] = number
    elif power_of_10 == 2:
        language_data["HUNDREDS"][root_word] = number


def _add_multiplier_words(number, word, language_data):
    """Adding words that are large powers of 10 which are used to form bigger numbers."""
    required_part = word.split("<")[-1]
    if required_part[0] != " ":
        return

    power_of_10 = _count_zero(number)
    if "$" in required_part:
        valid_words = re.findall(CAPTURE_BRACKET_CONTENT, required_part)
        for valid_word in valid_words:
            power_of_10_num = pow(10, power_of_10)
            if power_of_10 >= 2:
                language_data["BIG_POWERS_OF_TEN"][valid_word] = power_of_10_num
        return
    if "[" in required_part:
        valid_word = required_part.split("[")[0].strip()
    else:
        valid_word = required_part.split(">")[0].strip()

    power_of_10_num = pow(10, power_of_10)
    if power_of_10 >= 2:
        language_data["BIG_POWERS_OF_TEN"][valid_word] = power_of_10_num


def _extract_information(key, word, language_data):
    """Identify the type of number - simple,compound,multiplier."""
    try:
        number = int(key)
    except ValueError:
        print(f"The given key {key} is not an integer")
        return

    word = word.replace(";", "")
    count_greater_than_sign = word.count(">")
    count_less_than_sign = word.count("<")
    count_equal_to_sign = word.count("=")
    if count_equal_to_sign != 0:
        return
    if count_greater_than_sign == 0 and count_less_than_sign == 0:
        _add_base_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 0:
        _add_compound_words(number, word, language_data)
    elif count_greater_than_sign == 2 and count_less_than_sign == 2:
        _add_multiplier_words(number, word, language_data)


def write_complete_data():
    """
    Main function to extract data from source files , merge with supplementary data
    and write the combined results to the final target directory.
    """
    for file_name in os.listdir(SOURCE_PATH):
        if file_name in ["root.json", "es-419.json"]:
            # "root" is not a language, "es-419" doesn't contain spell-out rules
            continue
        full_source_path = os.path.join(SOURCE_PATH, file_name)
        full_target_path = os.path.join(TARGET_PATH, file_name.split(".")[0] + ".py")
        full_supplementary_path = os.path.join(SUPPLEMENTARY_PATH, file_name)

        language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        ordered_language_data = {key: {} for key in REQUIRED_NUMBERS_DATA}
        with open(full_source_path, "r") as source:
            data = json.load(source)
            try:
                requisite_data = data["rbnf"]["rbnf"]["SpelloutRules"]
            except KeyError:
                logging.error(
                    f"\"['rbnf']['rbnf']['SpelloutRules']\" doesn't exist in {file_name}"
                )
                continue

            for keys, vals in requisite_data.items():
                if _is_valid(keys):
                    for key, val in vals.items():
                        # Removing soft-hyphens from the source file.
                        val = val.replace("\xad", "")
                        _extract_information(key, val, language_data)

        with open(full_supplementary_path, "r") as supplementary_data:
            data = json.load(supplementary_data)
            for keys in REQUIRED_NUMBERS_DATA:
                language_data[keys].update(data[keys])
                sorted_tuples = sorted(
                    language_data[keys].items(), key=lambda x: (x[1], x[0])
                )
                for items in sorted_tuples:
                    word, number = items[0], items[1]
                    ordered_language_data[keys][word] = int(number)
        ordered_language_data["SKIP_TOKENS"] = sorted(data["SKIP_TOKENS"])
        try:
            ordered_language_data["USE_LONG_SCALE"] = data["USE_LONG_SCALE"]
        except KeyError:
            logging.error(f"long_scale information missing in {file_name}")

        translation_data = json.dumps(
            ordered_language_data, indent=4, ensure_ascii=False
        )
        # Overwriting boolean value with capitalized form
        translation_data = re.sub(r"\bfalse\b", "False", translation_data)
        translation_data = re.sub(r"\btrue\b", "True", translation_data)
        out_text = "info = " + translation_data + "\n"
        with open(full_target_path, "w+") as target_file:
            target_file.write(out_text)


if __name__ == "__main__":
    write_complete_data()
