Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 296 → Rev 297

/trunk/tools/eazytrans/vuh.py
55,14 → 55,24
return cmp_to_key(sort_dict_alnum_vulcan)
 
class VulcanDictionary (Dictionary):
"""
"""
def translate (self, phrase, search_prefix=True, search_plural=True):
dictionary = self
 
"""
:param phrase:
:type phrase:
:param search_prefix:
:type search_prefix:
:param search_plural:
:type search_plural:
"""
translation = super().translate(phrase)
if translation is not None:
return translation
else:
expr_translation = dictionary.translate_expression(phrase)
expr_translation = self.translate_expression(phrase)
if expr_translation is not None:
return expr_translation
 
90,6 → 100,17
 
return None
 
def clean_entry(self, phrase):
"""
Replace GV Media Script parens with FSE parens
:param phrase:
:type phrase:
"""
return sub(
r'(\([^)]*\))|\|([^|)]+)\|',
lambda m: '({0})'.format(m.group(2)) if m.group(2) else m.group(1),
phrase)
 
if __name__ == '__main__':
if len(argv) < 2:
print('Nothing to translate.', end='\n\n', file=stderr)
157,4 → 178,5
else word,
words)),
min_level=2)
# dmsg(dictionary._expressions)
# for key, value in dictionary._expressions.items():
# dmsg(key, value, min_level=3)
/trunk/tools/eazytrans/Dictionary.py
29,7 → 29,8
 
class Dictionary(dict):
"""
classdocs
A Dictionary (not to be confused with its ancestor, dict)
represents a word dictionary stored in a file.
"""
_language_key = 'en'
37,6 → 38,13
_expressions = {}
 
def load (self, dictionary_file, language_key='en'):
"""
Loads a word dictionary from a file.
:param dictionary_file:
:type dictionary_file:
:param language_key:
:type language_key:
"""
self._language_key = language_key
 
dmsg('Loading dictionary '.format(dictionary_file), end='', min_level=1)
105,8 → 113,11
dmsg(' done ({0} entries).'.format(len(self)), min_level=1)
 
def clean (self):
"""
Cleans dictionary entries
"""
re_parens = compile(r'\(.+\)', DOTALL)
re_parens_no_alt = compile(r'\(([^|]+)\)', DOTALL)
re_parens_no_alt = compile(r'\(([^\|]+)\)', DOTALL)
re_braces = compile(
r'^\s*\{(?P<phrase>.+)\}(?:\s*\((?P<variant>.+?)\))?\s*$',
DOTALL)
134,7 → 145,11
else:
m = match(re_braces, orig_phrase)
if m is not None:
phrase = m.group("phrase")
phrase = m.group('phrase')
 
if callable(getattr(self, 'clean_entry', None)):
phrase = self.clean_entry(phrase)
 
m_parens = search(re_parens, phrase)
if m_parens is not None:
# alternation and optional parts
148,6 → 163,13
del self[orig_phrase]
 
def translate (self, phrase):
"""
Translate a phrase according to this dictionary.
For language-specific processing, this method should be
called/overridden by inheriting classes.
:param phrase:
:type phrase: str
"""
translation = self.get(phrase.lower(), None)
if translation is not None:
translation[self._language_key] = phrase
156,7 → 178,13
return None
 
def translate_expression (self, phrase):
for expression, data in list(self._expressions.items()):
"""
Translate a phrase according entries in this dictionary
based on regular expressions.
:param phrase:
:type phrase:
"""
for expression, data in sorted(self._expressions.items(), key=lambda item:-len(item[1])):
expression_match = match(expression, phrase)
if expression_match is not None:
data[self._language_key] = expression_match.group(0)