-
Notifications
You must be signed in to change notification settings - Fork 0
/
speaker.py
132 lines (110 loc) · 4.47 KB
/
speaker.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
126
127
128
129
130
131
132
import re
class Speaker(object):
"""
Class for spelling moves and some texts
"""
piece_names = {
'K': {'ru': 'Король', 'en': 'King'},
'Q': {'ru': 'Ферзь', 'en': 'Queen'},
'R': {'ru': 'Ладья', 'en': 'Rock'},
'N': {'ru': 'Конь', 'en': 'Knight'},
'B': {'ru': 'Слон', 'en': 'Bishop'},
'p': {'ru': 'Пешка', 'en': 'Pawn'}
}
letters_for_pronunciation = {
'ru': {'a': 'а', 'b': 'бэ', 'c': 'цэ', 'd': 'дэ', 'e': 'е', 'f': 'эф',
'g': 'же', 'h': 'аш'}}
check = {'ru': 'шах', 'en': 'check'}
mate = {'ru': 'мат', 'en': 'mate'}
checkmate_names = {
'+': {'ru': 'шах', 'en': 'check'},
'#': {'ru': 'мат', 'en': 'mate'}
}
captures_names = {'ru': 'берёт', 'en': 'capture'}
promotions_names = {'ru': 'превращение в', 'en': 'promotion to'}
castling_names = {
'0-0': {'ru': 'Короткая рокировка', 'en': 'Kingside castling'},
'0-0-0': {'ru': 'Длинная рокировка', 'en': 'Queenside castling'}
}
white_black_names = {
'White': {'ru': 'Белые', 'en': 'White'},
'Black': {'ru': 'Черные', 'en': 'Black'}
}
gameover_reasons = {
'#': {'ru': 'мат', 'en': 'mate'},
'=': {'ru': 'пат', 'en': 'stalemate'},
'5': {'ru': 'ничья из-за 5 повторов', 'en': 'fivefold repetition'},
'insufficient': {'ru': 'ничья из-за недостаточности материала',
'en': 'draw due to insufficient material'}
}
def __init__(self):
pass
def _castling_pron_(self, move_san, lang='ru'):
# returns castling pronunciation in specified language
res = ''
castle_index = move_san.replace('O', '0')
castlings = self.castling_names.get(castle_index, None)
if castlings is not None:
res = castlings.get(lang, '')
return res
def _file_pron_(self, file, lang='ru'):
# returns file (column) pronunciation in specified language
res = file
letters_set = self.letters_for_pronunciation.get(lang, None)
if letters_set is not None:
res = letters_set.get(file, file)
return res
def _piece_pron_(self, piece, lang='ru'):
# return name on lang by piece symbol
res = ''
piece_name = self.piece_names.get(piece, None)
if piece_name is not None:
res = piece_name.get(lang, '')
return res
def _checkmate_pron_(self, cm_type, lang='ru'):
# returns check or mate pronunciation in specified language
# todo: stalemate pronunciation
res = ''
checkmates = self.checkmate_names.get(cm_type, None)
if checkmates is not None:
res = checkmates.get(lang, '')
return res
def _capture_pron_(self, lang='ru'):
# returns capture pronunciation in specified language
return self.captures_names.get(lang, '')
def say_move(self, move_san, lang='ru'):
speak_list = []
regex_body = r'[a-h]|[1-8]|x|[KQRBN]|[+#]|0-0-0|0-0|O-O-O|O-O'
move_regex = re.compile(regex_body)
for sym in re.finditer(move_regex, move_san):
if '0-0' in sym[0] or 'O-O' in sym[0]:
# castling
speak_list.append(self._castling_pron_(sym[0], lang))
elif 'a' <= sym[0] <= 'h':
# file
speak_list.append(self._file_pron_(sym[0], lang))
elif '1' <= sym[0] <= '8':
# rank
speak_list.append(sym[0])
elif sym[0] in 'KQRBN':
# piece
speak_list.append(self._piece_pron_(sym[0], lang))
elif sym[0] in 'x':
# capture
speak_list.append(self._capture_pron_(lang))
elif sym[0] in '+#':
# check or mate
speak_list.append(self._checkmate_pron_(sym[0], lang))
return ' '.join(speak_list)
def say_turn(self, who, lang='ru'):
res = ''
turn_names = self.white_black_names.get(who, None)
if turn_names is not None:
res = turn_names.get(lang, '')
return res
def say_reason(self, reason, lang='ru'):
res = ''
reasons = self.gameover_reasons.get(reason, None)
if reasons is not None:
res = reasons.get(lang, '')
return res