Rev 297 |
View as "text/plain" |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
1
#!/usr/bin/env python3
'''
Created on 2014-10-20
@author: Thomas 'PointedEars' Lahn <mail@PointedEars.de>
'''
from sys import argv, stderr
from re import findall, compile
from os.path import basename
from functools import cmp_to_key
from Dictionary import dmsg, sort_dict_alnum_english_key
from VulcanDictionary import VulcanDictionary, Text
dictionary = {}
def cli_help():
print('Usage: {0} TEXT...'.format(basename(argv[0])))
def get_sort_dict_alnum_vulcan_key ():
letters = list(map(str.lower, [
" ", 'S', 'T', 'P', 'K', 'R', 'L', 'A', 'Sh', 'O', 'U', 'D',
'V', 'Kh', 'E', 'H', 'G', 'Ch', 'I', 'N', 'Zh', 'M', 'Y', 'F', 'Z',
'Th', 'W', 'B', "'", '-']))
letter_values = dict(map(lambda x: (x[1], x[0]), enumerate(letters)))
letters_re = compile(r'(?:{0})'.format('|'.join(sorted(letters, key=lambda char:-len(char)))))
def sort_dict_alnum_vulcan (a, b):
# split into Vulcan letters
a = findall(letters_re, sort_dict_alnum_english_key(a))
b = findall(letters_re, sort_dict_alnum_english_key(b))
if len(a) < len(b):
for index, char in enumerate(a):
diff = letter_values[char] - letter_values[b[index]]
if diff != 0:
return diff
return -1
# len(b) <= len(a)
for index, char in enumerate(b):
diff = letter_values[a[index]] - letter_values[char]
if diff != 0:
return diff
return 1 if len(b) < len(a) else 0
return cmp_to_key(sort_dict_alnum_vulcan)
if __name__ == '__main__':
if len(argv) < 2:
print('Nothing to translate.', end='\n\n', file=stderr)
cli_help()
exit(1)
text = ' '.join(argv[1:])
dictionary = VulcanDictionary(dictionary)
dictionary.load('vuh-gol-en.dict.zdb.txt', 'vuh')
dictionary.clean()
# try:
# for phrase, data in OrderedDict(sorted(
# dictionary.items(),
# key=get_sort_dict_alnum_vulcan_key()
# )).items():
# print(phrase, "=", data)
# except BrokenPipeError:
# pass
text = Text(text)
dmsg("text:", text, min_level=2)
dmsg("text:", text.__repr__(), min_level=2)
text.translate(dictionary)
# dmsg("words-translation:", words, min_level=2)
# dmsg("words-translation-reduced:",
# list(map(
# lambda word:
# word['en']
# if (hasattr(word, "get") and word.get('en', None) is not None)
# else word,
# words)),
# min_level=2)
# for key, value in dictionary._expressions.items():
# dmsg(key, value, min_level=3)