-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredictions.py
77 lines (66 loc) · 2.9 KB
/
predictions.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
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
import os, pickle
import numpy as np
tokenizer_path = 'tokenizer'
model_path = 'model'
model_file = os.path.join(model_path, 'movie_sentiment_m1.h5')
tokenizer_file = os.path.join(tokenizer_path, 'tokenizer_m1.pickle')
model = load_model(model_file)
# load tokenizer
with open(tokenizer_file, 'rb') as handle:
tokenizer = pickle.load(handle)
def decode_review(text_list):
word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(text_list)
data = pad_sequences(sequences, maxlen=500)
# decode the words
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decoded_review = ' '.join([reverse_word_index.get(i, '?') for i in sequences[0]])
return decoded_review, data
def review_rating(score, decoded_review):
if float(score) >= 0.9:
print('Review: {}\nSentiment: Strongly Positive\nScore: {}'.format(decoded_review, score))
elif float(score) >= 0.7 and float(score) < 0.9:
print('Review: {}\nSentiment: Positive\nScore: {}'.format(decoded_review, score))
elif float(score) >= 0.5 and float(score) < 0.7:
print('Review: {}\nSentiment: Okay\nScore: {}'.format(decoded_review, score))
else:
print('Review: {}\nSentiment: Negative\nScore: {}'.format(decoded_review, score))
print('\n\n')
def score_review(source=None, file_type=None):
text_list = list()
if isinstance(source, str) and file_type is None:
text_list.append(source)
decoded_review, data = decode_review(text_list)
# make prediction
score = model.predict(data)[0][0]
review_rating(score, decoded_review)
if isinstance(source, list) and file_type is None:
for item in source:
text_list = list()
text_list.append(item)
decoded_review, data = decode_review(text_list)
score = model.predict(data)[0][0]
review_rating(score, decoded_review)
if isinstance(source, str) and file_type == 'file':
file_data = open(source).read()
text_list.append(file_data)
decoded_review, data = decode_review(text_list)
# make prediction
score = model.predict(data)[0][0]
review_rating(score, decoded_review)
if isinstance(source, str) and file_type == 'dir':
file_content_holder = list()
for fname in os.listdir(source):
if fname[-4:] == '.txt':
f = open(os.path.join(source, fname))
file_content_holder.append(f.read())
f.close()
for item in file_content_holder:
text_list = list()
text_list.append(item)
decoded_review, data = decode_review(text_list)
score = model.predict(data)[0][0]
review_rating(score, decoded_review)
score_review('test_dir/test', file_type='dir')