Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 296 → Rev 295

/trunk/tools/eazytrans/Dictionary.py
32,13 → 32,10
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__)))
59,7 → 56,7
indent = None
 
for line in f:
m = match(r'^\s*{0}:\s*(?P<phrase>.+)'.format(self._language_key), line)
m = match(r'^\s*{0}:\s*(?P<phrase>.+)'.format(language_key), line)
if m is not None:
phrase = m.group("phrase")
self[phrase] = {}
105,16 → 102,15
dmsg(' done ({0} entries).'.format(len(self)), min_level=1)
 
def clean (self):
re_parens = compile(r'\(.+\)', DOTALL)
re_parens_no_alt = compile(r'\(([^|]+)\)', DOTALL)
re_braces = compile(
parens_re = compile(r'\(.+\)', DOTALL)
braces_re = compile(
r'^\s*\{(?P<phrase>.+)\}(?:\s*\((?P<variant>.+?)\))?\s*$',
DOTALL)
re_semicolon = compile(r'\s*;\s*')
semicolon_re = compile(r'\s*;\s*')
 
for orig_phrase, data in list(self.items()):
# if there are optional or alternating parts
if search(re_parens, orig_phrase):
if search(parens_re, orig_phrase):
if orig_phrase.find('|') > -1:
# TODO alternation
pass
124,8 → 120,8
 
if orig_phrase.find(';') > -1:
synonyms = map(
lambda x: sub(re_braces, r'\1', x),
split(re_semicolon, orig_phrase))
lambda x: sub(braces_re, r'\1', x),
split(semicolon_re, orig_phrase))
 
for synonym in synonyms:
self[synonym] = data
132,34 → 128,13
 
del self[orig_phrase]
else:
m = match(re_braces, orig_phrase)
m = match(braces_re, orig_phrase)
if m is not None:
phrase = m.group("phrase")
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
m2 = match(parens_re, phrase)
if m2 is not None:
# TODO alternation and optional parts
pass
 
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
/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,18 → 54,15
 
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 = super().translate(phrase)
translation = dictionary.get(phrase.lower(), None)
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:
96,7 → 93,7
cli_help()
exit(1)
 
text = ' '.join(argv[1:])
text = argv[1]
 
dictionary = VulcanDictionary(dictionary)
dictionary.load('vuh-gol-en.dict.zdb.txt', 'vuh')
149,12 → 146,3
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)