-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathhello.py
47 lines (40 loc) · 1.16 KB
/
hello.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
46
47
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Shared by http://www.tensorflownews.com/
# Github: https://github.com/TensorFlowNews
import tensorflow as tf
import numpy as np
import pickle
class Word_Id_Map(object):
idx2w=None
w2idx=None
def __init__(self):
with open('idx2w.pkl', 'rb') as f:
self.idx2w=pickle.load(f)
with open('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(['hello','world',"are","you","ok",'i','am','ok'])
print(ids)
sentence=map.ids2sentence(ids)
print(sentence)
print(map.idx2w[0])
train_x = np.load('./idx_q.npy', mmap_mode='r')
print(train_x)
print(map.ids2sentence(train_x[5]))
train_y = np.load('./idx_a.npy', mmap_mode='r')
print(train_y)
print(map.ids2sentence(train_y[5]))
if __name__=="__main__":
main()