Convert between text-label and text-index.
| 6 | |
| 7 | |
| 8 | class BaseRecLabelEncode(object): |
| 9 | """Convert between text-label and text-index.""" |
| 10 | |
| 11 | def __init__( |
| 12 | self, |
| 13 | max_text_length, |
| 14 | character_dict_path=None, |
| 15 | use_space_char=False, |
| 16 | lower=False, |
| 17 | ): |
| 18 | self.max_text_len = max_text_length |
| 19 | self.beg_str = 'sos' |
| 20 | self.end_str = 'eos' |
| 21 | self.lower = lower |
| 22 | self.reverse = False |
| 23 | if character_dict_path is None: |
| 24 | logger = get_logger() |
| 25 | logger.warning( |
| 26 | 'The character_dict_path is None, model can only recognize number and lower letters' |
| 27 | ) |
| 28 | self.character_str = '0123456789abcdefghijklmnopqrstuvwxyz' |
| 29 | dict_character = list(self.character_str) |
| 30 | self.lower = True |
| 31 | else: |
| 32 | self.character_str = [] |
| 33 | with open(character_dict_path, 'rb') as fin: |
| 34 | lines = fin.readlines() |
| 35 | for line in lines: |
| 36 | line = line.decode('utf-8').strip('\n').strip('\r\n') |
| 37 | self.character_str.append(line) |
| 38 | if use_space_char: |
| 39 | self.character_str.append(' ') |
| 40 | dict_character = list(self.character_str) |
| 41 | if 'arabic' in character_dict_path: |
| 42 | self.reverse = True |
| 43 | dict_character = self.add_special_char(dict_character) |
| 44 | self.dict = {} |
| 45 | for i, char in enumerate(dict_character): |
| 46 | self.dict[char] = i |
| 47 | self.character = dict_character |
| 48 | |
| 49 | def label_reverse(self, text): |
| 50 | text_re = [] |
| 51 | c_current = '' |
| 52 | for c in text: |
| 53 | if not bool(re.search('[a-zA-Z0-9 :*./%+١٢٣٤٥٦٧٨٩٠-]', c)): |
| 54 | if c_current != '': |
| 55 | text_re.append(c_current) |
| 56 | text_re.append(c) |
| 57 | c_current = '' |
| 58 | else: |
| 59 | c_current += c |
| 60 | if c_current != '': |
| 61 | text_re.append(c_current) |
| 62 | |
| 63 | return ''.join(text_re[::-1]) |
| 64 | |
| 65 | def add_special_char(self, dict_character): |
nothing calls this directly
no outgoing calls
no test coverage detected