aws-samples / aws-samples/amazon-textract-response-parser
KeyError exception in Python trp package when parsing a page that doesn't have a Polygon element
- Dominant language
- TypeScript
- Stars
- 236
- Forks
- 97
- PR merge metrics
- No merged PRs in 30d
Description
A 12 page PDF document was processed by Textract, and I'm trying to use this package to parse the resulting response.json. The very first is a PAGE block that has the following `Geometry` element:
```json
{
"DocumentMetadata": { "Pages": 12 },
"JobStatus": "SUCCEEDED",
"NextToken": "RYAd635ujGFqn4t5XLy4H+7BT1mguxFfHvBA8pGfJ3C9FnC8Pv7Cz/+qj+v/MisnIcNR7fwh+/CfJVGIdHn/sSplCQcE2ra4ZXjtDJ9SIp6Z9v5ICHmkzGNrVtS4m4GG",
"Blocks": [
{
"BlockType": "PAGE",
"Geometry": {
"BoundingBox": {
"Width": 1.0,
"Height": 1.0,
"Left": 0.0,
"Top": 0.0
}
},
"Id": "e5413485-55aa-405c-b547-25d6f3db1251",
"...","...."
}]}
```
I've loaded the response into a dictionary and then tried to instantiate the `Document` class, passing the document dictionary to the constructor; when I do so, I get the following error:
```
./tests/TextractOutputProcessor_test.py::test_processResponseJson Failed: [undefined]KeyError: 'Polygon'
responseJsonFile = './tests/textract/response.json'
def test_processResponseJson(responseJsonFile):
"""Test the processResponseJson method"""
assert isinstance(responseJsonFile, str)
processor = TextractOutputProcessor()
try:
> processor.loadResponseJson(responseJsonFile)
tests/TextractOutputProcessor_test.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
TextractOutputProcessor.py:24: in loadResponseJson
self.document = Document(self.metadata)
venv/lib/python3.8/site-packages/trp/__init__.py:638: in __init__
self._parse()
venv/lib/python3.8/site-packages/trp/__init__.py:675: in _parse
page = Page(documentPage["Blocks"], self._blockMap)
venv/lib/python3.8/site-packages/trp/__init__.py:522: in __init__
self._parse(blockMap)
venv/lib/python3.8/site-packages/trp/__init__.py:533: in _parse
self._geometry = Geometry(item['Geometry'])
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self =
geometry = {'BoundingBox': {'Height': 1.0, 'Left': 0.0, 'Top': 0.0, 'Width': 1.0}}
def __init__(self, geometry):
boundingBox = geometry["BoundingBox"]
> polygon = geometry["Polygon"]
E KeyError: 'Polygon'
venv/lib/python3.8/site-packages/trp/__init__.py:111: KeyError
```
It seems that the `Geometry` class expects there to be a `Polygon` element within every `Geometry` element in the response JSON, even though Textract did not create such an element when it processed my PDF document.
Contributor guide
Assessment
This issue has not been assessed yet.