|
| 1 | +from flask import Flask, render_template, url_for, request, redirect |
| 2 | +from flask_sqlalchemy import SQLAlchemy |
| 3 | +import re |
| 4 | +app = Flask(__name__) |
| 5 | + |
| 6 | +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' |
| 7 | +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 8 | +db = SQLAlchemy(app) |
| 9 | + |
| 10 | +class PatientData(db.Model): |
| 11 | + id = db.Column(db.Integer, primary_key=True) |
| 12 | + name = db.Column(db.String(100)) |
| 13 | + gender = db.Column(db.String(5)) |
| 14 | + story = db.Column(db.String(2000)) |
| 15 | + complaint = db.Column(db.String(300)) |
| 16 | + symptoms = db.Column(db.String(200)) |
| 17 | + duration = db.Column(db.String(200)) |
| 18 | + severity = db.Column(db.String(200)) |
| 19 | + asymptom = db.Column(db.String(200)) |
| 20 | + afactors = db.Column(db.String(300)) |
| 21 | + rfactors = db.Column(db.String(300)) |
| 22 | + |
| 23 | + |
| 24 | +@app.route("/") |
| 25 | +@app.route("/home") |
| 26 | +def home(): |
| 27 | + return render_template('home.html', active='home') |
| 28 | + |
| 29 | + |
| 30 | +@app.route("/evaluate") |
| 31 | +def evaluate(): |
| 32 | + return render_template('evaluate.html', title='Evaluation', active='evaluate') |
| 33 | + |
| 34 | +@app.route("/database") |
| 35 | +def database(): |
| 36 | + Patientlist = PatientData.query.all() |
| 37 | + return render_template('database.html', title='Database', active='database',Patientlist=Patientlist) |
| 38 | + |
| 39 | +@app.route("/statistics") |
| 40 | +def statistics(): |
| 41 | + return render_template('statistics.html', title='Statistics', active='statistics') |
| 42 | + |
| 43 | + |
| 44 | +@app.route("/howitworks") |
| 45 | +def howitworks(): |
| 46 | + return render_template('howitworks.html', title='How it works', active='howitworks') |
| 47 | + |
| 48 | + |
| 49 | +@app.route("/credits") |
| 50 | +def credits(): |
| 51 | + return render_template('creditspage.html', title='Credits', active='credits') |
| 52 | + |
| 53 | +@app.route("/add", methods=["POST"]) |
| 54 | +def add(): |
| 55 | + story = request.form.get("story") |
| 56 | + gender = request.form.get("gender") |
| 57 | + name = request.form.get("fullname") |
| 58 | + complaint = request.form.get("complaint") |
| 59 | + symptoms = request.form.get("symptoms") |
| 60 | + duration = request.form.get("duration") |
| 61 | + severity = request.form.get("severity") |
| 62 | + asymptom = request.form.get("asymptom") |
| 63 | + rfactor = request.form.get("rfactor") |
| 64 | + afactor = request.form.get("afactor") |
| 65 | + new_data = PatientData(story=story, name=name, gender=gender, complaint=complaint,symptoms=symptoms,duration=duration,severity=severity,asymptom=asymptom,rfactors=rfactor,afactors=afactor) |
| 66 | + db.session.add(new_data) |
| 67 | + db.session.commit() |
| 68 | + return redirect(url_for("database")) |
| 69 | + |
| 70 | +@app.route("/report", methods=["POST"]) |
| 71 | +def report(): |
| 72 | + story = request.form.get("story") |
| 73 | + gender = request.form.get("gender") |
| 74 | + name = request.form.get("fullname") |
| 75 | + # need to integrate InstaMD function from classification.ipynb here |
| 76 | + # This route is called from "evaluate.html" and renders "report.html" with results |
| 77 | + def InstantMD(story,gender,name): |
| 78 | + symptom_list = [] |
| 79 | + with open('symptom_list.txt', 'r') as filehandle: |
| 80 | + for line in filehandle: |
| 81 | + symptom = line[:-1] |
| 82 | + symptom_list.append(symptom) |
| 83 | + anatomy_list = [] |
| 84 | + with open('anatomy_list.txt', 'r') as filehandle: |
| 85 | + for line in filehandle: |
| 86 | + anatomy = line[:-1] |
| 87 | + anatomy_list.append(anatomy) |
| 88 | + |
| 89 | + Symptoms = {} |
| 90 | + Anatomy = {} |
| 91 | + |
| 92 | + story = story.replace(',', '.').replace('?', '.') |
| 93 | + sentences = story.split('.') |
| 94 | + |
| 95 | + word_list = [] |
| 96 | + for i in sentences: |
| 97 | + words = i.split(" ") |
| 98 | + word_list = word_list + words |
| 99 | + |
| 100 | + while("" in word_list) : |
| 101 | + word_list.remove("") |
| 102 | + |
| 103 | + |
| 104 | + for i in symptom_list: |
| 105 | + if((' ' in i) == True): |
| 106 | + if story.count(i)>0: |
| 107 | + Symptoms[i] = story.count(i) |
| 108 | + |
| 109 | + for i in symptom_list: |
| 110 | + if word_list.count(i)>0: |
| 111 | + Symptoms[i] = word_list.count(i) |
| 112 | + |
| 113 | + for i in anatomy_list: |
| 114 | + if word_list.count(i)>0: |
| 115 | + Anatomy [i] = word_list.count(i) |
| 116 | + |
| 117 | + number_dict = {"one":"1","two":"2","three":"3","four":"4","five":"5","six":"6","seven":"7","eight":"8","nine":"9"} |
| 118 | + for i in range(len(word_list)): |
| 119 | + if word_list[i] in list(number_dict.keys()): |
| 120 | + word_list[i] = number_dict[word_list[i]] |
| 121 | + |
| 122 | + chief_complaint_final = "" |
| 123 | + duration_final = "" |
| 124 | + aggravating_factor_final = "" |
| 125 | + relieving_factor_final = "" |
| 126 | + associated_symptom_final= "" |
| 127 | + severity_final = "" |
| 128 | + symptoms_final = "" |
| 129 | + |
| 130 | + aggrevating_factor_keywords = ["increase","increases","increasing","rise","rises","becomes more",] |
| 131 | + relieving_factor_keywords = ["decreases", "decrease" , "decreasing" , "lessens" , "less" , "subsides", "subside","better"] |
| 132 | + |
| 133 | + duration_list_postfix = ["month","year","week","day","months","years","weeks","days"] |
| 134 | + |
| 135 | + |
| 136 | + for i in sentences: |
| 137 | + symptoms = set(Symptoms.keys()) |
| 138 | + anatomy = set(Anatomy.keys()) |
| 139 | + words = set(i.split(" ")) |
| 140 | + if len(symptoms & words)!=0 and len(anatomy & words)!=0: |
| 141 | + chief_complaint_final = ' and '.join(list(symptoms & words)) + ", area: " + " and ".join(list(anatomy & words)) |
| 142 | + break |
| 143 | + |
| 144 | + if (chief_complaint_final == ""): |
| 145 | + chief_complaint_final = max(Symptoms, key=Symptoms.get) |
| 146 | + |
| 147 | + symptoms_final = list(Symptoms.keys()) |
| 148 | + |
| 149 | + for i in duration_list_postfix: |
| 150 | + if i in word_list: |
| 151 | + index = word_list.index(i) - 1 |
| 152 | + duration_final = word_list[index] + " " + word_list[index + 1] |
| 153 | + break |
| 154 | + |
| 155 | + def get_aggravating_factor(): |
| 156 | + for i in sentences: |
| 157 | + words = set(i.split(" ")) |
| 158 | + for keyword in aggrevating_factor_keywords: |
| 159 | + if keyword in words: |
| 160 | + result = re.search(keyword + '(.*)and', i) |
| 161 | + if(result == None): |
| 162 | + result = re.search(keyword + '(.*)', i) |
| 163 | + return keyword + result.group(1) |
| 164 | + |
| 165 | + |
| 166 | + def get_relieving_factor(): |
| 167 | + for i in sentences: |
| 168 | + words = set(i.split(" ")) |
| 169 | + for keyword in relieving_factor_keywords: |
| 170 | + if keyword in words: |
| 171 | + result = re.search(keyword + '(.*)', i) |
| 172 | + return keyword + result.group(1) |
| 173 | + |
| 174 | + |
| 175 | + def get_associated_symptom(): |
| 176 | + for i in sentences: |
| 177 | + words = set(i.split(" ")) |
| 178 | + keyword = "also" |
| 179 | + if keyword in words: |
| 180 | + result = re.search(keyword + '(.*)', i) |
| 181 | + return result.group(1) |
| 182 | + |
| 183 | + def get_severity(): |
| 184 | + if "mild" in word_list: |
| 185 | + return "mild" |
| 186 | + elif "moderate" in word_list: |
| 187 | + return "moderate" |
| 188 | + elif "severe" in word_list: |
| 189 | + return "severe" |
| 190 | + elif story.count("pain")>1: |
| 191 | + return "severe" |
| 192 | + elif story.count("pain")==1: |
| 193 | + return "severe" |
| 194 | + else: |
| 195 | + return "mild" |
| 196 | + |
| 197 | + aggravating_factor_final = get_aggravating_factor() |
| 198 | + relieving_factor_final = get_relieving_factor() |
| 199 | + associated_symptom_final= get_associated_symptom() |
| 200 | + severity_final = get_severity() |
| 201 | + |
| 202 | + report_dict = { |
| 203 | + "Symptoms" : symptoms_final, |
| 204 | + "Chief Complaint": chief_complaint_final, |
| 205 | + "Duration" : duration_final, |
| 206 | + "Aggravating Factor" : aggravating_factor_final, |
| 207 | + "Relieving Factor" : relieving_factor_final, |
| 208 | + "Associated Symptom" : associated_symptom_final, |
| 209 | + "Severity": severity_final, |
| 210 | + "Story" : story, |
| 211 | + "Gender" : gender, |
| 212 | + "Name" : name |
| 213 | + } |
| 214 | + return report_dict |
| 215 | + |
| 216 | + |
| 217 | + report_dict = InstantMD(story,gender,name) |
| 218 | + return render_template('report.html', title='Report', active='report',report_dict=report_dict) |
| 219 | + |
| 220 | + |
| 221 | +if __name__ == "__main__": |
| 222 | + db.create_all() |
| 223 | + app.run(debug=True) |
0 commit comments