-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
113 lines (93 loc) · 3.13 KB
/
app.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
from flask import Flask, jsonify
from flask_cors import CORS
from markupsafe import escape
from generated_doa_list import generated_doa_list
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
import operator
app = Flask(__name__)
CORS(app)
app.config['JSON_SORT_KEYS'] = False
@app.route("/")
def index():
result = {
"name": "Tanyadoa API",
"author": {
"name" : "Miftah Afina",
"email": "surat@miftahafina.com",
"blog" : "miftahafina.com"
},
"endpoints": {
"/all": "Get all doa",
"/search/<keywords>": "Search doa by keywords",
"/show/<id_doa>": "Search doa by id_doa",
}
}
return jsonify(result)
@app.route("/all")
def all():
result = {
"code" : 200,
"message": "Success",
"data" : generated_doa_list
}
return jsonify(result)
@app.route("/show/<id_doa>")
def show(id_doa):
id_doa = escape(id_doa)
result = {
"code" : None,
"message": None,
"data": []
}
for doa in generated_doa_list:
if doa["id_doa"] == id_doa:
result["data"].append(doa)
if result["data"]:
result["code"] = 200
result["message"] = "Success"
else:
result["code"] = 404
result["message"] = "Not Found"
return jsonify(result)
@app.route("/search/<search_query>")
def search(search_query):
search_query = escape(search_query)
stemmer = StemmerFactory().create_stemmer()
query_list = stemmer.stem(search_query).split(" ")
result_list = []
final_result = {
"code" : None,
"message": None,
"data": []
}
for query in query_list:
for doa in generated_doa_list:
if query != "doa" and query in doa["kata_kunci"]:
if not any(result["id_doa"] == doa["id_doa"] for result in result_list):
# does not exists in result_list
result_list.append({
"id_doa" : doa["id_doa"],
"kecocokan": 1,
# "nama" : doa["nama"],
# "kata_kunci" : " ".join(doa["kata_kunci"]),
"doa_data" : doa
})
else:
# exists in result_list
for result in result_list:
if result["id_doa"] == doa["id_doa"]:
result["kecocokan"] += 1
if result_list:
result_list.sort(key=operator.itemgetter("kecocokan"), reverse=True)
# return jsonify(result_list) # comment this line on production server
highest_kecocokan = result_list[0]["kecocokan"]
for result in result_list:
if highest_kecocokan == result["kecocokan"]:
result["doa_data"]["kecocokan"] = result["kecocokan"]
final_result["data"].append(result["doa_data"])
final_result["code"] = 200
final_result["message"] = "Success"
else:
final_result["code"] = 404
final_result["message"] = "Not Found"
return jsonify(final_result)