Skip to content

Commit

Permalink
add python cli
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Nov 9, 2024
1 parent 41f6fed commit f42b210
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ Select a transliteration scheme:
s = uklatn.encode("Борщ", uklatn.DSTU_9112_A)
```

Module command line
--
```sh
python -m uklatn 'Бери вершину'
```

```txt
usage: uklatn [-h] [-t {DSTU_9112_A,DSTU_9112_B,KMU_55}] [-l] [-c] text [text ...]
positional arguments:
text text to transliterate
options:
-h, --help show this help message and exit
-t, --table {DSTU_9112_A,DSTU_9112_B,KMU_55}
transliteration system (default: DSTU_9112_A)
-l, --latin convert to Latin script (default)
-c, --cyrillic convert to Cyrillic script
```
26 changes: 26 additions & 0 deletions python/uklatn.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,29 @@ def decode(text, table=None):
if not dec:
raise ValueError(f'invalid table {table!r}')
return dec.transform(text)


def main(args):
text = ' '.join(args.text)
table = args.table
if table is None:
table = 'DSTU_9112_A'
names = {'DSTU_9112_A': 1, 'DSTU_9112_B': 2, 'KMU_55': 3}
table = names[table]
tr = encode
if args.cyrillic and not args.latin:
tr = decode
res = tr(text, table)
print(res)


if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('text', nargs='+', help='text to transliterate')
parser.add_argument('-t', '--table', choices=['DSTU_9112_A', 'DSTU_9112_B', 'KMU_55'], help='transliteration system (default: DSTU_9112_A)')
parser.add_argument('-l', '--latin', action='store_true', help='convert to Latin script (default)')
parser.add_argument('-c', '--cyrillic', action='store_true', help='convert to Cyrillic script')

args = parser.parse_args()
main(args)
29 changes: 29 additions & 0 deletions tools/gen/gen_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def _emit_tr(cname, rules, file):
tabledef = so.getvalue()

context['global_tables'] = classdefs_tables + tabledef
context['default_table'] = default_table
context['table_list'] = list(tables)
context['table_names'] = {k:i for i,k in enumerate(tables, 1)}

template = '''
"""Ukrainian Cyrillic transliteration to Latin script
Expand Down Expand Up @@ -237,6 +240,32 @@ def decode(text, table=None):
if not dec:
raise ValueError(f'invalid table {{table!r}}')
return dec.transform(text)
def main(args):
text = ' '.join(args.text)
table = args.table
if table is None:
table = {default_table!r}
names = {table_names!r}
table = names[table]
tr = encode
if args.cyrillic and not args.latin:
tr = decode
res = tr(text, table)
print(res)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('text', nargs='+', help='text to transliterate')
parser.add_argument('-t', '--table', choices={table_list!r}, help='transliteration system (default: {default_table})')
parser.add_argument('-l', '--latin', action='store_true', help='convert to Latin script (default)')
parser.add_argument('-c', '--cyrillic', action='store_true', help='convert to Cyrillic script')
args = parser.parse_args()
main(args)
'''
text = template.format(**context)
return text
Expand Down

0 comments on commit f42b210

Please sign in to comment.