Better alphabet access

This commit is contained in:
Tilman Kamp 2019-10-23 15:10:08 +02:00
parent 3424ab2b5d
commit 010f24578f
2 changed files with 5 additions and 8 deletions

View File

@ -159,13 +159,7 @@ def read_token(token):
def in_alphabet(alphabet, c):
if alphabet is None:
return False
try:
alphabet.label_from_string(c)
return True
except KeyError:
return False
return True if alphabet is None else alphabet.has_char(c)
alphabets = {}
@ -200,7 +194,7 @@ def label_filter(label, language):
.encode("ascii", "ignore")
.decode("ascii", "ignore"))
for sc in c:
if alphabet is not None and not in_alphabet(alphabet, sc):
if not in_alphabet(alphabet, sc):
return None, 'illegal character'
chars.append(sc)
label = ''.join(chars)

View File

@ -36,6 +36,9 @@ class Alphabet(object):
'then add all these to data/alphabet.txt.'.format(string)
).with_traceback(e.__traceback__)
def has_char(self, char):
return char in self._str_to_label
def encode(self, string):
res = []
for char in string: