forked from puke3615/MachineCoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordsmanager.py
More file actions
29 lines (22 loc) · 706 Bytes
/
wordsmanager.py
File metadata and controls
29 lines (22 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import pickle
import config
import os
class WordsInfo:
words = []
index2word = {}
def __init__(self, words, index2word):
self.words = words
self.index2word = index2word
def dump(words, index2word):
with open(config.WORDS_PATH, 'wb+') as f:
dumpStr = pickle.dumps(WordsInfo(words, index2word))
pickle.dump(dumpStr, f)
def parse():
path = config.WORDS_PATH
if not os.path.exists(path):
raise RuntimeError('File "%s" not found' % path)
with open(path, 'rb') as f:
info = pickle.loads(pickle.load(f))
if info is None:
raise RuntimeError('Load WordsInfo failure.')
return info.words, info.index2word