Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions tools/lou_maketable.d/export_chunked_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ def main():
for text, braille in c.fetchall():
# ignore long words for now
if len(text) > 48:
printerrln('WARNING: word is too long to be processed, may be translated incorrectly: %s' % text)
printerrln(
f'WARNING: word is too long to be processed, may be translated incorrectly: {text}'
)

continue
if not is_letter(text):
printerrln('WARNING: word has non-letters: %s' % text)
printerrln(f'WARNING: word has non-letters: {text}')
continue
suggested_hyphen_string = suggest_chunks(text, braille)
if suggested_hyphen_string:
if suggested_hyphen_string := suggest_chunks(text, braille):
Comment on lines -38 to +46
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

if hyphenation_enabled:
actual_hyphen_string = hyphenate(text)
else:
Expand Down
19 changes: 8 additions & 11 deletions tools/lou_maketable.d/make_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,29 @@ def main():
if actual_braille != braille:
problem = True
if problems < args.MAX_PROBLEMS:
comments = []
comments.append("wrong braille\t\t" + actual_braille)
comments = ["wrong braille\t\t" + actual_braille]
suggest_rows = []
suggest_rules = []
non_letters = False
if not is_letter(text):
non_letters = True
comments.append("word has non-letters")
comments.append("applied rules")
applied_rules = [get_rule(x) for x in applied_rules if x is not None]
for rule in applied_rules:
comments.append("> " + "\t".join(rule))
other_relevant_rules = set(find_relevant_rules(text)) - set(applied_rules)
if other_relevant_rules:
comments.extend("> " + "\t".join(rule) for rule in applied_rules)
if other_relevant_rules := set(
find_relevant_rules(text)
) - set(applied_rules):
comments.append("other relevant rules")
for rule in other_relevant_rules:
comments.append("> " + "\t".join(rule))
suggest_rules.append({"opcode": "word", "text": text, "braille": braille})
comments.extend("> " + "\t".join(rule) for rule in other_relevant_rules)
suggest_rules = [{"opcode": "word", "text": text, "braille": braille}]
Comment on lines -59 to +73
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

if problem and problems < args.MAX_PROBLEMS:
if args.NON_INTERACTIVE and suggest_rules:
for rule in suggest_rules:
println("%(opcode)s\t%(text)s\t%(braille)s" % rule)
else:
println("# >>>\t%s\t%s" % (text, braille or ""))
for comment in comments:
println("# | " + comment)
println(f"# | {comment}")
println("# |__")
for row in suggest_rows:
println("#\t%(text)s\t%(braille)s" % row)
Expand Down
13 changes: 6 additions & 7 deletions tools/lou_maketable.d/submit_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ def __next__(self):
row = next(self.reader)
if not row or row[0].startswith("#"):
return next(self)
if not len(row) == 3:
printerrln('expected 3 columns, got %s: %s' % (len(row),row))
if len(row) != 3:
printerrln(f'expected 3 columns, got {len(row)}: {row}')
exit(1)
if not row[0] == "":
if row[0] != "":
printerrln("expected first column to be empty, got '%s'" % (row[0],))
exit(1)
maybe_chunked_text, braille = row[1:3]
maybe_chunked_text = to_lowercase(maybe_chunked_text)
text, chunked_text = read_text(maybe_chunked_text)
braille = braille if braille != "" else None
if braille != None:
if '0' in to_dot_pattern(braille).split('-'):
printerrln('invalid braille: %s' % (braille,))
exit(1)
if braille != None and '0' in to_dot_pattern(braille).split('-'):
printerrln(f'invalid braille: {braille}')
exit(1)
Comment on lines -36 to +48
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Reader.__next__ refactored with the following changes:

exit_if_not(not chunked_text or validate_chunks(chunked_text))
return {'text': text,
'braille': braille,
Expand Down
16 changes: 11 additions & 5 deletions tools/lou_maketable.d/submit_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ def main():
rules = []
for line in fileinput.FileInput(args.CONTRACTION_TABLE, openhook=fileinput.hook_encoded("utf-8")):
m = p.match(line)
exit_if_not(m and not m.group(1))
exit_if_not(m and not m[1])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

_, opcode, text, braille, _, comment = m.groups()
if opcode:
rule = {"opcode": opcode, "text": text, "braille": braille, "comment": comment}
rules.append(rule)
for line in fileinput.FileInput(args.FILE, openhook=fileinput.hook_encoded("utf-8")):
m = p.match(line)
if not m:
printerrln("%s: rule is not valid" % (line,))
printerrln(f"{line}: rule is not valid")
exit(1)
exit_if_not(m)
add_or_delete, opcode, text, braille, _, _ = m.groups()
if opcode:
comment = braille
if comment.endswith('\\'):
comment = comment + " "
comment = f"{comment} "
braille = to_dot_pattern(braille)
rules, rule = partition(lambda rule: rule["opcode"] == opcode and
rule["text"] == text and
Expand All @@ -60,11 +60,17 @@ def main():
rule = list(rule)
if add_or_delete == '-':
if not rule:
printerrln("%s %s %s: rule can not be deleted because not in %s" % (opcode,text,braille,args.CONTRACTION_TABLE))
printerrln(
f"{opcode} {text} {braille}: rule can not be deleted because not in {args.CONTRACTION_TABLE}"
)

exit(1)
else:
if rule:
printerrln("%s %s %s: rule can not be added because already in %s" % (opcode,text,braille,args.CONTRACTION_TABLE))
printerrln(
f"{opcode} {text} {braille}: rule can not be added because already in {args.CONTRACTION_TABLE}"
)

exit(1)
rule = {"opcode": opcode, "text": text, "braille": braille, "comment": comment}
rules.append(rule)
Expand Down
18 changes: 10 additions & 8 deletions tools/lou_maketable.d/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def split_into_words(text, hyphen_string):
word_hyphen_strings = []
word = []
word_hyphen_string = []
for c,(h1,h2) in izip(text, pairwise('1' + hyphen_string + '1')):
for c,(h1,h2) in izip(text, pairwise(f'1{hyphen_string}1')):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function split_into_words refactored with the following changes:

if is_letter(c):
word.append(c)
word_hyphen_string.append(h1)
Expand All @@ -138,7 +138,7 @@ def load_table(new_table):
liblouis.loadTable(table);

def is_letter(text):
return all([liblouis.isLetter(c) for c in text])
return all(liblouis.isLetter(c) for c in text)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is_letter refactored with the following changes:


def to_lowercase(text):
return "".join([liblouis.toLowercase(c) for c in text])
Expand Down Expand Up @@ -167,13 +167,15 @@ def translate(text):
c_rules_len = c_int(max_rules)
exit_if_not(liblouis._lou_translateWithTracing(table, c_text, byref(c_text_len), c_braille, byref(c_braille_len),
None, None, None, None, None, 0, c_rules, byref(c_rules_len)))
return c_braille.value, c_rules[0:c_rules_len.value]
return c_braille.value, c_rules[:c_rules_len.value]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function translate refactored with the following changes:


def get_rule(c_rule_pointer):
c_rule_string = create_unicode_buffer(u"", 128)
if not liblouis.printRule(cast(c_rule_pointer, c_void_p), c_rule_string):
return None
return tuple(c_rule_string.value.split(" "))
return (
tuple(c_rule_string.value.split(" "))
if liblouis.printRule(cast(c_rule_pointer, c_void_p), c_rule_string)
else None
)
Comment on lines -174 to +178
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_rule refactored with the following changes:


def suggest_chunks(text, braille):
c_text = create_unicode_buffer(text)
Expand All @@ -182,15 +184,15 @@ def suggest_chunks(text, braille):
if not liblouis.suggestChunks(c_text, c_braille, c_hyphen_string):
return None;
hyphen_string = c_hyphen_string.value.decode('ascii')
hyphen_string = hyphen_string[1:len(hyphen_string)-1]
hyphen_string = hyphen_string[1:-1]
Comment on lines -185 to +187
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function suggest_chunks refactored with the following changes:

assert len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string)
return hyphen_string

def find_relevant_rules(text):
c_text = create_unicode_buffer(text)
max_rules = 16
c_rules = [u""] * max_rules + [None]
for i in range(0, max_rules):
for i in range(max_rules):
Comment on lines -193 to +195
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function find_relevant_rules refactored with the following changes:

c_rules[i] = create_unicode_buffer(c_rules[i], 128)
c_rules[i] = cast(c_rules[i], c_wchar_p)
c_rules = (c_wchar_p * (max_rules + 1))(*c_rules)
Expand Down