|
|
import sys |
|
|
import io |
|
|
import ast |
|
|
|
|
|
|
|
|
if sys.platform == 'win32': |
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') |
|
|
|
|
|
from hindi_xlit import HindiTransliterator |
|
|
|
|
|
def load_dictionary(filename='dictionary.txt'): |
|
|
""" |
|
|
Load the mythology dictionary from file. |
|
|
Returns a dictionary mapping English words to Hindi transliterations. |
|
|
""" |
|
|
dictionary = {} |
|
|
try: |
|
|
with open(filename, 'r', encoding='utf-8') as f: |
|
|
in_dict = False |
|
|
for line in f: |
|
|
line = line.strip() |
|
|
|
|
|
|
|
|
if not line or line.startswith('#'): |
|
|
continue |
|
|
|
|
|
|
|
|
if 'MYTHOLOGY_DICTIONARY = {' in line: |
|
|
in_dict = True |
|
|
line = line.split('{', 1)[1].strip() |
|
|
if not line: |
|
|
continue |
|
|
|
|
|
if not in_dict: |
|
|
continue |
|
|
|
|
|
|
|
|
if ':' in line: |
|
|
|
|
|
while not line.rstrip().endswith(','): |
|
|
next_line = next(f, '').strip() |
|
|
if not next_line: |
|
|
break |
|
|
line += ' ' + next_line |
|
|
|
|
|
|
|
|
line = line.split('}')[0].strip() |
|
|
|
|
|
|
|
|
entries = [e.strip() for e in line.split(',') if ':' in e] |
|
|
|
|
|
for entry in entries: |
|
|
try: |
|
|
key_part, value_part = entry.split(':', 1) |
|
|
key = key_part.strip().strip("'\"") |
|
|
value = value_part.strip().strip("'\",}") |
|
|
if key and value: |
|
|
dictionary[key.lower()] = value |
|
|
except (ValueError, IndexError): |
|
|
continue |
|
|
|
|
|
|
|
|
if '}' in line and in_dict: |
|
|
break |
|
|
|
|
|
print(f"✓ Dictionary loaded successfully: {len(dictionary)} words") |
|
|
return dictionary |
|
|
|
|
|
except FileNotFoundError: |
|
|
print(f"Warning: Dictionary file '{filename}' not found.") |
|
|
print("Proceeding with model-only transliteration.") |
|
|
return {} |
|
|
except Exception as e: |
|
|
print(f"Warning: Error loading dictionary: {str(e)}") |
|
|
print("Proceeding with model-only transliteration.") |
|
|
return {} |
|
|
|
|
|
def get_transliteration(word, dictionary, transliterator, show_source=False): |
|
|
""" |
|
|
Get transliteration for a word. |
|
|
First checks dictionary, then falls back to model. |
|
|
|
|
|
Args: |
|
|
word: English word to transliterate |
|
|
dictionary: Dictionary mapping English to Hindi |
|
|
transliterator: HindiTransliterator instance |
|
|
show_source: If True, returns (transliteration, source) |
|
|
|
|
|
Returns: |
|
|
Transliteration string, or tuple (transliteration, source) if show_source=True |
|
|
""" |
|
|
word_lower = word.lower().strip() |
|
|
|
|
|
|
|
|
if word_lower in dictionary: |
|
|
result = dictionary[word_lower] |
|
|
if show_source: |
|
|
return result, "dictionary" |
|
|
return result |
|
|
|
|
|
|
|
|
try: |
|
|
model_result = transliterator.transliterate(word) |
|
|
|
|
|
|
|
|
if isinstance(model_result, list): |
|
|
result = model_result[0] |
|
|
else: |
|
|
result = model_result |
|
|
|
|
|
if show_source: |
|
|
return result, "model" |
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
raise Exception(f"Transliteration failed: {str(e)}") |
|
|
|
|
|
def main(): |
|
|
print("=" * 50) |
|
|
print("Hindi Transliterator with Mythology Dictionary") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
print("\nLoading dictionary...") |
|
|
dictionary = load_dictionary('dictionary.txt') |
|
|
|
|
|
|
|
|
print("Initializing transliterator model...") |
|
|
transliterator = HindiTransliterator() |
|
|
print("✓ Model loaded successfully") |
|
|
|
|
|
print("\nCommands:") |
|
|
print(" 'exit' or 'quit' - Exit the program") |
|
|
print(" 'stats' - Show statistics") |
|
|
print(" 'check <word>' - Check if word is in dictionary") |
|
|
print("-" * 50) |
|
|
|
|
|
while True: |
|
|
|
|
|
user_input = input("\nEnter English word to transliterate: ").strip() |
|
|
|
|
|
|
|
|
if user_input.lower() in ('exit', 'quit', 'q'): |
|
|
print("Exiting transliterator...") |
|
|
break |
|
|
|
|
|
|
|
|
if user_input.lower() == 'stats': |
|
|
print(f"\nDictionary Statistics:") |
|
|
print(f" Total words in dictionary: {len(dictionary)}") |
|
|
print(f" Coverage: Mythology, Vedic texts, Sanskrit terms") |
|
|
continue |
|
|
|
|
|
|
|
|
if user_input.lower().startswith('check '): |
|
|
word_to_check = user_input[6:].strip().lower() |
|
|
if word_to_check in dictionary: |
|
|
print(f"✓ '{word_to_check}' is in dictionary: {dictionary[word_to_check]}") |
|
|
else: |
|
|
print(f"✗ '{word_to_check}' is NOT in dictionary (will use model)") |
|
|
continue |
|
|
|
|
|
if not user_input: |
|
|
print("Please enter a word to transliterate.") |
|
|
continue |
|
|
|
|
|
try: |
|
|
|
|
|
transliteration, source = get_transliteration( |
|
|
user_input, |
|
|
dictionary, |
|
|
transliterator, |
|
|
show_source=True |
|
|
) |
|
|
|
|
|
|
|
|
print(f"\nTransliteration:") |
|
|
print(f"English: {user_input}") |
|
|
print(f"Hindi: {transliteration}") |
|
|
print(f"Source: {source.upper()}") |
|
|
|
|
|
|
|
|
if source == "dictionary": |
|
|
print(" ✓ Verified from mythology dictionary") |
|
|
else: |
|
|
print(" ⚠ Generated by model (not in dictionary)") |
|
|
|
|
|
|
|
|
with open('output_trans.txt', 'a', encoding='utf-8') as f: |
|
|
f.write(f"{user_input} -> {transliteration} [{source}]\n") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error: {str(e)}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |