Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expense): Expenses with no summary fields #379

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tests/fixtures/receipt_no_summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

35 changes: 34 additions & 1 deletion tests/test_analyze_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_analyze_expense_from_path(self):
self.assertEqual(len(document.expense_documents[0].summary_groups.VENDOR), 2)
self.assertEqual(len(document.expense_documents[0].line_items_groups[0].to_pandas()), 4,
"There are 4 line item in the receipts")

def test_analyze_expense_from_image(self):
# Testing local single image input
if os.environ.get("CALL_TEXTRACT"):
Expand All @@ -58,6 +58,39 @@ def test_analyze_expense_from_image(self):
self.assertEqual(len(document.expense_documents[0].line_items_groups[0].to_pandas()), 4,
"There are 4 line item in the receipts")


class TestTextractorAnalyzeExpenseNoSummary(unittest.TestCase):
def setUp(self):
# insert credentials and filepaths here to run test
self.profile_name = "default"
self.current_directory = os.path.abspath(os.path.dirname(__file__))
self.image_path = os.path.join(self.current_directory, "fixtures/receipt_no_summary.png")

if self.profile_name is None:
raise InvalidProfileNameError(
"Textractor could not be initialized. Populate profile_name with a valid input in tests/test_textractor.py."
)
if os.environ.get("CALL_TEXTRACT"):
self.extractor = Textractor(
profile_name=self.profile_name, kms_key_id=""
)

def test_analyze_expense_no_summary_fields(self):
"""Correctly load expense line items where no summary fields were recognized

Per: https://github.com/aws-samples/amazon-textract-textractor/issues/370
"""
if os.environ.get("CALL_TEXTRACT"):
document = self.extractor.analyze_expense(file_source=self.image_path)
with open(get_fixture_path(), "w") as f:
json.dump(document.response, f)
else:
document = Document.open(get_fixture_path())

self.assertIsInstance(document, Document)
self.assertEqual(len(document.expense_documents), 1)
self.assertGreater(len(document.expense_documents[0].line_items_groups), 0)

if __name__ == "__main__":
test = TestTextractorAnalyzeExpense()
test.setUp()
Expand Down
19 changes: 15 additions & 4 deletions textractor/parsers/response_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,10 +1477,21 @@ def parser_analyze_expense_response(response):
]
document = parse_document_api_response(response)
for doc in response["ExpenseDocuments"]:
# FIXME
if len(doc["SummaryFields"]) == 0:
page_number = None
if len(doc["SummaryFields"]):
page_number = doc["SummaryFields"][0].get("PageNumber")
elif len(doc["LineItemGroups"]):
# A group must have at least one LineItem, and a LI must have at least one field:
first_field = doc["LineItemGroups"][0]["LineItems"][0]["LineItemExpenseFields"][0]
page_number = first_field.get("PageNumber")
if page_number is None:
logging.warning(
"Skipping parsing ExpenseDocument %s as its page number could not be determined"
% (doc["ExpenseIndex"],)
)
continue
page = document.pages[doc["SummaryFields"][0]["PageNumber"] - 1]

page = document.pages[page_number - 1]
summary_fields = []
for summary_field in doc["SummaryFields"]:
summary_fields.append(create_expense_from_field(summary_field, page))
Expand Down Expand Up @@ -1525,7 +1536,7 @@ def parser_analyze_expense_response(response):
page=page.page_num,
)
expense_document.raw_object = doc
document.pages[summary_field["PageNumber"] - 1].expense_documents.append(
document.pages[page_number - 1].expense_documents.append(
expense_document
)
document.response = response
Expand Down
Loading