diff --git a/fieldExtraction/src/ac_funcs.py b/fieldExtraction/src/ac_funcs.py index e7b4b53..fd37901 100644 --- a/fieldExtraction/src/ac_funcs.py +++ b/fieldExtraction/src/ac_funcs.py @@ -33,166 +33,6 @@ def get_ac_answer(prompt, filename, fields): response_dict = json_parsing_search(response_text, fields) return response_dict - -def select_context(text_dict, field): - if (field == "RELATIONSHIP_OF_PARTIES_IND") or ( - field == "RELATIONSHIP_OF_PARTIES_LANGUAGE" - ): - keywords = ["Relationship of Parties"] - if field == "REGULATORY_REQUIREMENTS": - keywords = ["Complience with Regulatory Requirements"] - if (field == "DISPARAGEMENT_PROHIBITION_IND") or ( - field == "DISPARAGEMENT_PROHIBITION_LANGUAGE" - ): - keywords = ["Disparagement Prohibition"] - if field == "ELIGIBILITY_VERIFICATION": - keywords = ["Eligibility Determination"] - if field == "INSURANCE_REQUIREMENT": - keywords = ["Insurance"] - if field == "PARTICIPATION_IN_PRODUCTS": - keywords = ["Participation in Products"] - if field == "POLICIES_AND_PROCEDURES": - keywords = ["Provider Manual", "Policies and Procedures"] - if field == "ARBITRATION_AND_DISPUTES": - keywords = ["Informal Dispute Resolution", "Arbitration"] - - page_list = [] - - for keyword in keywords: - for page in text_dict: - if keyword.lower() in text_dict[page].lower(): - if page > 1: - if page < len(text_dict): - page_list.extend([page - 1, page, page + 1]) - else: - page_list.extend([page - 1, page]) - else: - page_list.extend([page, page + 1]) - - page_list = list(set(page_list)) - context = [text_dict[page] for page in page_list] - context = "\n".join(context) - - return context, page_list - - -def json_parsing(response_text, context): - try: - if response_text.split("{", 1)[1].strip()[0] == '"': - response_text = "{" + response_text.split("{", 1)[1] - else: - response_text = "{" + response_text - except: - response_text = "{" + response_text - - if len(response_text.split("}", 1)) > 1: - if response_text.rsplit("}", 1)[0].strip()[-1] == '"': - response_text = response_text.rsplit("}", 1)[0] + "}" - elif response_text.rsplit("}", 1)[0].strip()[-1] == "}": - response_text = response_text.rsplit("}", 1)[0] - else: - response_text = response_text.rstrip(",") + "}" - - try: - response_dict = json.loads(response_text) - except: - response_dict = {"Test field": response_text} - - field_l = list(response_dict.keys()) - answer_l = list(response_dict.values()) - field_dict = {k: v for k, v in response_dict.items() if not k.endswith("_PG")} - page_dict = {k: v for k, v in response_dict.items() if k.endswith("_PG")} - page_dict = {k[:-3]: v for k, v in response_dict.items()} - field_l = list(field_dict.keys()) - answer_l = list(field_dict.values()) - page_no_l = [page_dict.get(x, "") for x in field_l] - - answer_for_snippet = [ - a.lower() if isinstance(a, str) else str(a) if isinstance(a, int) else "" - for a in answer_l - ] - - answer_for_snippet = [ - ( - a[:4] - if "_DT" in f - else ( - "renew" - if f - in ["CONTRACT_AUTO_RENEWAL_IND", "CONTRACT_AUTO_RENEWAL_TERM_LENGTH"] - else ( - "signature" - if f == "CONTRACT_SIGNATORY_IND" - else ( - "hospital" - if f == "CONTRACT_TYPE" - else ( - a[:10] - if f in ["PROV_NPI_OTHER", "PROV_TIN_OTHER"] - else ( - "participat" - if f == "PROV_PARTICIPATION_STATUS" - else ( - "master" - if f == "CONTRACT_CHARGEMASTER_PROTECTION_LANGUAGE" - else a - ) - ) - ) - ) - ) - ) - ) - for a, f in zip(answer_for_snippet, field_l) - ] - - try: - location_l = [ - ( - context.lower().find(a, context.find("Start of Page No. = " + str(p))) - if isinstance(a, str) and a != "" - else -1 - ) - for a, p in zip(answer_for_snippet, page_no_l) - ] - except: - location_l = [ - ( - context.lower().find(answer) - if isinstance(answer, str) and answer != "" - else -1 - ) - for answer in answer_for_snippet - ] - - words_before = [ - " ".join(context[:location].split()[-25:]) if location != -1 else " " - for location in location_l - ] - words_after = [ - " ".join(context[location:].split()[:30]) if location != -1 else " " - for location in location_l - ] - snippet_l = [ - ( - " ".join(context[: location - len(wb) - 1].split(".")[-1:]) - + " " - + wb - + " " - + wa - + " " - + " ".join(context[location + len(wa) + 1 :].split(".")[:1]) - + "." - if location != -1 - else " " - ) - for location, wb, wa in zip(location_l, words_before, words_after) - ] - snippet_l = [snippet.strip() for snippet in snippet_l] - - return field_l, answer_l, page_no_l, location_l, snippet_l - - def extract_text_from_delimiters( raw_output: str, delimiter: Delimiter, match_index: int = -1 ) -> str: