In [26]:
import string
from sklearn import tree
In [27]:
X = [[1, 0, 0, 0, 0, 0], # A
[1, 0, 1, 0, 0, 0], # B
[1, 1, 0, 0, 0, 0], # C
[1, 1, 0, 1, 0, 0], # D
[1, 0, 0, 1, 0, 0], # E
[1, 1, 1, 0, 0, 0], # F
[1, 1, 1, 1, 0, 0], # G
[1, 0, 1, 1, 0, 0], # H
[0, 1, 1, 0, 0, 0], # I
[0, 1, 1, 1, 0, 0], # J
[1, 0, 0, 0, 1, 0], # K
[1, 0, 1, 0, 1, 0], # L
[1, 1, 0, 0, 1, 0], # M
[1, 1, 0, 1, 1, 0], # N
[1, 0, 0, 1, 1, 0], # O
[1, 1, 1, 0, 1, 0], # P
[1, 1, 1, 1, 1, 0], # Q
[1, 0, 1, 1, 1, 0], # R
[0, 1, 1, 0, 1, 0], # S
[0, 1, 1, 1, 1, 0], # T
[1, 0, 0, 0, 1, 1], # U
[1, 0, 1, 0, 1, 1], # V
[0, 1, 1, 1, 0, 1], # W
[1, 1, 0, 0, 1, 1], # X
[1, 1, 0, 1, 1, 1], # Y
[1, 0, 0, 1, 1, 1]] # Z
In [28]:
Y = [x for x in xrange(1,27)]
In [29]:
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
clf.predict([1, 0, 1, 1, 0, 0])
Out[29]:
In [30]:
def translate_braille(filename):
alphabet = list(string.ascii_lowercase)
lines = {}
counter = 0
with open(filename, 'r') as f:
for line in f:
lines[counter] = line.replace(' \n','').replace('\n','').split(' ')
counter += 1
characters = []
for x in xrange(len(lines[0])):
characters.append(lines[0][x] + lines[1][x] + lines[2][x])
characters = [x.replace('O','1').replace('.','0') for x in characters]
X = []
for c in characters:
X.append([int(x) for x in list(c)])
predictions = clf.predict(X)
message = ""
for x in predictions:
message += alphabet[x-1]
print message
In [31]:
translate_braille('test_input.txt')
In [32]:
translate_braille('input.txt')