Skip to content

Commit

Permalink
Fix grammar table for verbs
Browse files Browse the repository at this point in the history
Verbs have multiple grammar tables in duden sometimes each with
different number of keys for each grammar form, since there are special
forms such as 'infinitiv mit zu' where normal categories such as
singular and plural do not apply.

This commit fixes the table by adding padding to rows with less keys.

Fixes #94.
  • Loading branch information
radomirbosak committed Jun 6, 2020
1 parent fd77d31 commit bc8168a
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions duden/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,21 +675,34 @@ def display_grammar(word, grammar_args):

grammar_tokens = [token.lower() for token in grammar_args.split(',')]

table = []
# filter out grammar forms which do not match provided keys
tag_columns = []
value_column = []
for keys, value in word.grammar_raw:
lkeys = {key.lower() for key in keys}

if not (grammar_args == 'ALL' or lkeys.issuperset(grammar_tokens)):
continue

reduced_keys = lkeys.difference(grammar_tokens)
keystring = ' '.join(reduced_keys)

if keystring:
row = list(reduced_keys) + [blue("|"), value]
else:
row = [value]
tag_columns.append(list(reduced_keys))
value_column.append(value)

# determine the width of the table
max_keys_count = max(map(len, tag_columns))

# if provided keys uniquely determine the value(s), display a 1-col table
if max_keys_count == 0:
return display_table([[value] for value in value_column])

# otherwise make a nice "| key1 key2 | value |" table
table = []
for keys, value in zip(tag_columns, value_column):
padding = [""] * (max_keys_count - len(keys))
row = keys + padding + [blue("|")] + [value]
table.append(row)

display_table(table)


Expand Down

0 comments on commit bc8168a

Please sign in to comment.