| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- from .translation import translation
- class phrasebook:
- """ It store single collection of phrases.
-
- This is responsible for searching phrases in the phrasebook, or when
- object exists, then also in the object notation.
- Methods
- -------
- tr(phrase: str) -> translation
- Translate phrase, short version of translate.
- translate(phrase: str) -> translation
- Translate phrase.
- prepare(phrase: str) -> str
- This prepare phrase, remove dots and white chars.
- """
- def __init__(self, phrases: dict, objects: dict|None = None) -> None:
- """ This initialize new phrasebook.
- It require phrases dict and also objects for objects notation. Objects
- is optional, could be leave empty.
- Parameters
- ----------
- phrases : dict
- Dictionary with phrases.
-
- objects : dict | None, default: None
- Objects to take phrases for object notation.
- """
- self.__phrases = self.__parse_phrases(phrases)
- self.__objects = objects
- def __parse_phrases(self, phrases: dict) -> dict:
- """ This prepare phrasebook, by flattending dictionary keys.
- Parameters
- ----------
- phrases : dict
- Dictionary to parse
- Returns
- -------
- dict
- Flattened dictionary with phrases.
- """
- flattened = dict()
- for key in phrases.keys():
- flat_key = phrasebook.prepare(key)
- if flat_key in flattened:
- raise TypeError("Key \"" + flat_key + "\" exists twice.")
- flattened[flat_key] = phrases[key]
- return flattened
- def tr(self, phrase: str) -> translation:
- """ This translate phrase, from phrases or objects.
- Parameters
- ----------
- phrase : str
- Phrase to translate.
- Returns
- -------
- translation
- Translated phrase.
- """
- return self.translate(phrase)
- def translate(self, phrase: str) -> translation:
- """ This translate phrase, from phrases or objects.
- Parameters
- ----------
- phrase : str
- Phrase to translate.
- Returns
- -------
- translation
- Translated phrase.
- """
-
- if self.__is_nested(phrase):
- return self.__translate_nested(phrase)
- return self.__translate_flat(phrasebook.prepare(phrase))
- def __translate_nested(self, phrase: str) -> translation:
- """ This translate nested phrase.
-
- This transalate nested phrase, that mean phrase in object
- notation. When objects is not set, then return phrase as
- failed translation.
- Parameters
- ----------
- phrase : str
- Nested phrase to translate.
- Raises
- ------
- SyntaxError
- When two dots '..' found in phrase.
- Returns
- -------
- translation
- Translated nested phrase.
- """
- if phrase.find("..") != -1:
- raise SyntaxError("Symbol \"..\" in \"" + phrase + "\".")
- if self.__objects is None:
- return translation(phrase, False)
- parts = phrase.split(".")
- current = self.__objects
- for part in parts:
- if type(current) is not dict:
- return translation(phrase, False)
- if not part in current:
- return translation(phrase, False)
-
- current = current[part]
- if type(current) is str:
- return translation(current, True)
- return translation(phrase, False)
- def __is_nested(self, phrase: str) -> bool:
- """ This check that phrase is nested or not.
-
- When phrase contain white chars, or dot only as last chat, then
- it is not nested. When phrase contain dot, then phrase is nested.
- Parameters
- ----------
- phrase : str
- Phrase to check.
- Returns
- -------
- bool
- True when phrase is nested, False if not.
- """
- if phrase.find(" ") != -1:
- return False
- if phrase[-1] == ".":
- return False
- return phrase.find(".") != -1
- def __translate_flat(self, phrase: str) -> translation:
- """ This translate standard flat phrase.
- Parameters
- ----------
- phrase : str
- Phrase to translate.
- Returns
- -------
- translation
- Translation of the phrase.
- """
- if phrase in self.__phrases:
- return translation(self.__phrases[phrase], True)
- return translation(phrase, False)
- def prepare(phrase: str) -> str:
- """ This prepare phrase to being phrasebook dict key.
-
- Parameters
- ----------
- phrase : str
- Phrase to translate.
- Returns
- -------
- str
- Prepared phrase.
- """
- return phrase.lower().replace(" ", "_").replace(".", "")
|