| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from ctypes import * |
| 22 |
from itertools import takewhile, zip_longest, chain, tee |
| 23 |
izip = zip |
| 24 |
izip_longest = zip_longest |
| 25 |
|
| 26 |
from louis import _loader, liblouis, wideCharBytes, outlenMultiplier, createEncodedByteString |
| 27 |
import re |
| 28 |
import sqlite3 |
| 29 |
import sys |
| 30 |
|
| 31 |
def exit_if_not(expression): |
| 32 |
if not expression: |
| 33 |
raise RuntimeError() |
| 34 |
|
| 35 |
exit_if_not(liblouis.lou_charSize() == 4) |
| 36 |
|
| 37 |
liblouis.isLetter.argtypes = (c_wchar,) |
| 38 |
liblouis.toLowercase.argtypes = (c_wchar,) |
| 39 |
liblouis.toLowercase.restype = c_wchar |
| 40 |
|
| 41 |
def println(line=""): |
| 42 |
sys.stdout.write(("%s\n" % line)) |
| 43 |
|
| 44 |
def printerrln(line=""): |
| 45 |
sys.stderr.write(("%s\n" % line)) |
| 46 |
|
| 47 |
def validate_chunks(chunked_text): |
| 48 |
return re.search(r"^([^)(|]+|\([^)(|][^)(|]+\))(\|?([^)(|]+|\([^)(|][^)(|]+\)))*$", chunked_text) != None |
| 49 |
|
| 50 |
def print_chunks(text, hyphen_string): |
| 51 |
exit_if_not(len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string)) |
| 52 |
chunked_text = [] |
| 53 |
k = 0 |
| 54 |
prev_c = None |
| 55 |
for c in hyphen_string: |
| 56 |
if c != prev_c and c == "0": |
| 57 |
chunked_text.append("(") |
| 58 |
chunked_text.append(text[k]) |
| 59 |
if c != prev_c and prev_c == "0": |
| 60 |
chunked_text.append(")") |
| 61 |
if c == "1": |
| 62 |
chunked_text.append("|") |
| 63 |
prev_c = c |
| 64 |
k += 1 |
| 65 |
chunked_text.append(text[k]) |
| 66 |
if (prev_c == "0"): |
| 67 |
chunked_text.append(")") |
| 68 |
return "".join(chunked_text) |
| 69 |
|
| 70 |
def parse_chunks(chunked_text): |
| 71 |
exit_if_not(validate_chunks(chunked_text)) |
| 72 |
text, _ = read_text(chunked_text) |
| 73 |
hyphen_string = ["x"] * (len(text) - 1) |
| 74 |
k = 0 |
| 75 |
for c in chunked_text: |
| 76 |
if c == "(": |
| 77 |
hyphen_string[k:] = ["0"] * (len(text) - 1 - k) |
| 78 |
elif c == ")": |
| 79 |
hyphen_string[k-1:] = ["x"] * (len(text) - 1 - (k-1)) |
| 80 |
elif c == "|": |
| 81 |
hyphen_string[k-1] = "1" |
| 82 |
else: |
| 83 |
k += 1 |
| 84 |
if k > len(text): |
| 85 |
break |
| 86 |
return text, "".join(hyphen_string) |
| 87 |
|
| 88 |
def read_text(maybe_chunked_text): |
| 89 |
if re.search("[)(|]", maybe_chunked_text) != None: |
| 90 |
text = re.sub("[)(|]", "", maybe_chunked_text) |
| 91 |
chunked_text = maybe_chunked_text |
| 92 |
else: |
| 93 |
text = maybe_chunked_text |
| 94 |
chunked_text = None |
| 95 |
return text, chunked_text |
| 96 |
|
| 97 |
def compare_chunks(expected_hyphen_string, actual_hyphen_string, text): |
| 98 |
exit_if_not(len(expected_hyphen_string) == len(text) - 1 and re.search("^[01x]+$", expected_hyphen_string)) |
| 99 |
exit_if_not(len(actual_hyphen_string) == len(text) - 1 and re.search("^[01]+$", actual_hyphen_string)) |
| 100 |
chunk_errors = my_zip(text, |
| 101 |
map(lambda e, a: "*" if e in "1x" and a == "1" else |
| 102 |
"." if a == "1" else |
| 103 |
"-" if e == "1" else None, |
| 104 |
expected_hyphen_string, actual_hyphen_string)) |
| 105 |
return chunk_errors if re.search(r"[-\.]", chunk_errors) else None |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
def split_into_words(text, hyphen_string): |
| 110 |
exit_if_not(len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string)) |
| 111 |
words = [] |
| 112 |
word_hyphen_strings = [] |
| 113 |
word = [] |
| 114 |
word_hyphen_string = [] |
| 115 |
for c,(h1,h2) in izip(text, pairwise('1' + hyphen_string + '1')): |
| 116 |
if is_letter(c): |
| 117 |
word.append(c) |
| 118 |
word_hyphen_string.append(h1) |
| 119 |
elif h1 not in "1x" or h2 not in "1x": |
| 120 |
return [] |
| 121 |
else: |
| 122 |
if len(word) > 1: |
| 123 |
words.append("".join(word)) |
| 124 |
word_hyphen_strings.append("".join(word_hyphen_string[1:])) |
| 125 |
word = [] |
| 126 |
word_hyphen_string = [] |
| 127 |
if len(word) > 1: |
| 128 |
words.append("".join(word)) |
| 129 |
word_hyphen_strings.append("".join(word_hyphen_string[1:])) |
| 130 |
return izip(words, word_hyphen_strings) |
| 131 |
|
| 132 |
table = None |
| 133 |
|
| 134 |
def load_table(new_table): |
| 135 |
global table |
| 136 |
table = new_table |
| 137 |
table = table.encode("ASCII") if isinstance(table, str) else bytes(table) |
| 138 |
liblouis.loadTable(table); |
| 139 |
|
| 140 |
def is_letter(text): |
| 141 |
return all([liblouis.isLetter(c) for c in text]) |
| 142 |
|
| 143 |
def to_lowercase(text): |
| 144 |
return "".join([liblouis.toLowercase(c) for c in text]) |
| 145 |
|
| 146 |
def to_dot_pattern(braille): |
| 147 |
c_braille = create_unicode_buffer(braille) |
| 148 |
c_dots = create_string_buffer(9 * len(braille)) |
| 149 |
liblouis.toDotPattern(c_braille, c_dots) |
| 150 |
return c_dots.value.decode('ascii') |
| 151 |
|
| 152 |
def hyphenate(text): |
| 153 |
c_text = createEncodedByteString(text) |
| 154 |
c_text_len = c_int(int(len(c_text)/wideCharBytes)) |
| 155 |
c_hyphen_string = create_string_buffer(c_text_len.value + 1) |
| 156 |
exit_if_not(liblouis.lou_hyphenate(table, c_text, c_text_len, c_hyphen_string, 0)) |
| 157 |
return "".join(['1' if int(p) % 2 else '0' for p in c_hyphen_string.value[1:]]) |
| 158 |
|
| 159 |
def translate(text): |
| 160 |
c_text = create_unicode_buffer(text) |
| 161 |
c_text_len = c_int(len(text)) |
| 162 |
braille_len = len(text) * outlenMultiplier |
| 163 |
c_braille = create_unicode_buffer(braille_len) |
| 164 |
c_braille_len = c_int(braille_len) |
| 165 |
max_rules = 16 |
| 166 |
c_rules = (c_void_p * max_rules)() |
| 167 |
c_rules_len = c_int(max_rules) |
| 168 |
exit_if_not(liblouis._lou_translate(table, table, c_text, byref(c_text_len), c_braille, byref(c_braille_len), |
| 169 |
None, None, None, None, None, 0, c_rules, byref(c_rules_len))) |
| 170 |
return c_braille.value, c_rules[0:c_rules_len.value] |
| 171 |
|
| 172 |
def get_rule(c_rule_pointer): |
| 173 |
c_rule_string = create_unicode_buffer(u"", 128) |
| 174 |
if not liblouis.printRule(cast(c_rule_pointer, c_void_p), c_rule_string): |
| 175 |
return None |
| 176 |
return tuple(c_rule_string.value.split("\t")) |
| 177 |
|
| 178 |
def suggest_chunks(text, braille): |
| 179 |
c_text = create_unicode_buffer(text) |
| 180 |
c_braille = create_unicode_buffer(braille) |
| 181 |
c_hyphen_string = create_string_buffer(len(text) + 2) |
| 182 |
if not liblouis.suggestChunks(c_text, c_braille, c_hyphen_string): |
| 183 |
return None; |
| 184 |
hyphen_string = c_hyphen_string.value.decode('ascii') |
| 185 |
hyphen_string = hyphen_string[1:len(hyphen_string)-1] |
| 186 |
assert len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string) |
| 187 |
return hyphen_string |
| 188 |
|
| 189 |
def find_relevant_rules(text): |
| 190 |
c_text = create_unicode_buffer(text) |
| 191 |
max_rules = 16 |
| 192 |
c_rules = [u""] * max_rules + [None] |
| 193 |
for i in range(0, max_rules): |
| 194 |
c_rules[i] = create_unicode_buffer(c_rules[i], 128) |
| 195 |
c_rules[i] = cast(c_rules[i], c_wchar_p) |
| 196 |
c_rules = (c_wchar_p * (max_rules + 1))(*c_rules) |
| 197 |
liblouis.findRelevantRules(c_text, c_rules) |
| 198 |
return map(lambda x: tuple(x.split("\t")), takewhile(lambda x: x, c_rules)) |
| 199 |
|
| 200 |
def open_dictionary(dictionary): |
| 201 |
conn = sqlite3.connect(dictionary) |
| 202 |
c = conn.cursor() |
| 203 |
return conn, c |
| 204 |
|
| 205 |
def filterfalse(predicate, iterable): |
| 206 |
return [x for x in iterable if not predicate(x)] |
| 207 |
|
| 208 |
def partition(pred, iterable): |
| 209 |
t1, t2 = tee(iterable) |
| 210 |
return filterfalse(pred, t1), filter(pred, t2) |
| 211 |
|
| 212 |
def pairwise(iterable): |
| 213 |
a, b = tee(iterable) |
| 214 |
next(b, None) |
| 215 |
return izip(a, b) |
| 216 |
|
| 217 |
def my_zip(*iterables): |
| 218 |
return "".join([x for x in chain(*izip_longest(*iterables)) if x is not None]) |
| 219 |
|
| 220 |
class future: |
| 221 |
def __init__(self, f): |
| 222 |
self.f = f |
| 223 |
self.fut = f |
| 224 |
self.is_realized = False |
| 225 |
def __call__(self): |
| 226 |
if not self.is_realized: |
| 227 |
self.fut = self.f() |
| 228 |
self.is_realized = True |
| 229 |
return self.fut |