This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws.py
63 lines (51 loc) · 2.01 KB
/
ws.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
import sys
import websocket
import json
import classify
import bodyfetcher
from termcolor import cprint
prior_spam = 0.07
prior_ham = 0.93
def main():
try:
clf = classify.NaiveBayesClassifier(prior_spam, prior_ham)
except Exception as ex:
print(ex)
sys.exit(1)
ws = websocket.create_connection("ws://qa.sockets.stackexchange.com/")
ws.send("155-questions-active")
while True:
try:
recv_data = ws.recv()
print("Received data from realtime websocket.")
recv_json = json.loads(recv_data)
post_data = json.loads(recv_json['data'])
cprint("ID: {0} on site: {1}".format(post_data['id'], post_data['siteBaseHostAddress']), 'cyan')
full_data = bodyfetcher.fetch_post(post_data['siteBaseHostAddress'], post_data['id'])
print("Fetched full post data from API; classifying post.")
run_classifier(clf, full_data)
if 'answers' in full_data:
print("Classifying answers...")
for answer in full_data['answers']:
answer_obj = {
'title': full_data['title'],
'body': answer['body']
}
run_classifier(clf, answer_obj)
else:
print("no answers")
print("")
except Exception as ex:
cprint("Exception while running classification process on received data. {0}".format(ex), 'yellow')
def run_classifier(clf, full_data):
clobj = {
'title': full_data['title'],
'body': full_data['body']
}
status, spam_score, ham_score = clf.classify(clobj, ['title', 'body'])
if status == True:
cprint("NB Classifier result: spamstatus={0}, spamscore={1}, hamscore={2}".format(status, spam_score, ham_score), 'red')
else:
cprint("NB Classifier result: spamstatus={0}, spamscore={1}, hamscore={2}".format(status, spam_score, ham_score), 'white')
if __name__ == "__main__":
main()