phrasebook.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. from .translation import translation
  2. class phrasebook:
  3. """ It store single collection of phrases.
  4. This is responsible for searching phrases in the phrasebook, or when
  5. object exists, then also in the object notation.
  6. Methods
  7. -------
  8. tr(phrase: str) -> translation
  9. Translate phrase, short version of translate.
  10. translate(phrase: str) -> translation
  11. Translate phrase.
  12. prepare(phrase: str) -> str
  13. This prepare phrase, remove dots and white chars.
  14. """
  15. def __init__(self, phrases: dict, objects: dict|None = None) -> None:
  16. """ This initialize new phrasebook.
  17. It require phrases dict and also objects for objects notation. Objects
  18. is optional, could be leave empty.
  19. Parameters
  20. ----------
  21. phrases : dict
  22. Dictionary with phrases.
  23. objects : dict | None, default: None
  24. Objects to take phrases for object notation.
  25. """
  26. self.__phrases = self.__parse_phrases(phrases)
  27. self.__objects = objects
  28. def __parse_phrases(self, phrases: dict) -> dict:
  29. """ This prepare phrasebook, by flattending dictionary keys.
  30. Parameters
  31. ----------
  32. phrases : dict
  33. Dictionary to parse
  34. Returns
  35. -------
  36. dict
  37. Flattened dictionary with phrases.
  38. """
  39. flattened = dict()
  40. for key in phrases.keys():
  41. flat_key = phrasebook.prepare(key)
  42. if flat_key in flattened:
  43. raise TypeError("Key \"" + flat_key + "\" exists twice.")
  44. flattened[flat_key] = phrases[key]
  45. return flattened
  46. def tr(self, phrase: str) -> translation:
  47. """ This translate phrase, from phrases or objects.
  48. Parameters
  49. ----------
  50. phrase : str
  51. Phrase to translate.
  52. Returns
  53. -------
  54. translation
  55. Translated phrase.
  56. """
  57. return self.translate(phrase)
  58. def translate(self, phrase: str) -> translation:
  59. """ This translate phrase, from phrases or objects.
  60. Parameters
  61. ----------
  62. phrase : str
  63. Phrase to translate.
  64. Returns
  65. -------
  66. translation
  67. Translated phrase.
  68. """
  69. if self.__is_nested(phrase):
  70. return self.__translate_nested(phrase)
  71. return self.__translate_flat(phrasebook.prepare(phrase))
  72. def __translate_nested(self, phrase: str) -> translation:
  73. """ This translate nested phrase.
  74. This transalate nested phrase, that mean phrase in object
  75. notation. When objects is not set, then return phrase as
  76. failed translation.
  77. Parameters
  78. ----------
  79. phrase : str
  80. Nested phrase to translate.
  81. Raises
  82. ------
  83. SyntaxError
  84. When two dots '..' found in phrase.
  85. Returns
  86. -------
  87. translation
  88. Translated nested phrase.
  89. """
  90. if phrase.find("..") != -1:
  91. raise SyntaxError("Symbol \"..\" in \"" + phrase + "\".")
  92. if self.__objects is None:
  93. return translation(phrase, False)
  94. parts = phrase.split(".")
  95. current = self.__objects
  96. for part in parts:
  97. if type(current) is not dict:
  98. return translation(phrase, False)
  99. if not part in current:
  100. return translation(phrase, False)
  101. current = current[part]
  102. if type(current) is str:
  103. return translation(current, True)
  104. return translation(phrase, False)
  105. def __is_nested(self, phrase: str) -> bool:
  106. """ This check that phrase is nested or not.
  107. When phrase contain white chars, or dot only as last chat, then
  108. it is not nested. When phrase contain dot, then phrase is nested.
  109. Parameters
  110. ----------
  111. phrase : str
  112. Phrase to check.
  113. Returns
  114. -------
  115. bool
  116. True when phrase is nested, False if not.
  117. """
  118. if phrase.find(" ") != -1:
  119. return False
  120. if phrase[-1] == ".":
  121. return False
  122. return phrase.find(".") != -1
  123. def __translate_flat(self, phrase: str) -> translation:
  124. """ This translate standard flat phrase.
  125. Parameters
  126. ----------
  127. phrase : str
  128. Phrase to translate.
  129. Returns
  130. -------
  131. translation
  132. Translation of the phrase.
  133. """
  134. if phrase in self.__phrases:
  135. return translation(self.__phrases[phrase], True)
  136. return translation(phrase, False)
  137. def prepare(phrase: str) -> str:
  138. """ This prepare phrase to being phrasebook dict key.
  139. Parameters
  140. ----------
  141. phrase : str
  142. Phrase to translate.
  143. Returns
  144. -------
  145. str
  146. Prepared phrase.
  147. """
  148. return phrase.lower().replace(" ", "_").replace(".", "")