-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnosis_search.py
executable file
·50 lines (44 loc) · 1.88 KB
/
diagnosis_search.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
#!hd/bin/python
from flask import Flask, jsonify, abort, request, make_response, url_for
from Trie import Trie
app = Flask(__name__)
prefix_tree = Trie()
# Dummy database array
selected_diagnoses = []
# Autocomplete words, given prefix as query
@app.route('/api/search/<prefix>', methods = ['GET'])
def get_autocomplete_words(prefix):
return jsonify({ 'words': prefix_tree.autocomplete(prefix) })
# Update/store query with selected diagnosis
@app.route('/api/search', methods = ['PUT'])
def select_diagnosis():
if not request.json:
abort(400)
diagnosis = {
'query': request.json['query'],
'selected': request.json['selected']
}
# Store new entry or find relevant query in db then update value
matched_queries = [matched for matched in selected_diagnoses if matched['query'] == request.json['query']]
if not matched_queries:
selected_diagnoses.append(diagnosis)
else:
old_query = matched_queries[0]
old_query['query'] = request.json['query']
old_query['selected'] = request.json['selected']
return jsonify({ 'selected_diagnosis': diagnosis }), 200
# Get database values of queries matched to selected diagnoses
@app.route('/api/search', methods = ['GET'])
def get_selected_diagnoses():
return jsonify({ 'selected_diagnoses': selected_diagnoses })
if __name__ == '__main__':
# Insert all sample diagnoses into prefix tree
with open('short-diagnoses.txt', 'r') as word_sample_file:
for line in word_sample_file:
full_word = line.strip()
# Also insert alternate names for diagnoses
if full_word.find("(") != -1 and full_word.find(")") != -1:
alternate_name = full_word[full_word.find("(")+1:full_word.find(")")]
prefix_tree.addWord(alternate_name, line.strip())
prefix_tree.addWord(line.strip())
app.run(debug = True)