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:

  • keys: one-word str

  • values: list of the words (str) that are a proper translation of the associated key.

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

given langD (dict) a language dictionary as described above

returns a new dict a language dictionary where:

  1. The keys are all the words (str) in the langD values.

  2. The value associated to each key is a list with all the words that are a proper translation of the key according to langD. The words must be alphabetically ordered.

The input dict should not be modified.

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 above

returns a new dict a language dictionary where:

  1. The keys are single letters (str) that are the first letter of a word in the langD values.

  2. The value associated to each key is a dict where the keys are the works in the values of langD stating with that letter and the values is a list of the words that are a proper translation of the word according to langD. The words must be alphabetically ordered.

The input dict should 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.