-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtroll_detector.py
33 lines (27 loc) · 968 Bytes
/
troll_detector.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
import re
import nltk
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
model_filepath='trollmodels/Troll-Killer-11-0.849-0.965.model'
model=load_model(model_filepath)
print(model.summary())
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
print("enter sentence")
string=input('>')
def process_input(x):
x=x.lower()
line=''
for word in nltk.word_tokenize(x):
if word not in nltk.corpus.stopwords.words('english') and word.isdigit()==False and re.search(r'[^!@#$%^&*()_.+=]+',word):
line+=word+' '
line=line.rstrip()
sequence=tokenizer.texts_to_sequences(line)[0]
padded=pad_sequences([sequence],maxlen=151)
return padded
processed=process_input(string)
prediction=model.predict_classes(processed)[0][0]
if prediction==1:
print('troll')