Spaces:
Sleeping
Sleeping
File size: 15,070 Bytes
0106d5f |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
"""
Rules for policy analyser
@author : Sakshi Tantak
"""
# Imports
import json
from datetime import datetime
from policy_analyser.llm import call_openai
def prepare_payload(extraction):
payload = {
'Sum Insured (SI)' : 0,
'Pre-existing diseases (PED) Waiting period' : 0,
'30-Day Waiting Period' : False,
'Specific Illness Waiting Period' : 0,
'Maternity waiting period' : 0,
'Exclusions' : [],
'Maternity benefits' : False,
'OPD' : 0,
'Copay' : 0,
'Deductible' : 0,
'Daycare treatment' : [],
'Free Health checkup' : False,
'Restoration benefit' : False,
'Sublimits' : [],
'Room rent limit (proportionate deduction)' : 100,
'Pre & Post Hospitalization' : False,
'Domiciliary Cover' : False,
'No claim bonus' : 0,
'Ambulance cover' : 0,
'International coverage' : False,
'Dental treatment' : 0,
'AYUSH treatment' : False,
'Health incentives' : False,
'Wellness Services' : False,
'Consumables/ Non medical expenses' : False,
'Hospital Cash' : False,
'Adults' : 0,
'Children' : 0,
'City' : '',
'Is Top City' : True,
'Policy Age' : 0
}
num_adults, num_children, is_top_city = 0, 0, True
today = datetime.today()
for entity in extraction:
if entity['entityName'] in ['Exclusions', 'Daycare treatment', 'Sublimits']:
try:
value = json.loads(entity['entityValue'])
payload[entity['entityName']] = value
except:
pass
if entity['entityName'] == "Policy Holder's Details":
value = entity['entityValue']
city = ''
try:
value = json.loads(value)
if 'city' in value:
city = value['city']
try:
response = call_openai('Does a given city string belong to set of given cities : [Mumbai, Delhi, Bangalore, Chennai, Hyderabad, Gurgaon, Pune]. Answer in true/false only', city)
is_top_city = True if response == 'true' else False
except:
pass
except:
pass
payload['Is Top City'] = is_top_city
payload['City'] = city
if entity['entityName'] == 'Insured Persons details':
value = entity['entityValue']
try:
value = json.loads(value)
for person in value:
if 'date_of_birth' in person:
dob = person['date_of_birth']
dob = datetime.strptime(dob, '%d/%m/%Y')
age = (today - dob).days / 365
elif 'age' in person:
age = person['age']
if age >= 18:
num_adults += 1
else:
num_children += 1
except:
num_adults = 1
payload['Adults'] = num_adults
payload['Children'] = num_children
if entity['entityName'] == 'Policy Details':
try:
value = json.loads(entity['entityValue'])
if 'policy_start_date' in value:
payload['Policy Age'] = ((today - datetime.strptime(value['policy_start_date'], '%d/%m/%Y')).days / 365) * 12
except:
pass
if entity['entityName'] in ['Sum Insured (SI)', 'Pre-existing diseases (PED) Waiting period', 'Specific Illness Waiting Period',
'Maternity waiting period', 'OPD', 'Copay', 'Deductible', 'No claim bonus', 'Ambulance cover',
'Dental treatment', 'Room rent limit (proportionate deduction)']:
value = entity['entityValue']
if isinstance(value, (float, int)):
payload[entity['entityName']] = value
else:
try:
value = float(value)
payload[entity['entityName']] = value
except:
pass
if entity['entityName'] in ['30-Day Waiting Period', 'Maternity benefits', 'Free Health checkup',
'Restoration benefit', 'Pre & Post Hospitalization', 'Domiciliary Cover',
'International coverage', 'AYUSH treatment', 'Health incentives', 'Wellness Services',
'Consumables/ Non medical expenses', 'Hospital Cash']:
value = entity['entityValue']
if isinstance(value, bool):
payload[entity['entityName']] = value
else:
payload[entity['entityName']] = True if 'true' in value else False
return payload
def rules(payload):
analysis = []
if payload['Adults'] == 1:
if payload['Is Top City']:
if payload['Sum Insured (SI)'] >= 2500000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 25L for an adult in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 1000000 and payload['Sum Insured (SI)'] < 2500000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 25L but > 10L for an adult in {payload["City"]}'
if payload['Sum Insured (SI)'] < 1000000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 10L for an adult in {payload["City"]}'
else:
if payload['Sum Insured (SI)'] >= 1000000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 10L for an adult in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 500000 and payload['Sum Insured (SI)'] < 1000000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 5L but < 10L for an adult in {payload["City"]}'
if payload['Sum Insured (SI)'] < 500000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 5L for an adult in {payload["City"]}'
if payload['Adults'] >= 2:
if payload['Children'] == 0:
if payload['Is Top City']:
if payload['Sum Insured (SI)'] >= 5000000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 50L for {payload["Adults"]} adults in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 2500000 and payload['Sum Insured (SI)'] < 5000000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 25L but < 50L for {payload["Adults"]} adults in {payload["City"]}'
if payload['Sum Insured (SI)'] < 2500000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 25L for {payload["Adults"]} adults in {payload["City"]}'
else:
if payload['Sum Insured (SI)'] >= 2500000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 25L for {payload["Adults"]} adults in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 1000000 and payload['Sum Insured (SI)'] < 2500000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 10L but < 25L for {payload["Adults"]} adults in {payload["City"]}'
if payload['Sum Insured (SI)'] < 1000000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 10L for {payload["Adults"]} adults in {payload["City"]}'
if payload['Children'] >= 1:
if payload['Children'] > 1 or payload['Is Top City']:
if payload['Sum Insured (SI)'] >= 10000000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 1 CR for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 5000000 and payload['Sum Insured (SI)'] < 10000000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 50L but < 1 CR for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
if payload['Sum Insured (SI)'] < 5000000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 50L for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
else:
if payload['Sum Insured (SI)'] >= 5000000:
verdict, reason = 'Good', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 50L for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
if payload['Sum Insured (SI)'] >= 2500000 and payload['Sum Insured (SI)'] < 5000000:
verdict, reason = 'Average', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) > 25L but < 50L for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
if payload['Sum Insured (SI)'] < 2500000:
verdict, reason = 'Bad', f'Sum Insured (SI) ({payload["Sum Insured (SI)"]}) < 25L for {payload["Adults"]} adults & {payload["Children"]} children in {payload["City"]}'
analysis.append(
{
'factor' : 'Sum Insured (SI)',
'verdict' : verdict,
'reason' : reason
}
)
if payload['Room rent limit (proportionate deduction)'] > 0:
verdict, reason = 'Bad', f'There is cap of {payload["Room rent limit (proportionate deduction)"]} on room rent'
else:
verdict, reason = 'Good', 'There is no cap on room rent'
analysis.append({'factor' : 'Room rent limit (proportionate deduction)', 'verdict' : verdict, 'reason' : reason})
if payload['Deductible'] > 0:
verdict, reason = 'Bad', f'There is a deductible of {payload["Deductible"]}'
else:
verdict, reason = 'Good', 'No deductible'
analysis.append({'factor' : 'Deductible', 'verdict' : verdict, 'reason' : reason})
if payload['Sublimits'] == []:
verdict, reason = 'Good', 'There are no sublimits on any treatments or diseases'
else:
verdict = 'Bad'
sublimits_str = '\n'.join([f'{sublimit["sublimit_name"]}: {sublimit["sublimit_value"]}' for sublimit in payload['Sublimits']])
reason = f'Following sublimits were found in your policy:\n{sublimits_str}'
analysis.append({'factor' :'Sublimits', 'verdict' : verdict, 'reason' : reason})
if payload['Copay'] == 0 and payload['Copay'] <= 5:
verdict, reason = 'Good', f'Copayment ({payload["Copay"]}) < 5%'
elif payload['Copay'] > 5 and payload['Copay'] <= 10:
verdict, reason = 'Average', f'Copayment ({payload["Copay"]}) > 5% but < 10%'
elif payload['Copay'] > 10:
verdict, reason = 'Bad', f'Copayment (({payload["Copay"]})) > 10%'
analysis.append({'factor' : 'Copay', 'verdict' : verdict, 'reason' : reason})
if payload['Pre-existing diseases (PED) Waiting period'] > 0:
if payload['Policy Age'] > payload['Pre-existing diseases (PED) Waiting period']:
verdict, reason = 'Good', f'Your policy has a waiting period of {payload["Pre-existing diseases (PED) Waiting period"]} months on pre-existing diseases but the waiting period has expired as of today'
else:
verdict, reason = 'Bad', f'Your policy has a waiting period of {payload["Pre-existing diseases (PED) Waiting period"]} months on pre-existing diseases which is yet to expire'
else:
verdict, reason = 'Good', f'Your policy has no waiting period on pre-existing diseases'
analysis.append({'factor' : 'Pre-existing diseases (PED) Waiting period', 'verdict' : verdict, 'reason' : reason})
if payload['30-Day Waiting Period']:
if payload['Policy Age'] > 1:
verdict, reason = 'Good', f'Your policy has a 30 day waiting period but it has expired as of today'
else:
verdict, reason = 'Bad', f'Your policy has a 30 day waiting period which is yet to expire'
else:
verdict, reason = 'Good', f'Your policy has no 30 day waiting period'
analysis.append({'factor' : '30-Day Waiting Period', 'verdict' : verdict, 'reason' : reason})
if payload['Specific Illness Waiting Period'] > 0:
if payload['Policy Age'] > payload['Specific Illness Waiting Period']:
verdict, reason = 'Good', f'Your policy has a waiting period of {payload["Specific Illness Waiting Period"]} on specific illnesses but the waiting period has expired as of today'
else:
verdict, reason = 'Bad', f'Your policy has a waiting period of {payload["Specific Illness Waiting Period"]} on specific illnesses which is yet to expire'
else:
verdict, reason = 'Good', f'Your policy has no waiting period any on specific illnesses'
analysis.append({'factor' : 'Specific Illness Waiting Period', 'verdict' : verdict, 'reason' : reason})
if payload['Maternity benefits']:
analysis.append(
{
'factor' : 'Maternity benefits',
'verdict' : 'Good',
'reason' : 'Maternity benefits present, check waiting period'
}
)
if payload['Maternity waiting period'] > 0:
if payload['Policy Age'] > payload['Maternity waiting period']:
verdict, reason = 'Good', f'Your policy has a waiting period of {payload["Maternity waiting period"]} for maternity cases but it has expired as of today'
else:
verdict, reason = 'Bad', f'Your policy has a waiting period of {payload["Maternity waiting period"]} for maternity cases which is yet to expire'
else:
verdict, reason = 'Good', f'Your policy has a no waiting period for maternity cases'
analysis.append({'factor' : 'Maternity waiting period', 'verdict' : verdict, 'reason' : reason})
else:
analysis.append(
{
'factor' : 'Maternity benefits',
'verdict' : 'Bad',
'reason' : 'No maternity benefits'
}
)
return analysis
if __name__ == '__main__':
import json
import glob
dirpath = '/Users/sakshi.tantak/Downloads/Porting Documents/testing-data/sample/poc'
for file in glob.glob(f'{dirpath}/*.analysis.json'):
json_data = json.load(open(file))
payload = prepare_payload(json_data[1]['response']['processed'])
json_data.append({
'stage' : 'POST_PROCESS',
'response' : payload,
'time' : 0
})
# print(json_data)
with open(file, 'w') as f:
json.dump(json_data, f, indent = 4) |