Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 297 → Rev 296

/trunk/tools/eazytrans/vuh.py
55,24 → 55,14
return cmp_to_key(sort_dict_alnum_vulcan)
 
class VulcanDictionary (Dictionary):
"""
"""
def translate (self, phrase, search_prefix=True, search_plural=True):
"""
:param phrase:
:type phrase:
:param search_prefix:
:type search_prefix:
:param search_plural:
:type search_plural:
"""
dictionary = self
 
translation = super().translate(phrase)
if translation is not None:
return translation
else:
expr_translation = self.translate_expression(phrase)
expr_translation = dictionary.translate_expression(phrase)
if expr_translation is not None:
return expr_translation
 
100,17 → 90,6
 
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)
178,5 → 157,4
else word,
words)),
min_level=2)
# for key, value in dictionary._expressions.items():
# dmsg(key, value, min_level=3)
# dmsg(dictionary._expressions)
/trunk/tools/eazytrans/Dictionary.py
29,8 → 29,7
 
class Dictionary(dict):
"""
A Dictionary (not to be confused with its ancestor, dict)
represents a word dictionary stored in a file.
classdocs
"""
_language_key = 'en'
38,13 → 37,6
_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)
113,11 → 105,8
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)
145,11 → 134,7
else:
m = match(re_braces, orig_phrase)
if m is not None:
phrase = m.group('phrase')
 
if callable(getattr(self, 'clean_entry', None)):
phrase = self.clean_entry(phrase)
 
phrase = m.group("phrase")
m_parens = search(re_parens, phrase)
if m_parens is not None:
# alternation and optional parts
163,13 → 148,6
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
178,13 → 156,7
return None
 
def translate_expression (self, phrase):
"""
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])):
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)