Tags: python 

Rating:

I converted the provided C code into a python dictionary and then I write down a little python script based on the examples provided:
```
file_name = 'sms4.csv'
pause_time = 1000

keys_map = {
-1: [''],
0: " 0",
1: ".,'?!\"1-()@/:",
2: "abc2",
3: "def3",
4: "ghi4",
5: "jkl5",
6: "mno6",
7: "pqrs7",
8: "tuv8",
9: "wxyz9",
10: "@/:_;+&%*[]{}",
11: [
'[IME_T9_CAPS]',
'[IME_ABC]',
'[IME_ABC_CAPS]',
'[IME_T9]', ],
100: ['[left'],
101: ['[right'],
102: ['[up'],
103: ['[down'],
104: ['[accept'],
105: ['[reject'],
}

message = []
with open(file_name) as fp:
old_key = -1
key_count = 0
old_time = 0
cursor = 0
for line in fp:
line = line.replace('\n', '')
curr_time = int(line.split(',')[0])
curr_key = int(line.split(',')[1])
# print(str(curr_time - old_time))
if curr_key != old_key or (curr_time - old_time) > pause_time:
# print(old_key, key_count)
offset = (key_count-1) % len(keys_map[old_key])
print(keys_map[old_key][offset], end='')
if old_key >= 100:
print('*'+str(key_count)+']', end='')
if old_key == 101: #right
for _ in range(key_count):
message.pop(cursor-1)
cursor -= 1
elif old_key == 102: #up
cursor -= key_count
elif old_key == 103: #up
cursor += key_count
else:
message.insert(cursor, keys_map[old_key][offset])
cursor += 1
key_count = 0
key_count += 1
old_key = curr_key
old_time = curr_time

offset = (key_count-1) % len(keys_map[old_key])
print(keys_map[old_key][offset], end='')
if old_key >= 100:
print('*'+str(key_count)+']', end='')

print('\n---')
print(''.join(message))
```