Files
doczyai-pipelines/fieldExtraction/src/investment/lesser_of_funcs.py
T
Alex Galarce 674add9a3e Merged in feature/black-and-isort (pull request #717)
Feature/black and isort

* black and isort constants

* black and isort codes

* black and isort crosswalks

* black and isort investment

* black and isort prompts

* isort testbed

* isort tracking

* black and isort utils

* poetry and black tests


Approved-by: Katon Minhas
2025-09-30 20:56:02 +00:00

103 lines
3.8 KiB
Python

import logging
import re
import src.investment.prompt_calls as prompt_calls
from src.utils import string_utils
def has_explicit_lesser_of_carveout(reimb_term: str) -> bool:
"""Check for explicit 'lesser of' carve-out language in the reimbursement term.
This function uses a list of keywords to identify if the reimbursement term contains explicit language indicating a 'lesser of' carve-out.
Args:
reimb_term (str): The reimbursement term to check.
Returns:
bool: True if 'lesser of' carve-out language is found, False otherwise.
"""
carveout_patterns = ["not subject to.*lesser", "not bound by.*lesser"]
reimb_lower = reimb_term.lower()
return any(re.search(pattern, reimb_lower) for pattern in carveout_patterns)
def create_exhibit_lesser_of_row(
original_row: dict, exhibit_lesser_of: str, filename: str
) -> dict:
"""Create enhanced row with exhibit lesser-of constraint applied, avoiding redundancy with substring checks.
Args:
original_row (dict): Original row dictionary containing reimbursement details.
exhibit_lesser_of (str): Exhibit lesser-of statement to apply.
filename (str): The name of the file being processed, for logging.
Returns:
dict: Enhanced row dictionary with exhibit lesser-of constraint applied.
"""
enhanced_row = original_row.copy()
# Get original REIMB_TERM
original_reimb = original_row.get("REIMB_TERM", "")
# Skip if exhibit_lesser_of is empty or N/A
if not exhibit_lesser_of or string_utils.is_empty(exhibit_lesser_of):
return enhanced_row
# FIRST-ORDER DEDUPLICATION: Check if the exhibit constraint is already present (case-insensitive)
if exhibit_lesser_of.lower() in original_reimb.lower():
logging.debug(
f"Exhibit constraint '{exhibit_lesser_of}' already present in REIMB_TERM: {original_reimb}"
)
return enhanced_row # No need to modify if already present
# SECOND-ORDER DEDUPLICATION: Use LLM to check if the exhibit constraint is redundant
if prompt_calls.is_exhibit_lesser_of_redundant(
original_reimb, exhibit_lesser_of, filename
):
logging.debug(
f"LLM determined that exhibit constraint '{exhibit_lesser_of}' is semantically redundant with REIMB_TERM: {original_reimb}"
)
return enhanced_row # No need to modify if redundant
# Apply the exhibit lesser-of constraint
enhanced_reimb = f"lesser of ({original_reimb}) or ({exhibit_lesser_of})"
enhanced_row["REIMB_TERM"] = enhanced_reimb
logging.debug(
f"Enhanced REIMB_TERM with exhibit lesser-of: {enhanced_row['REIMB_TERM']}"
)
return enhanced_row
def apply_exhibit_lesser_of(
rows: list[dict], exhibit_lesser_of: str, filename: str
) -> list[dict]:
"""Apply exhibit lesser-of constraint to rows that don't explicitly have a lesser-of carve-out.
Args:
rows (list[dict]): The rows to which the constraint should be applied.
exhibit_lesser_of (str): The exhibit lesser-of statement to apply.
filename (str): The name of the file being processed, for logging.
Returns:
list[dict]: The rows with the exhibit lesser-of constraint applied.
"""
enhanced_rows = []
for row in rows:
reimb_term = row.get("REIMB_TERM", "")
# Check if row explicitly carves out from lesser-of
if has_explicit_lesser_of_carveout(reimb_term):
logging.debug(
f"Skipping exhibit lesser-of application for row with explicit carve-out: {row}"
)
enhanced_rows.append(row) # Keep as-is
else:
# Apply exhibit lesser-of to rows without explicit carve-out
row = create_exhibit_lesser_of_row(row, exhibit_lesser_of, filename)
enhanced_rows.append(row)
return enhanced_rows