-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_id_test.py
45 lines (35 loc) · 1.03 KB
/
word_id_test.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
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
import pickle
class Word_Id_Map(object):
idx2w = None
w2idx = None
def __init__(self):
with open('data/idx2w.pkl', 'rb') as f:
self.idx2w = pickle.load(f)
with open('data/w2idx.pkl', 'rb') as f:
self.w2idx = pickle.load(f)
def sentence2ids(self, sentence):
ids = []
for word in sentence:
ids.append(self.w2idx[word])
return ids
def ids2sentence(self, ids):
sentence = []
for id in ids:
sentence.append(self.idx2w[id])
return sentence
def main():
map = Word_Id_Map()
ids = map.sentence2ids(["you", "ok", 'i', 'am', 'ok'])
print(ids)
sentence = map.ids2sentence(ids)
print(sentence)
print(map.idx2w[0])
train_x = np.load('./data/idx_q.npy', mmap_mode='r')
print(train_x)
print(map.ids2sentence(train_x[5]))
train_y = np.load('./data/idx_a.npy', mmap_mode='r')
print(train_y)
print(map.ids2sentence(train_y[5]))
if __name__ == "__main__":
main()