Subversion Repositories LCARS

Rev

Rev 292 | Rev 294 | Go to most recent revision | 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
293 PointedEar 9
from re import findall, DOTALL, match, sub, compile, \
10
    escape, search
11
from os.path import basename
291 PointedEar 12
from functools import cmp_to_key
293 PointedEar 13
from Dictionary import Dictionary, dmsg, \
14
    sort_dict_alnum_english_key
291 PointedEar 15
 
16
dictionary = {}
17
 
18
prepositions = {
19
    "fi'": 'on',
20
    "na'": 'at|to',
21
    "t'": 'of'
22
}
23
 
292 PointedEar 24
def cli_help():
25
    print('Usage: {0} TEXT...'.format(basename(argv[0])))
26
 
291 PointedEar 27
def get_sort_dict_alnum_vulcan_key():
28
    letters = list(map(str.lower, [
29
        " ", 'S', 'T', 'P', 'K', 'R', 'L', 'A', 'Sh', 'O', 'U', 'D',
30
        'V', 'Kh', 'E', 'H', 'G', 'Ch', 'I', 'N', 'Zh', 'M', 'Y', 'F', 'Z',
31
        'Th', 'W', 'B', "'", '-']))
32
    letter_values = dict(map(lambda x: (x[1], x[0]), enumerate(letters)))
33
    letters_re = compile(r'(?:{0})'.format('|'.join(sorted(letters, key=lambda char:-len(char)))))
34
 
35
    def sort_dict_alnum_vulcan (a, b):
36
        # split into Vulcan letters
37
        a = findall(letters_re, sort_dict_alnum_english_key(a))
38
        b = findall(letters_re, sort_dict_alnum_english_key(b))
39
 
40
        if len(a) < len(b):
41
            for index, char in enumerate(a):
42
                diff = letter_values[char] - letter_values[b[index]]
43
                if diff != 0:
44
                    return diff
45
            return -1
46
 
47
        # len(b) <= len(a)
48
        for index, char in enumerate(b):
49
            diff = letter_values[a[index]] - letter_values[char]
50
            if diff != 0:
51
                return diff
52
 
53
        return 1 if len(b) < len(a) else 0
54
 
55
    return cmp_to_key(sort_dict_alnum_vulcan)
56
 
293 PointedEar 57
class VulcanDictionary(Dictionary):
58
    def translate (self, phrase, search_prefix=True, search_plural=True):
59
        dictionary = self
291 PointedEar 60
 
293 PointedEar 61
        translation = dictionary.get(phrase.lower(), None)
62
        if translation is not None:
63
            translation['vuh'] = phrase
64
            return translation
65
        else:
66
            if search_prefix:
67
                # find prefix
68
                for preposition in prepositions:
69
                    prefix = match(escape(preposition), phrase)
70
                    if prefix is not None:
71
                        prefix_translation = self.translate(prefix.group(0))
72
                        if prefix_translation is not None:
73
                            tail = sub(preposition, '', phrase)
74
                            tail_translation = self.translate(tail, search_prefix=False)
75
                            if tail_translation is not None:
76
                                return [prefix_translation, tail_translation]
77
            elif search_plural:
78
                # find plural
79
                suffix = search(r'lar$', phrase)
80
                if suffix is not None:
81
                    head = sub(r'lar$', '', phrase)
82
                    head_translation = self.translate(head, search_prefix=False, search_plural=False)
83
                    if head_translation is not None:
84
                        head_translation = dict(head_translation)
85
                        head_translation['en'] += ' (pl.)'
86
                        return head_translation
291 PointedEar 87
 
293 PointedEar 88
        return None
89
 
292 PointedEar 90
if __name__ == '__main__':
91
    if len(argv) < 2:
92
        print('Nothing to translate.', end='\n\n', file=stderr)
93
        cli_help()
94
        exit(1)
291 PointedEar 95
 
292 PointedEar 96
    text = argv[1]
97
 
293 PointedEar 98
    dictionary = VulcanDictionary(dictionary)
99
    dictionary.load('vuh-gol-en.dict.zdb.txt')
100
    dictionary.clean()
291 PointedEar 101
 
102
#     try:
103
#         for phrase, data in OrderedDict(sorted(
104
#             dictionary.items(),
105
#             key=get_sort_dict_alnum_vulcan_key()
106
#         )).items():
107
#             print(phrase, "=", data)
108
#     except BrokenPipeError:
109
#         pass
110
 
293 PointedEar 111
    dmsg("text:", text, min_level=2)
291 PointedEar 112
    sentences = findall(r'(?!\s+)(?:.+?\.{1,3}|.+$)', text, DOTALL)
292 PointedEar 113
    dmsg("sentences:", sentences, min_level=2)
291 PointedEar 114
    for sentence in sentences:
292 PointedEar 115
        dmsg("sentence:", sentence, min_level=2)
291 PointedEar 116
 
293 PointedEar 117
        clauses = findall(r'(?!\s+)(?:.+?(?:\s+-\s*|\s*[–—]\s*|\.{1,3}|.+$))', sentence, DOTALL)
292 PointedEar 118
        dmsg("clauses:", clauses, min_level=2)
119
        for clause in clauses:
120
            dmsg("clause:", clause, min_level=2)
291 PointedEar 121
 
292 PointedEar 122
            words = findall(r'[^\s.]+', clause)
123
            dmsg("words:", words, min_level=2)
291 PointedEar 124
 
292 PointedEar 125
            offset = 0
126
            while offset < len(words):
127
                translation = None
291 PointedEar 128
 
293 PointedEar 129
                for i in range(len(words), offset, -1):
130
                    dmsg("words[{0}:{1}] = {2}".format(offset, i, words[offset:i]), min_level=2)
292 PointedEar 131
                    phrase = ' '.join(words[offset:i])
291 PointedEar 132
 
292 PointedEar 133
                    dmsg("phrase:", phrase, min_level=2)
134
 
293 PointedEar 135
                    translation = dictionary.translate(phrase)
292 PointedEar 136
 
137
                    if translation is not None:
138
                        dmsg("phrase-translation:", translation, min_level=2)
139
                        dmsg("words[{0}:{1}] = [\"{2}\"]".format(offset, i, translation), min_level=2)
140
                        words[offset:i] = [translation]
293 PointedEar 141
                        offset += i - offset
292 PointedEar 142
                        break
143
 
144
                if translation is None:
145
                    dmsg("phrase-translation:", translation, min_level=2)
146
                    offset += 1
147
 
148
            dmsg("words-translation:", words, min_level=2)