Files
doczyai-pipelines/fieldExtraction/tests/test_chunking.py
T
Karan Desai 1098be5cf5 Merged in feature_ac_chunking_clean (pull request #265)
grouped keywords and IRS regex chunking

* abandon old branch and rebuild

* removed print for field names, prompts

* pushing as part of  leftovers

* IRS regex chunking

* Merged in feature/chunk_term_clean (pull request #262)

chunk_log_added

* chunk_log_added

* Merge remote-tracking branch 'remotes/origin/feature_ac_chunking_clean' into feature/chunk_term_clean


Approved-by: Alex Galarce

* added function for regex based IRS chunking

* shifted regex_match_chunk function to utils

* added helper functions for regex based chunking

* replaced tin_regex function to ac_funcs

* removed prompt for irs_others

* added regex chunking for irs

* removed irs group from smart chunking

* testing functionality for regex based irs fields

* updated return N/A condition

* Merged main into feature_ac_chunking_clean

* file_processing.py edited online with Bitbucket
* added back conditional prompts

* minor fixed for PR

* remove install types

* mering clean branch

* with passed test casses

* added flag for regex based tin execution


Approved-by: Michael McGuinness
2024-11-13 17:08:19 +00:00

167 lines
3.8 KiB
Python

import pytest
from preprocessing_funcs import smart_chunk_ac
test_chunk_dict_term = {
"1": "NA",
"2": "Termination",
"3": "NA",
"4": "NA",
}
test_late_paid_claims = {
"1": "NA",
"2": "LATE PAYMENT", # try lowercase
"3": "NA",
"4": "NA",
"10": "NA",
"11": "LATE PAID",
"12": "NA",
}
test_keyword_mappings = {
"or_1": {
"methodology": "or",
"keywords": ["kw1", "kw2", "kw3"],
"case_sensitive": False,
},
"or_2": {"methodology": "or", "keywords": ["kw4"], "case_sensitive": False},
"hierarchy_1": {
"methodology": "hierarchy",
"keywords": ["h5", "h6"],
"case_sensitive": False,
},
"cs_or_1": {
"methodology": "or",
"keywords": ["KW1", "kw2", "kw3"],
"case_sensitive": True,
},
"and": {"methodology": "and", "keywords": ["KW1", "KW2"], "case_sensitive": False},
"regex": {
"methodology": "regex",
"regex": r"\b\d{2}-?\d{7}\b",
"case_sensitive": True,
"keywords": ["KW1", "KW2"],
},
}
case_1 = {
"1": "NA",
"2": "kw1",
"3": "NA",
"4": "h6",
"5": "NA",
"6": "kw3",
}
case_2 = {
"1": "NA",
"2": "h6",
"3": "NA",
"4": "kw2",
"10": "NA",
"11": "h5",
"12": "NA",
"15": "kw4",
}
case_3 = {
"1": "NA",
"2": "KW1",
"3": "NA",
"4": "h6",
"5": "NA",
"6": "kw3",
}
case_4 = {
"1": "NA",
"2": "KW1",
"3": "NA",
"4": "KW1KW2",
"5": "NA",
"6": "kw3",
}
case_5 = {
"1": "NA",
"2": "12-3456789",
"3": "987654321",
"4": "Next",
"5": "1234567-89",
"6": "kw3",
}
def test_smart_chunk_term():
assert smart_chunk_ac(text_dict=test_chunk_dict_term)["term_group"] == "\n".join(
["NA", "Termination", "NA"]
)
def test_smart_chunk_late_paid():
assert smart_chunk_ac(text_dict=test_late_paid_claims)[
"LATE_PAID_CLAIMS_LANGUAGE"
] == "\n".join(["NA", "LATE PAID", "NA"])
def test_or_1():
assert smart_chunk_ac(text_dict=case_1, keyword_mappings=test_keyword_mappings)[
"or_1"
] == "\n".join(["NA", "kw1", "NA", "NA", "kw3"])
assert smart_chunk_ac(text_dict=case_2, keyword_mappings=test_keyword_mappings)[
"or_1"
] == "\n".join(["NA", "kw2"])
def test_or_2():
assert smart_chunk_ac(text_dict=case_1, keyword_mappings=test_keyword_mappings)[
"or_2"
] == "\n".join([])
assert smart_chunk_ac(text_dict=case_2, keyword_mappings=test_keyword_mappings)[
"or_2"
] == "\n".join(["kw4"])
def test_case_sensitive_or_1():
assert smart_chunk_ac(text_dict=case_1, keyword_mappings=test_keyword_mappings)[
"cs_or_1"
] == "\n".join(["NA", "kw3"])
assert smart_chunk_ac(text_dict=case_3, keyword_mappings=test_keyword_mappings)[
"cs_or_1"
] == "\n".join(["NA", "KW1", "NA", "NA", "kw3"])
def test_hierarchy_1():
assert smart_chunk_ac(text_dict=case_1, keyword_mappings=test_keyword_mappings)[
"hierarchy_1"
] == "\n".join(["NA", "h6", "NA"])
assert smart_chunk_ac(text_dict=case_2, keyword_mappings=test_keyword_mappings)[
"hierarchy_1"
] == "\n".join(["NA", "h5", "NA"])
def test_and():
assert smart_chunk_ac(text_dict=case_4, keyword_mappings=test_keyword_mappings)[
"and"
] == "\n".join(["NA", "KW1KW2", "NA"])
def test_regex():
assert smart_chunk_ac(text_dict=case_5, keyword_mappings=test_keyword_mappings)[
"regex"
] == "\n".join(["NA", "12-3456789", "987654321", "Next"])
def test_undefined_methodology():
with pytest.raises(ValueError):
smart_chunk_ac(
text_dict=case_1,
keyword_mappings={
"e1": {
"methodology": "Not a real methodology",
"keywords": ["NA"],
"case_sensitive": True,
}
},
)