-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.py
55 lines (45 loc) · 1.65 KB
/
decoder.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
#!/usr/bin/python
#coding: utf8
from sys import argv
from constants import Constants
import sys
import json
import codecs
def breakSectionIntoTokens(section, decodingDictionary):
tokens = []
currentToken = ''
currentIndexStart = 0
token = ''
currentIndexEnd = 1
while len(section) > 0:
token = token + section[0]
section = section[1:]
if token not in decodingDictionary and token != '\n':
tokens.append(''.join(token[:-1]))
token = token[-1]
tokens.append(token)
return tokens
###################################################
script, decodingDictionaryFileName, decodingTargetFileName = argv
decodingDictionary = None
with codecs.open(decodingDictionaryFileName, encoding='utf-8') as fileHandle:
fileContents = fileHandle.read()
decodingDictionary = json.loads(fileContents)
with codecs.open(decodingTargetFileName, encoding='utf-8') as fileHandle:
lines = fileHandle.readlines()
for line in lines:
splitLine = line.split(Constants.SECTION_SEPARATOR)
decodedSections = []
for section in splitLine:
decodedTokenText = ''
tokens = breakSectionIntoTokens(section, decodingDictionary)
for token in tokens:
decodedToken = ''
if token != '\n':
if token != '~':
decodedToken = decodingDictionary[token]
else:
decodedToken ='~'
decodedTokenText = decodedTokenText + decodedToken
decodedSections.append(decodedTokenText)
print ('||'.join(decodedSections)).encode('utf-8')