-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtext_predict.py
155 lines (134 loc) · 4.49 KB
/
text_predict.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#encoding:utf-8
from text_model import *
import tensorflow as tf
import tensorflow.contrib.keras as kr
import os
import numpy as np
import jieba
import re
import heapq
import codecs
def predict(sentences):
config = TextConfig()
config.pre_trianing = get_training_word2vec_vectors(config.vector_word_npz)
model = TextRNN(config)
save_dir = './checkpoints/textrnn'
save_path = os.path.join(save_dir, 'best_validation')
_,word_to_id=read_vocab(config.vocab_filename)
input_x= process_file(sentences,word_to_id,max_length=config.seq_length)
labels = {0:'体育',
1:'财经',
2:'房产',
3:'家居',
4:'教育',
5:'科技',
6:'时尚',
7:'时政',
8:'游戏',
9:'娱乐'
}
feed_dict = {
model.input_x: input_x,
model.keep_prob: 1,
model.sequence_lengths: get_sequence_length(input_x)
}
session = tf.Session()
session.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess=session, save_path=save_path)
y_prob=session.run(tf.nn.softmax(model.logits), feed_dict=feed_dict)
y_prob=y_prob.tolist()
cat=[]
for prob in y_prob:
top2= list(map(prob.index, heapq.nlargest(1, prob)))
cat.append(labels[top2[0]])
tf.reset_default_graph()
return cat
def sentence_cut(sentences):
"""
Args:
sentence: a list of text need to segment
Returns:
seglist: a list of sentence cut by jieba
"""
re_han = re.compile(u"([\u4E00-\u9FD5a-zA-Z0-9+#&\._%]+)") # the method of cutting text by punctuation
with codecs.open('./data/stopwords.txt','r',encoding='utf-8') as f:
stopwords=[line.strip() for line in f.readlines()]
contents=[]
for sentence in sentences:
words=[]
blocks = re_han.split(sentence)
for blk in blocks:
if re_han.match(blk):
seglist = jieba.lcut(blk)
words.extend([w for w in seglist if w not in stopwords])
contents.append(words)
return contents
def process_file(sentences,word_to_id,max_length=600):
"""
Args:
sentence: a text need to predict
word_to_id:get from def read_vocab()
max_length:allow max length of sentence
Returns:
x_pad: sequence data from preprocessing sentence
"""
data_id=[]
seglist=sentence_cut(sentences)
for i in range(len(seglist)):
data_id.append([word_to_id[x] for x in seglist[i] if x in word_to_id])
x_pad=kr.preprocessing.sequence.pad_sequences(data_id,max_length)
return x_pad
def read_vocab(vocab_dir):
"""
Args:
filename:path of vocab_filename
Returns:
words: a list of vocab
word_to_id: a dict of word to id
"""
words = codecs.open(vocab_dir, 'r', encoding='utf-8').read().strip().split('\n')
word_to_id = dict(zip(words, range(len(words))))
return words, word_to_id
def get_training_word2vec_vectors(filename):
"""
Args:
filename:numpy file
Returns:
data["embeddings"]: a matrix of vocab vector
"""
with np.load(filename) as data:
return data["embeddings"]
def get_sequence_length(x_batch):
"""
Args:
x_batch:a batch of input_data
Returns:
sequence_lenghts: a list of acutal length of every senuence_data in input_data
"""
sequence_lengths=[]
for x in x_batch:
actual_length = np.sum(np.sign(x))
sequence_lengths.append(actual_length)
return sequence_lengths
if __name__ == '__main__':
print('predict random five samples in test data.... ')
import random
sentences=[]
labels=[]
with codecs.open('./data/cnews.test.txt','r',encoding='utf-8') as f:
sample=random.sample(f.readlines(),5)
for line in sample:
try:
line=line.rstrip().split('\t')
assert len(line)==2
sentences.append(line[1])
labels.append(line[0])
except:
pass
cat=predict(sentences)
for i,sentence in enumerate(sentences,0):
print ('----------------------the text-------------------------')
print (sentence[:50]+'....')
print('the orginal label:%s'%labels[i])
print('the predict label:%s'%cat[i])