Spaces:
Running
Running
""" | |
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) |