-
Notifications
You must be signed in to change notification settings - Fork 0
/
latynkojib_bot.py
125 lines (107 loc) · 2.76 KB
/
latynkojib_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import re
from collections import OrderedDict
from functools import reduce
cli_mode = len(sys.argv) > 1
if not cli_mode:
import telebot
bot = telebot.TeleBot("your_token")
latynko_soft_digraph_dict = OrderedDict([
('cj', 'шь'),
('gj', 'жь'),
('tj', 'ть'),
('dj', 'дь'),
('lj', 'ль'),
('nj', 'нь'),
('rj', 'рь'),
('sj', 'сь'),
('zj', 'зь'),
('tsj', 'ць'),
('tcj', 'чь'),
])
latynko_apostrophe_digraph_dict = OrderedDict([
('bj', 'бʼj'),
('pj', 'пʼj'),
('qj', 'ґʼj'),
('kj', 'кʼj'),
('fj', 'фʼj'),
('vj', 'вʼj'),
('wj', 'ввʼj'),
('hj', 'гʼj'),
('xj', 'хʼj'),
('mj', 'мʼj'),
('svja', 'свя'), # *ь non-normal // tbh no need, but current OG-fags
('tsvja', 'цвя'),
('dzvja', 'дзвя'),
('tjmja', 'тьмя'),
])
latynko_jotted_digraph_dict = OrderedDict([
('ja', 'я'),
('je', 'є'),
('ji', 'ї'),
('jy', 'ї'),
('ju', 'ю'),
])
latynko_letter_dict = OrderedDict([
('ctc', 'щ'),
('tc', 'ч'),
('ts', 'ц'),
('a', 'а'),
('b', 'б'),
('c', 'ш'),
('d', 'д'),
('e', 'е'),
('f', 'ф'),
('g', 'ж'),
('h', 'г'),
('i', 'і'),
('j', 'й'),
('k', 'к'),
('l', 'л'),
('m', 'м'),
('n', 'н'),
('o', 'о'),
('p', 'п'),
('q', 'ґ'),
('r', 'р'),
('s', 'с'),
('t', 'т'),
('u', 'у'),
('v', 'в'),
('w', 'вв'),
('x', 'х'),
('y', 'и'),
('z', 'з'),
('\'', 'ʼ'),
])
patterns_dicts = [(re.compile("(%s)" % '|'.join(dict.keys())), dict) for dict in (
latynko_apostrophe_digraph_dict,
latynko_jotted_digraph_dict,
latynko_soft_digraph_dict,
latynko_letter_dict,
)]
def is_latynka(str):
return re.search(r"[a-z\']", str)
def xlate(s, pattern_dict):
regex, dict = pattern_dict
return regex.sub(lambda x: dict[x.group()], s)
def xlate_all(s):
return reduce(xlate, patterns_dicts, s)
if cli_mode:
latynka = str(sys.argv[1]).lower()
if is_latynka(latynka):
kyrylka = xlate_all(latynka)
print(kyrylka)
else:
@bot.message_handler(content_types=['text', 'photo'])
@bot.edited_message_handler(content_types=['text', 'photo'])
def reply(message):
if message.content_type == 'text':
latynka = message.text
else:
latynka = message.caption
latynka = latynka.lower()
if is_latynka(latynka):
kyrylka = xlate_all(latynka)
bot.send_message(message.chat.id, kyrylka)
bot.polling()