Language Dictionary¶
Consider a Language dictionary meaning a dict with the translation from words of one language to words of another language. Let’s call it a as has the following format:
For example, see few words of a english-catalan dictionary:
>>> eng_catD = { ... 'balance': ['equilibri', 'romanent', 'saldo'], ... 'swing': ['gronxar', 'gronxador', 'balencejar'], ... 'residue': ['residu', 'saldo'], ... 'rock': ['roca', 'pedra', 'roc', 'balencejar'], ... 'stone': ['pedra',], ... }
You are required to implement the following functions and deliver them in the same module language_dictionary (file language_dictionary.py).
Their relative weight is 2/3 and 1/3 respectively.
————————T————————————————————————
The first function is:
- invert_lang_dict(langD)¶
such that
For example, the output of the above english-catalan dictionary would be:
>>> d = invert_lang_dict(eng_catD) >>> solD = { ... 'equilibri': ['balance'], ... 'romanent': ['balance'], ... 'saldo': ['balance', 'residue'], ... 'gronxar': ['swing'], ... 'gronxador': ['swing'], ... 'balencejar': ['rock', 'swing'], ... 'residu': ['residue'], ... 'roca': ['rock'], ... 'pedra': ['rock', 'stone'], 'roc': ['rock'] ... }
Doctests are available in the invert_lang_dict.test file.
The second function is:
- invert_lang_dict_plus(langD)¶
such that
given langD (
dict) a language dictionary as described abovereturns a new
dicta language dictionary where:
The keys are single letters (
str) that are the first letter of a word in the langD values.The value associated to each key is a
dictwhere the keys are the works in the values of langD stating with that letter and the values is alistof the words that are a proper translation of the word according to langD. The words must be alphabetically ordered.The input
dictshould not be modified.
For example, the output of the above english-catalan dictionary would be:
>>> d = invert_lang_dict_plus(eng_catD) >>> solD = { ... 'e': {'equilibri': ['balance']}, ... 'r': {'romanent': ['balance'], 'residu': ['residue'], 'roca': ['rock'], 'roc': ['rock']}, ... 's': {'saldo': ['balance', 'residue']}, ... 'g': {'gronxar': ['swing'], 'gronxador': ['swing']}, ... 'b': {'balencejar': ['rock', 'swing']}, ... 'p': {'pedra': ['rock', 'stone']}, ... }
Doctests are available in the invert_lang_dict_plus.test file.