Subversion Repositories LCARS

Rev

Rev 297 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
292 PointedEar 1
#!/usr/bin/env python3
2
 
291 PointedEar 3
'''
4
Created on 2014-10-20
5
 
6
@author: Thomas 'PointedEars' Lahn <mail@PointedEars.de>
7
'''
8
from sys import argv, stderr
300 PointedEar 9
from re import findall, compile
293 PointedEar 10
from os.path import basename
291 PointedEar 11
from functools import cmp_to_key
300 PointedEar 12
from Dictionary import dmsg, sort_dict_alnum_english_key
13
from VulcanDictionary import VulcanDictionary, Text
291 PointedEar 14
 
15
dictionary = {}
16
 
292 PointedEar 17
def cli_help():
18
    print('Usage: {0} TEXT...'.format(basename(argv[0])))
19
 
296 PointedEar 20
def get_sort_dict_alnum_vulcan_key ():
291 PointedEar 21
    letters = list(map(str.lower, [
22
        " ", 'S', 'T', 'P', 'K', 'R', 'L', 'A', 'Sh', 'O', 'U', 'D',
23
        'V', 'Kh', 'E', 'H', 'G', 'Ch', 'I', 'N', 'Zh', 'M', 'Y', 'F', 'Z',
24
        'Th', 'W', 'B', "'", '-']))
25
    letter_values = dict(map(lambda x: (x[1], x[0]), enumerate(letters)))
26
    letters_re = compile(r'(?:{0})'.format('|'.join(sorted(letters, key=lambda char:-len(char)))))
27
 
28
    def sort_dict_alnum_vulcan (a, b):
29
        # split into Vulcan letters
30
        a = findall(letters_re, sort_dict_alnum_english_key(a))
31
        b = findall(letters_re, sort_dict_alnum_english_key(b))
32
 
33
        if len(a) < len(b):
34
            for index, char in enumerate(a):
35
                diff = letter_values[char] - letter_values[b[index]]
36
                if diff != 0:
37
                    return diff
38
            return -1
39
 
40
        # len(b) <= len(a)
41
        for index, char in enumerate(b):
42
            diff = letter_values[a[index]] - letter_values[char]
43
            if diff != 0:
44
                return diff
45
 
46
        return 1 if len(b) < len(a) else 0
47
 
48
    return cmp_to_key(sort_dict_alnum_vulcan)
49
 
296 PointedEar 50
 
292 PointedEar 51
if __name__ == '__main__':
52
    if len(argv) < 2:
53
        print('Nothing to translate.', end='\n\n', file=stderr)
54
        cli_help()
55
        exit(1)
291 PointedEar 56
 
296 PointedEar 57
    text = ' '.join(argv[1:])
292 PointedEar 58
 
293 PointedEar 59
    dictionary = VulcanDictionary(dictionary)
294 PointedEar 60
    dictionary.load('vuh-gol-en.dict.zdb.txt', 'vuh')
293 PointedEar 61
    dictionary.clean()
291 PointedEar 62
 
300 PointedEar 63
#       try:
64
#           for phrase, data in OrderedDict(sorted(
65
#               dictionary.items(),
66
#               key=get_sort_dict_alnum_vulcan_key()
67
#           )).items():
68
#               print(phrase, "=", data)
69
#       except BrokenPipeError:
70
#           pass
291 PointedEar 71
 
300 PointedEar 72
    text = Text(text)
73
 
293 PointedEar 74
    dmsg("text:", text, min_level=2)
300 PointedEar 75
    dmsg("text:", text.__repr__(), min_level=2)
291 PointedEar 76
 
300 PointedEar 77
    text.translate(dictionary)
291 PointedEar 78
 
300 PointedEar 79
#             dmsg("words-translation:", words, min_level=2)
80
#             dmsg("words-translation-reduced:",
81
#                 list(map(
82
#                     lambda word:
83
#                         word['en']
84
#                         if (hasattr(word, "get") and word.get('en', None) is not None)
85
#                         else word,
86
#                     words)),
87
#                 min_level=2)
88
#              for key, value in dictionary._expressions.items():
89
#                  dmsg(key, value, min_level=3)