Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 295 → Rev 296

/trunk/tools/eazytrans/vuh.py
24,7 → 24,7
def cli_help():
print('Usage: {0} TEXT...'.format(basename(argv[0])))
 
def get_sort_dict_alnum_vulcan_key():
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',
54,15 → 54,18
 
return cmp_to_key(sort_dict_alnum_vulcan)
 
class VulcanDictionary(Dictionary):
class VulcanDictionary (Dictionary):
def translate (self, phrase, search_prefix=True, search_plural=True):
dictionary = self
 
translation = dictionary.get(phrase.lower(), None)
translation = super().translate(phrase)
if translation is not None:
translation['vuh'] = phrase
return translation
else:
expr_translation = dictionary.translate_expression(phrase)
if expr_translation is not None:
return expr_translation
 
if search_prefix:
# find prefix
for preposition in prepositions:
93,7 → 96,7
cli_help()
exit(1)
 
text = argv[1]
text = ' '.join(argv[1:])
 
dictionary = VulcanDictionary(dictionary)
dictionary.load('vuh-gol-en.dict.zdb.txt', 'vuh')
146,3 → 149,12
offset += 1
 
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)
# dmsg(dictionary._expressions)
/trunk/tools/eazytrans/Dictionary.py
32,10 → 32,13
classdocs
"""
_language_key = 'en'
_keys = "ipa|en|lit|pos|com|tag|ex"
_expressions = {}
 
def load (self, dictionary_file, language_key='en'):
self._language_key = language_key
 
dmsg('Loading dictionary '.format(dictionary_file), end='', min_level=1)
 
chdir(dirname(realpath(__file__)))
56,7 → 59,7
indent = None
 
for line in f:
m = match(r'^\s*{0}:\s*(?P<phrase>.+)'.format(language_key), line)
m = match(r'^\s*{0}:\s*(?P<phrase>.+)'.format(self._language_key), line)
if m is not None:
phrase = m.group("phrase")
self[phrase] = {}
102,15 → 105,16
dmsg(' done ({0} entries).'.format(len(self)), min_level=1)
 
def clean (self):
parens_re = compile(r'\(.+\)', DOTALL)
braces_re = compile(
re_parens = compile(r'\(.+\)', DOTALL)
re_parens_no_alt = compile(r'\(([^|]+)\)', DOTALL)
re_braces = compile(
r'^\s*\{(?P<phrase>.+)\}(?:\s*\((?P<variant>.+?)\))?\s*$',
DOTALL)
semicolon_re = compile(r'\s*;\s*')
re_semicolon = compile(r'\s*;\s*')
 
for orig_phrase, data in list(self.items()):
# if there are optional or alternating parts
if search(parens_re, orig_phrase):
if search(re_parens, orig_phrase):
if orig_phrase.find('|') > -1:
# TODO alternation
pass
120,8 → 124,8
 
if orig_phrase.find(';') > -1:
synonyms = map(
lambda x: sub(braces_re, r'\1', x),
split(semicolon_re, orig_phrase))
lambda x: sub(re_braces, r'\1', x),
split(re_semicolon, orig_phrase))
 
for synonym in synonyms:
self[synonym] = data
128,13 → 132,34
 
del self[orig_phrase]
else:
m = match(braces_re, orig_phrase)
m = match(re_braces, orig_phrase)
if m is not None:
phrase = m.group("phrase")
m2 = match(parens_re, phrase)
if m2 is not None:
# TODO alternation and optional parts
pass
m_parens = search(re_parens, phrase)
if m_parens is not None:
# alternation and optional parts
expr = sub(re_parens_no_alt, r'(?:\1)?', phrase)
expr = sub('~', '(?=.)', expr)
self._expressions[expr] = data
else:
# remove braces
self[phrase] = data
 
self[phrase] = data
del self[orig_phrase]
 
def translate (self, phrase):
translation = self.get(phrase.lower(), None)
if translation is not None:
translation[self._language_key] = phrase
return translation
 
return None
 
def translate_expression (self, phrase):
for expression, data in list(self._expressions.items()):
expression_match = match(expression, phrase)
if expression_match is not None:
data[self._language_key] = expression_match.group(0)
return data
 
return None