|
| 1 | +from kaalin.converter.latin_cyrillic_converter import latin2cyrillic, cyrillic2latin |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +def cyr2lat(): |
| 6 | + if len(sys.argv) < 2 or len(sys.argv) > 3: |
| 7 | + print("Use:\n cyr2lat <input_file> [output_file]") |
| 8 | + sys.exit(1) |
| 9 | + |
| 10 | + input_file = sys.argv[1] |
| 11 | + |
| 12 | + if len(sys.argv) == 3: |
| 13 | + output_file = sys.argv[2] |
| 14 | + else: |
| 15 | + base, ext = os.path.splitext(input_file) |
| 16 | + output_file = f"{base}-lat{ext}" |
| 17 | + |
| 18 | + try: |
| 19 | + with open(input_file, "r", encoding="utf-8") as f: |
| 20 | + text = f.read() |
| 21 | + converted = cyrillic2latin(text) |
| 22 | + with open(output_file, "w", encoding="utf-8") as f: |
| 23 | + f.write(converted) |
| 24 | + print(f"✅ Converted text written to: {output_file}") |
| 25 | + except Exception as e: |
| 26 | + print(f"❌ Error: {e}") |
| 27 | + sys.exit(1) |
| 28 | + |
| 29 | + |
| 30 | +def lat2cyr(): |
| 31 | + if len(sys.argv) < 2 or len(sys.argv) > 3: |
| 32 | + print("Use:\n lat2cyr <input_file> [output_file]") |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + input_file = sys.argv[1] |
| 36 | + |
| 37 | + if len(sys.argv) == 3: |
| 38 | + output_file = sys.argv[2] |
| 39 | + else: |
| 40 | + base, ext = os.path.splitext(input_file) |
| 41 | + output_file = f"{base}-cyr{ext}" |
| 42 | + |
| 43 | + try: |
| 44 | + with open(input_file, "r", encoding="utf-8") as f: |
| 45 | + text = f.read() |
| 46 | + converted = latin2cyrillic(text) |
| 47 | + with open(output_file, "w", encoding="utf-8") as f: |
| 48 | + f.write(converted) |
| 49 | + print(f"✅ Converted text written to: {output_file}") |
| 50 | + except Exception as e: |
| 51 | + print(f"❌ Error: {e}") |
| 52 | + sys.exit(1) |
0 commit comments