This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collecter_trainer.py
148 lines (121 loc) · 6.53 KB
/
collecter_trainer.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
import json
import nltk
import numpy
import os
import pickle
import praw
import random
import re
import signal
import sys
import tensorflow
import tflearn
import time
import traceback
from nltk.stem.lancaster import LancasterStemmer
from termcolor import colored, cprint
from time import sleep
CONST_REG = r'(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’])|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)\b/?(?!@)))'
def main():
with open("settings.json") as jsonFile1:
cfg = json.load(jsonFile1)
with open('training/intents.json') as jsonFile2:
data = json.load(jsonFile2)
with open("model/data.pickle", "rb") as p:
words, labels, training, output = pickle.load(p)
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
reddit = praw.Reddit(cfg['praw']['cred'])
sub = reddit.subreddit(cfg['praw']['sub'])
color = {
"ACCEPTABLE": "green",
"NEUTRAL": "white",
"POSSIBLE WARNING": "red"
}
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
stemmer = LancasterStemmer()
tensorflow.reset_default_graph()
net = tflearn.input_data(shape=[None, len(training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
model.load("model/model.tflearn")
os.system('cls' if os.name == 'nt' else 'clear')
print("\n Ready\n")
commentStream = sub.stream.comments(skip_existing=cfg['praw']['skipExisting'])
try:
for comment in commentStream:
raw = " ".join(comment.body.lower().splitlines())
raw = re.sub(CONST_REG, ' ', raw, flags=re.MULTILINE)
raw = re.sub(r'([\'’])', '', raw)
raw = re.sub(r'[^a-z ]', ' ', raw)
raw = re.sub(r'[ ]+', ' ', raw.strip())
inp = re.sub(r'( x b )|( nbsp )', ' ', raw)
user = comment.author.name
link = comment.permalink.replace(re.search(r'/r/[\w]+/comments/[\w\d]+/([\w\d_]+)/[\w\d]+/', comment.permalink).group(1), '-', 1)
if (len(inp) <= 0):
continue
results = model.predict([bag_of_words(inp, words)])[0]
results_index = numpy.argmax(results)
tag = labels[results_index]
confidence = results[results_index] * 100
if (results[results_index] > cfg['model']['confidence']):
for tg in data["intents"]:
if tg['tag'] == tag:
classification = tg['classification']
print(f'\n{inp}')
cprint(f'\n [{confidence:0.3f}% {classification}]', color[classification])
print(f' By: {user}\n http://reddit.com{link}\n')
else:
print(f'\n{inp}')
cprint(f'\n [UNSURE {confidence:0.3f}% {tag}]', 'cyan')
print(f' By: {user}\n http://reddit.com{link}\n')
category = input(" Category: ")
if (category.lower() == cfg['keyBindings']['acceptable']):
cat = 0
print(" ACCEPTABLE")
elif (category.lower() == cfg['keyBindings']['neutral']):
cat = 1
print(" NEUTRAL")
elif (category.lower() == cfg['keyBindings']['warning']):
cat = 2
print(" POSSIBLE WARNING")
else:
print(" Entry Rejected")
continue
with open("training/intents.json", "r+") as jsonFile2:
tmp = json.load(jsonFile2)
tmp['intents'][cat]['patterns'].append(inp)
jsonFile2.seek(0)
json.dump(tmp, jsonFile2)
jsonFile2.truncate()
except KeyboardInterrupt:
sys.exit(1)
except Exception as e:
print(f'EXCEPTION:\n{e}')
sleep(10)
def exit_gracefully(signum, frame):
signal.signal(signal.SIGINT, original_sigint)
try:
if input("\nDo you really want to quit? (y/n)> ").lower().startswith('y'):
sys.exit(1)
except KeyboardInterrupt:
print("\nQuitting")
sys.exit(1)
signal.signal(signal.SIGINT, exit_gracefully)
if __name__ == "__main__":
original_sigint = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, exit_gracefully)
main()