Rev 298 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 298 | Rev 300 | ||
---|---|---|---|
Line 11... | Line 11... | ||
11 | from pickle import dump, load |
11 | from pickle import dump, load |
12 | from re import match, DOTALL, search, sub, split, compile |
12 | from re import match, DOTALL, search, sub, split, compile |
13 | 13 | ||
14 | debug_level = 2 |
14 | debug_level = 2 |
15 | 15 | ||
16 | def dmsg(*args, **kwargs): |
16 | def dmsg (*args, **kwargs): |
17 | if not hasattr(kwargs, 'min_level') or kwargs['min_level'] is None: |
- | |
18 | kwargs['min_level'] = 1 |
- | |
19 | - | ||
20 | if not hasattr(kwargs, 'file'): |
17 | if not kwargs.get('file'): |
21 | kwargs['file'] = stderr |
18 | kwargs['file'] = stderr |
22 | 19 | ||
23 | if debug_level >= kwargs['min_level']: |
20 | min_level = kwargs.pop('min_level', 1) |
- | 21 | ||
24 | del kwargs['min_level'] |
22 | if debug_level >= min_level: |
25 | print(*args, **kwargs) |
23 | print(*args, **kwargs) |
26 | 24 | ||
27 | def sort_dict_alnum_english_key(phrase): |
25 | def sort_dict_alnum_english_key (phrase): |
28 | return sub(r'\{(.+)\}', r'\1', phrase[0]).lower() |
26 | return sub(r'\{(.+)\}', r'\1', phrase[0]).lower() |
29 | 27 | ||
30 | class Dictionary(dict): |
28 | class Dictionary (dict): |
31 | """
|
29 | """
|
32 | A Dictionary (not to be confused with its ancestor, dict)
|
30 | A Dictionary (not to be confused with its ancestor, dict)
|
33 | represents a word dictionary stored in a file.
|
31 | represents a word dictionary stored in a file.
|
34 |
|
32 |
|
35 | """
|
33 | """
|
36 | _language_key = 'en' |
34 | _language_key = 'en' |
37 | _keys = "ipa|en|lit|pos|com|tag|ex" |
35 | _keys = "ipa|en|lit|pos|com|tag|ex" |
38 | _expressions = {} |
36 | _expressions = {} |
39 | 37 | ||
40 | def load (self, dictionary_file, language_key='en'): |
38 | def load (self, dictionary_file, keys=None, language_key=None): |
41 | """
|
39 | """
|
42 | Loads a word dictionary from a file.
|
40 | Loads a word dictionary from a file.
|
43 | :param dictionary_file:
|
41 | :param dictionary_file:
|
44 | :type dictionary_file:
|
42 | :type dictionary_file:
|
45 | :param language_key:
|
43 | :param language_key:
|
46 | :type language_key:
|
44 | :type language_key:
|
47 | """
|
45 | """
|
- | 46 | if keys is not None: |
|
- | 47 | self._keys = keys |
|
- | 48 | ||
- | 49 | if language_key is not None: |
|
48 | self._language_key = language_key |
50 | self._language_key = language_key |
49 | 51 | ||
50 | dmsg('Loading dictionary '.format(dictionary_file), end='', min_level=1) |
52 | dmsg('Loading dictionary '.format(dictionary_file), end='', min_level=1) |
51 | 53 | ||
52 | chdir(dirname(realpath(__file__))) |
54 | chdir(dirname(realpath(__file__))) |
53 | 55 | ||
Line 58... | Line 60... | ||
58 | except FileNotFoundError:
|
60 | except FileNotFoundError:
|
59 | pickle_mtime = None |
61 | pickle_mtime = None |
60 | 62 | ||
61 | if pickle_mtime is None or stat(dictionary_file).st_mtime > pickle_mtime: |
63 | if pickle_mtime is None or stat(dictionary_file).st_mtime > pickle_mtime: |
62 | dmsg('from {0} ...'.format(dictionary_file), end='', min_level=1) |
64 | dmsg('from {0} ...'.format(dictionary_file), end='', min_level=1) |
- | 65 | ||
63 | phrase = None |
66 | phrase = None |
64 | key = None |
67 | key = None |
65 | value = None |
68 | value = None |
66 | with open(dictionary_file) as f: |
69 | with open(dictionary_file) as f: |
67 | indent = None |
70 | indent = None |
Line 99... | Line 102... | ||
99 | # join last value if necessary
|
102 | # join last value if necessary
|
100 | if type(value) == list: |
103 | if type(value) == list: |
101 | self[phrase][key] = ' '.join(value) |
104 | self[phrase][key] = ' '.join(value) |
102 | 105 | ||
103 | dmsg('\nSaving pickle {0} ...'.format(pickle_file), end='', min_level=1) |
106 | dmsg('\nSaving pickle {0} ...'.format(pickle_file), end='', min_level=1) |
- | 107 | ||
104 | # TODO: Pickle should only contain strings to be small
|
108 | # TODO: Pickle should only contain strings to be small
|
105 | with open(pickle_file, mode='wb') as f: dump(self, f) |
109 | with open(pickle_file, mode='wb') as f: dump(self, f) |
- | 110 | ||
106 | dmsg(' done.', min_level=1) |
111 | dmsg(' done.', min_level=1) |
107 | else:
|
112 | else:
|
108 | dmsg('from {0} ...'.format(pickle_file), end='', min_level=1) |
113 | dmsg('from {0} ...'.format(pickle_file), end='', min_level=1) |
- | 114 | ||
109 | with open(pickle_file, mode='rb') as f: pickle = load(f) |
115 | with open(pickle_file, mode='rb') as f: pickle = load(f) |
110 | for key, value in pickle.items(): |
116 | for key, value in pickle.items(): |
111 | self[key] = value |
117 | self[key] = value |
112 | 118 | ||
113 | dmsg(' done ({0} entries).'.format(len(self)), min_level=1) |
119 | dmsg(' done ({0} entries).'.format(len(self)), min_level=1) |