-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscoder.py
67 lines (58 loc) · 1.96 KB
/
transcoder.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
__author__ = "federico"
__date__ = "$Feb 27, 2024 2:28:51 PM$"
class Transcoder(object):
"""Transcoder"""
def __init__(self, trans_map_fn):
"""Init"""
self.trans_map = None
self.max_span_len = None
self.populate_trans_map(trans_map_fn)
def populate_trans_map(self, trans_file_name):
"""Populate the transcoding map"""
self.max_span_len = 0
self.trans_map = {}
with open(trans_file_name, encoding='UTF-8') as trans_file:
for line in trans_file:
if line.startswith('//'): continue
try:
chunk1, chunk2 = line.split('\t')
if len(chunk1) > self.max_span_len:
self.max_span_len = len(chunk1)
self.trans_map[chunk1] = chunk2[:-1]
except:
pass
def transcode(self, str):
"""Transcode a string"""
in_str = [str[:]]
out_str = []
if in_str[0] == '':
return out_str
for i in range(0, self.max_span_len):
in_str.append(' ')
in_str = ''.join(in_str)
in_str_len = len(in_str)
i_left = 0
i_right = self.max_span_len
while i_right <= in_str_len:
while i_right > i_left:
try:
frag = in_str[i_left:i_right]
code = self.trans_map[frag]
code =re.sub(r'_',' ',code)
if code != '#*#':
out_str.append(code)
break
except:
#pass
if i_right-i_left == 1:
out_str.append(frag)
i_right -= 1
if i_right == i_left:
i_right += 1
i_left = i_right
i_right += self.max_span_len
out_str = ''.join(out_str)
return out_str