-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
135 lines (110 loc) · 4.44 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import (
Flask, redirect, render_template, request, url_for, jsonify
)
import test
import json
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route('/')
def main():
total_cases = 0
total_deaths = 0
county = {}
# url = "https://www.gov.ie/en/press-release/17f5cb-statement-from-the-national-public-health-emergency-team-tuesday-14-/"
url = "https://www.gov.ie/en/press-release/f4ad12-statement-from-the-national-public-health-emergency-team-thursday-23/"
res = requests.get(url)
soup = BeautifulSoup(res.content, "html.parser")
tables = soup.find_all('table')
for table in tables:
if 'Total number of cases' in table.text: # get total number of cases and death
tr = table.find_all('tr')
for row in tr:
if 'Total number of cases' in row.text:
td = row.find_all('td')
total_cases = td[1].text
if 'Total number of deaths' in row.text:
td = row.find_all('td')
total_deaths = td[1].text
elif 'Carlow' in table.text:
tr = table.find_all('tr')
for row in tr:
td = row.find_all('td')
if td[0].text not in county.keys():
county[td[0].text] = td[1].text
c = json.dumps(county)
return render_template('main.html', total_cases=total_cases, total_deaths=total_deaths, county=c)
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.stem import PorterStemmer
from gensim.models import Word2Vec
@app.route('/step1') # Get a string of symptom
def step1():
# Get arguments and initiate variables
symptom_string = request.args.get('symptom')
symptoms = []
symptom = test.init_text()
# Get Model
model = Word2Vec.load('word2vec2.model')
word_vectors = model.wv
# Word tokenize
lemmatizer = WordNetLemmatizer()
porter = PorterStemmer()
# Get symptoms from text
sentences = sent_tokenize(symptom_string)
for sentence in sentences:
if len(sentence) == 0:
break
words = word_tokenize(sentence)
tag_block = nltk.pos_tag(words)
for word, tag in tag_block:
print(word, tag)
if tag == 'JJ' or tag == 'JJR' or tag == 'RB':
continue
if tag == 'NN': # tag is entity
for s in symptom.keys():
if word == s:
symptoms.append(s)
elif tag == 'VBG' or tag == 'VBP' or tag == 'VBN' or tag == 'VBD':
keyword = porter.stem(word)
for s in symptom.keys(): # Check symptom could be happen in COVID-19
if keyword == s:
symptoms.append(s)
elif tag == 'NNS':
keyword = lemmatizer.lemmatize(word)
for s in symptom.keys(): # Check symptom could be happen in COVID-19
if keyword == s:
symptoms.append(s)
elif word in word_vectors.vocab.keys(): # find symptom word based on similarity
s_symptom = word_vectors.similarity(w1=word, w2='symptom')
s_difficulty = word_vectors.similarity(w1=word, w2='difficulty')
if s_symptom > 0.8 and s_difficulty > 0.9:
print(word, ' similarity:', s_symptom, ', ', s_difficulty)
symptoms.append(word)
return jsonify(symptoms=symptoms)
@app.route('/step3') # Get a string of symptom
def step3():
symptom_string = request.args.get('symptom')
symptoms = symptom_string.split(',')
print(symptom_string)
# The most common symptoms of COVID-19 from https://www.who.int/news-room/q-a-detail/q-a-coronaviruses
common_list = ['fever', 'tiredness', 'cough', 'ache', 'pain', 'breathing', 'diarrhea']
result = {}
count = 0
for s in symptoms:
if len(s) == 0:
break
rad_value = test.get_rad_value(s)
if rad_value > 0:
result[s] = rad_value
count += 1
else:
if s in common_list:
result[s] = 1 # There is no case with this symptom in RAD, but it is common symptom
else:
result[s] = 0 # There is no case with this symptom in RAD
c = json.dumps(result)
return jsonify(rad=[c])
if __name__ == '__main__':
app.run()