This repository was archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
117 lines (94 loc) · 3.23 KB
/
server.py
File metadata and controls
117 lines (94 loc) · 3.23 KB
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
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split as tts
from operator import itemgetter, attrgetter
from sklearn.metrics import accuracy_score
import time
from functions import *
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
# Allow cross origin request from Javascript
CORS(app)
@app.route('/training', methods=['POST'])
def training():
dictionary = make_keywords_dictionary()
features, labels = make_keywords_dataset(dictionary)
x_train, x_test, y_train, y_test = tts(features, labels)
# Train with Naive Bayes
clf = MultinomialNB()
clf.fit(x_train, y_train)
preds = clf.predict(x_test)
accuracy = accuracy_score(y_test, preds)
filename = 'category-model.classifier'
# delete trained file
delete_file(filename)
save(clf, filename)
return jsonify({
'statusCode': 200,
'data': {
'accuracy_score': str(accuracy)
},
'message': 'Save the classifier from dataset.'
})
@app.route('/classify', methods=['POST'])
def classify():
start_time = str(time.time())
# Do not forget to re-train in case there are new keywords
dictionary = make_keywords_dictionary()
# Get POST params from request
sentence = request.json.get('sentence')
words = sentence.split('')
# Clean words
adjust_words = []
for i in range(0, len(words)):
word = clean_word(words[i])
if word != '':
adjust_words.append(word)
words = adjust_words
features = sentence_to_features(dictionary, words)
result = predict_category(features)
if result[0] > 0:
found_category_id = result[0]
# Display related posts
keyword_posts = []
for keyword in words:
posts = get_posts_by_category_and_keyword(found_category_id, keyword)
keyword_posts.append([len(posts), keyword, posts])
# sort keyword_posts
sort_keyword_posts = sorted(keyword_posts, key=itemgetter(0), reverse=True)
# Update new keyword frequency of found category
news_keywords = {}
for keyword in words:
news_keywords[keyword] = update_keyword_frequency_for_web(found_category_id, keyword)
end_time = str(time.time())
return jsonify({
'statusCode': 200,
'data': {
'news': sort_keyword_posts,
'category': {
'id': str(found_category_id),
'name': result[1]
},
'news_keywords': news_keywords,
'start_time': start_time,
'end_time': end_time
},
'message': 'All found posts'
})
else:
return jsonify({
'statusCode': 404,
'data': {},
'message': 'There is no match category.'
})
# Read from files for more sentences
def get_search_sentences():
search_sentences = []
f = open('test.txt')
if f is not None:
content = f.read()
search_sentences = content.splitlines()
return search_sentences
if __name__ == '__main__':
# Enable debug mode for development
app.run(host='0.0.0.0', port=8080, debug=True)