Subversion Repositories LCARS

Compare Revisions

Last modification

Ignore whitespace Rev 300 → Rev 298

/trunk/tools/eazytrans/Dictionary.py
13,19 → 13,21
 
debug_level = 2
 
def dmsg (*args, **kwargs):
if not kwargs.get('file'):
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'):
kwargs['file'] = stderr
 
min_level = kwargs.pop('min_level', 1)
 
if debug_level >= min_level:
if debug_level >= kwargs['min_level']:
del kwargs['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.
35,7 → 37,7
_keys = "ipa|en|lit|pos|com|tag|ex"
_expressions = {}
 
def load (self, dictionary_file, keys=None, language_key=None):
def load (self, dictionary_file, language_key='en'):
"""
Loads a word dictionary from a file.
:param dictionary_file:
43,12 → 45,8
:param language_key:
:type language_key:
"""
if keys is not None:
self._keys = keys
self._language_key = language_key
 
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__)))
62,7 → 60,6
 
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
104,14 → 101,11
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