Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 298 → Rev 300

/trunk/tools/eazytrans/Dictionary.py
13,21 → 13,19
 
debug_level = 2
 
def dmsg(*args, **kwargs):
if not hasattr(kwargs, 'min_level') or kwargs['min_level'] is None:
kwargs['min_level'] = 1
 
if not hasattr(kwargs, 'file'):
def dmsg (*args, **kwargs):
if not kwargs.get('file'):
kwargs['file'] = stderr
 
if debug_level >= kwargs['min_level']:
del kwargs['min_level']
min_level = kwargs.pop('min_level', 1)
 
if debug_level >= min_level:
print(*args, **kwargs)
 
def sort_dict_alnum_english_key(phrase):
def sort_dict_alnum_english_key (phrase):
return sub(r'\{(.+)\}', r'\1', phrase[0]).lower()
 
class Dictionary(dict):
class Dictionary (dict):
"""
A Dictionary (not to be confused with its ancestor, dict)
represents a word dictionary stored in a file.
37,7 → 35,7
_keys = "ipa|en|lit|pos|com|tag|ex"
_expressions = {}
 
def load (self, dictionary_file, language_key='en'):
def load (self, dictionary_file, keys=None, language_key=None):
"""
Loads a word dictionary from a file.
:param dictionary_file:
45,8 → 43,12
:param language_key:
:type language_key:
"""
self._language_key = language_key
if keys is not None:
self._keys = keys
 
if language_key is not None:
self._language_key = language_key
 
dmsg('Loading dictionary '.format(dictionary_file), end='', min_level=1)
 
chdir(dirname(realpath(__file__)))
60,6 → 62,7
 
if pickle_mtime is None or stat(dictionary_file).st_mtime > pickle_mtime:
dmsg('from {0} ...'.format(dictionary_file), end='', min_level=1)
 
phrase = None
key = None
value = None
101,11 → 104,14
self[phrase][key] = ' '.join(value)
 
dmsg('\nSaving pickle {0} ...'.format(pickle_file), end='', min_level=1)
 
# TODO: Pickle should only contain strings to be small
with open(pickle_file, mode='wb') as f: dump(self, f)
 
dmsg(' done.', min_level=1)
else:
dmsg('from {0} ...'.format(pickle_file), end='', min_level=1)
 
with open(pickle_file, mode='rb') as f: pickle = load(f)
for key, value in pickle.items():
self[key] = value