1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Model of LOD database for Telegram""" |
3
|
|
|
|
4
|
|
|
from collections import defaultdict |
5
|
|
|
|
6
|
|
|
from loglan_core import Definition |
7
|
|
|
from loglan_core.addons.definition_selector import DefinitionSelector |
8
|
|
|
|
9
|
|
|
from app.engine import async_session_maker |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def export(definition: Definition) -> str: |
13
|
|
|
""" |
14
|
|
|
Convert definition's data to str for sending as a telegram messages |
15
|
|
|
:return: Adopted for posting in telegram string |
16
|
|
|
""" |
17
|
|
|
d_usage = ( |
18
|
|
|
f"<b>{definition.usage.replace('%', '—')}</b> " if definition.usage else "" |
19
|
|
|
) |
20
|
|
|
d_body = ( |
21
|
|
|
definition.body.replace("<", "<") |
22
|
|
|
.replace(">", ">") |
23
|
|
|
.replace("«", "<i>") |
24
|
|
|
.replace("»", "</i>") |
25
|
|
|
.replace("{", "<code>") |
26
|
|
|
.replace("}", "</code>") |
27
|
|
|
.replace("....", "….") |
28
|
|
|
.replace("...", "…") |
29
|
|
|
.replace("--", "—") |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
d_case_tags = f" [{definition.case_tags}]" if definition.case_tags else "" |
33
|
|
|
return f"{d_usage}{definition.grammar} {d_body}{d_case_tags}" |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def format_affixes(word): |
37
|
|
|
return ( |
38
|
|
|
f" ({' '.join([w.name for w in word.affixes]).strip()})" if word.affixes else "" |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def format_year(word): |
43
|
|
|
return "'" + str(word.year.year)[-2:] + " " if word.year else "" |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def format_origin(word): |
47
|
|
|
if word.origin or word.origin_x: |
48
|
|
|
return ( |
49
|
|
|
f"\n<i><{word.origin}" |
50
|
|
|
f"{' = ' + word.origin_x if word.origin_x else ''}></i>" |
51
|
|
|
) |
52
|
|
|
return "" |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def format_authors(word): |
56
|
|
|
return ( |
57
|
|
|
"/".join([a.abbreviation for a in word.authors]) + " " if word.authors else "" |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def format_rank(word): |
62
|
|
|
return word.rank + " " if word.rank else "" |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def format_definitions(word): |
66
|
|
|
return "\n\n".join([export(d) for d in word.definitions]) |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def export_as_str(word) -> str: |
70
|
|
|
""" |
71
|
|
|
Convert word's data to str for sending as a telegram messages |
72
|
|
|
:return: List of str with technical info, definitions, used_in part |
73
|
|
|
""" |
74
|
|
|
w_affixes = format_affixes(word) |
75
|
|
|
w_match = word.match + " " if word.match else "" |
76
|
|
|
w_year = format_year(word) |
77
|
|
|
w_orig = format_origin(word) |
78
|
|
|
w_authors = format_authors(word) |
79
|
|
|
w_type = word.type.type_ + " " |
80
|
|
|
w_rank = format_rank(word) |
81
|
|
|
|
82
|
|
|
word_str = ( |
83
|
|
|
f"<b>{word.name}</b>{w_affixes}," |
84
|
|
|
f"\n{w_match}{w_type}{w_authors}{w_year}{w_rank}{w_orig}" |
85
|
|
|
) |
86
|
|
|
w_definitions = format_definitions(word) |
87
|
|
|
return f"{word_str}\n\n{w_definitions}" |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
async def translation_by_key(request: str, language: str = None) -> str: |
91
|
|
|
""" |
92
|
|
|
We get information about loglan words by key in a foreign language |
93
|
|
|
:param request: Requested string |
94
|
|
|
:param language: Key language |
95
|
|
|
:return: Search results string formatted for sending to Telegram |
96
|
|
|
""" |
97
|
|
|
|
98
|
|
|
result = defaultdict(list) |
99
|
|
|
async with async_session_maker() as session: |
100
|
|
|
definitions = await ( |
101
|
|
|
DefinitionSelector() |
102
|
|
|
.by_key(key=request, language=language) |
103
|
|
|
.with_relationships("source_word") |
104
|
|
|
).all_async(session) |
105
|
|
|
for definition in definitions: |
106
|
|
|
result[definition.source_word.name].append(export(definition)) |
107
|
|
|
|
108
|
|
|
new = "\n" |
109
|
|
|
word_items = [ |
110
|
|
|
f"/{word_name},\n{new.join(definitions)}\n" |
111
|
|
|
for word_name, definitions in result.items() |
112
|
|
|
] |
113
|
|
|
return new.join(word_items).strip() |
114
|
|
|
|