-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
50 lines (41 loc) · 1.3 KB
/
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
from nltk import word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
from sklearn.externals import joblib
from tabulate import tabulate # pretty table
# Tokenizer that does stemming and strips punctuation
def tokenize(text):
text = re.sub(r'\W+', ' ', text)
tokens = word_tokenize(text)
lemas = []
for item in tokens:
lemas.append(WordNetLemmatizer().lemmatize(item))
return lemas
# Command line arguments
save = sys.argv[1]
file_dir = sys.argv[2]
if __name__ == "__main__":
# Load trained model
print("Loading model...")
clf = joblib.load(save)
print("Model loaded!")
print("\nModel Parameters:")
print(clf.named_steps['clf'].get_params())
# Read file into string and run analysis
with open(file_dir, 'r') as myfile:
data = myfile.read().replace('\n', '')
# Run data through model
predicted = clf.predict([data])
# print(predicted)
certainty = clf.decision_function([data])
# Is it bonkers?
if predicted[0]:
verdict = "Not too nuts!"
else:
verdict = "Bonkers!"
print("\nInput classified as:")
print(tabulate([["Verdict", verdict], ["Certainty", float(certainty)]],
tablefmt="fancy_grid"))