-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn_lstm.py
66 lines (52 loc) · 1.89 KB
/
rnn_lstm.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 09:14:13 2018
@author: gogol
"""
from keras.models import load_model
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import re
import argparse
import sys
#MAX_NO_WORDS = 20000
#MAX_SEQUENCE_LENGTH = 1000
def analyseSentiment(sentences):
model = load_model('model.h5')
for i in range(len(sentences)):
sentences[i] = sentences[i].lower()
sentences[i] = re.sub('[^a-zA-z0-9\s]','', sentences[i])
max_features = 2000
tokenizer = Tokenizer(num_words=max_features, split=' ')
tokenizer.fit_on_texts(sentences)
X = tokenizer.texts_to_sequences(sentences)
X = pad_sequences(X, maxlen=86, dtype='int32', value=0)
results = model.predict_classes(X, batch_size=len(sentences), verbose=0)
# print(results)
# sys.stdout.flush()
return results
# parser = argparse.ArgumentParser()
# parser.add_argument('--texts', nargs='*', type=str)
# args = parser.parse_args()
# analyse(args.texts)
def analyseToxicity(sentences):
for i in range(len(sentences)):
sentences[i] = sentences[i].lower()
sentences[i] = re.sub('[^a-zA-z0-9\s]','', sentences[i])
max_features = 20000
tokenizer = Tokenizer(num_words=max_features, split=' ')
tokenizer.fit_on_texts(sentences)
X = tokenizer.texts_to_sequences(sentences)
X = pad_sequences(X, maxlen=200, dtype='int32', value=0)
model = load_model('toxicity.h5')
results = model.predict(X, batch_size=len(sentences), verbose=0)
probs = np.amax(results, axis=1)
results = np.argmax(results, axis=1)
for i in range(len(results)):
if probs[i] < 0.5:
results[i] = 6
return results
# sents = ['fucking shit, fucking life', 'nigga i want to kill you and eat your cunt', 'good boy']
# print(analyseToxicity(sents))