forked from MiuLab/KB-InfoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie_dict.py
34 lines (27 loc) · 852 Bytes
/
movie_dict.py
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
30
31
32
33
34
'''
'''
import cPickle as pickle
import copy
import nltk
import string
from collections import defaultdict
from deep_dialog.tools import to_tokens
class MovieDict:
def __init__(self, path):
self.load_dict(path)
self.count_values()
self._build_token_index()
def load_dict(self, path):
dict_data = pickle.load(open(path, 'rb'))
self.dict = copy.deepcopy(dict_data)
def count_values(self):
self.lengths = {}
for k,v in self.dict.iteritems():
self.lengths[k] = len(v)
def _build_token_index(self):
self.tokens = {}
for slot,vals in self.dict.iteritems():
self.tokens[slot] = defaultdict(list)
for vi,vv in enumerate(vals):
w_v = to_tokens(vv)
for w in w_v: self.tokens[slot][w].append(vi)