diff --git a/arkindex_worker/worker/transcription.py b/arkindex_worker/worker/transcription.py index 98e3f19ad2cf436a128aab9a1b0c8cc1a39159ab..8ff8342e90bd1fd0ce2ff6553080f9d83d0b620e 100644 --- a/arkindex_worker/worker/transcription.py +++ b/arkindex_worker/worker/transcription.py @@ -256,9 +256,9 @@ class TranscriptionMixin(object): "element_id": annotation["element_id"], "text": transcription["text"], "confidence": transcription["score"], - "orientation": transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value, + "orientation": transcription.get( + "orientation", TextOrientation.HorizontalLeftToRight + ).value, "worker_version_id": self.worker_version_id, } ) diff --git a/tests/test_elements_worker/test_transcriptions.py b/tests/test_elements_worker/test_transcriptions.py index 2094568ddea3695118b1ddb5d46ce5fbdfbc89db..4a54f53291c1bd44e21d0fb40afecd1c9612603b 100644 --- a/tests/test_elements_worker/test_transcriptions.py +++ b/tests/test_elements_worker/test_transcriptions.py @@ -690,17 +690,22 @@ def test_create_transcriptions(responses, mock_elements_worker_with_cache): ("POST", "http://testserver/api/v1/transcription/bulk/"), ] - trans_response = list(map(dict, trans)) - for transcription in trans_response: - transcription["orientation"] = ( - transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value - ) - assert json.loads(responses.calls[-1].request.body) == { "worker_version": "12341234-1234-1234-1234-123412341234", - "transcriptions": trans_response, + "transcriptions": [ + { + "element_id": "11111111-1111-1111-1111-111111111111", + "text": "The", + "score": 0.75, + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "element_id": "11111111-1111-1111-1111-111111111111", + "text": "word", + "score": 0.42, + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + ], } # Check that created transcriptions were properly stored in SQLite cache @@ -770,17 +775,22 @@ def test_create_transcriptions_orientation(responses, mock_elements_worker_with_ transcriptions=trans, ) - trans_response = list(map(dict, trans)) - for transcription in trans_response: - transcription["orientation"] = ( - transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value - ) - assert json.loads(responses.calls[-1].request.body) == { "worker_version": "12341234-1234-1234-1234-123412341234", - "transcriptions": trans_response, + "transcriptions": [ + { + "element_id": "11111111-1111-1111-1111-111111111111", + "text": "Animula vagula blandula", + "score": 0.12, + "orientation": TextOrientation.HorizontalRightToLeft.value, + }, + { + "element_id": "11111111-1111-1111-1111-111111111111", + "text": "Hospes comesque corporis", + "score": 0.21, + "orientation": TextOrientation.VerticalLeftToRight.value, + }, + ], } # Check that oriented transcriptions were properly stored in SQLite cache @@ -1289,18 +1299,29 @@ def test_create_element_transcriptions(responses, mock_elements_worker): ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"), ] - transcriptions_sample_response = list(map(dict, TRANSCRIPTIONS_SAMPLE)) - for transcription in transcriptions_sample_response: - transcription["orientation"] = ( - transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value - ) - assert json.loads(responses.calls[-1].request.body) == { "element_type": "page", "worker_version": "12341234-1234-1234-1234-123412341234", - "transcriptions": transcriptions_sample_response, + "transcriptions": [ + { + "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]], + "score": 0.5, + "text": "The", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]], + "score": 0.75, + "text": "first", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "polygon": [[1000, 300], [1200, 300], [1200, 500], [1000, 500]], + "score": 0.9, + "text": "line", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + ], "return_elements": True, } assert annotations == [ @@ -1363,18 +1384,29 @@ def test_create_element_transcriptions_with_cache( ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"), ] - transcriptions_sample_response = list(map(dict, TRANSCRIPTIONS_SAMPLE)) - for transcription in transcriptions_sample_response: - transcription["orientation"] = ( - transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value - ) - assert json.loads(responses.calls[-1].request.body) == { "element_type": "page", "worker_version": "12341234-1234-1234-1234-123412341234", - "transcriptions": transcriptions_sample_response, + "transcriptions": [ + { + "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]], + "score": 0.5, + "text": "The", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]], + "score": 0.75, + "text": "first", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "polygon": [[1000, 300], [1200, 300], [1200, 500], [1000, 500]], + "score": 0.9, + "text": "line", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + ], "return_elements": True, } assert annotations == [ @@ -1494,18 +1526,29 @@ def test_create_transcriptions_orientation_with_cache( transcriptions=oriented_transcriptions, ) - oriented_transcriptions_response = list(map(dict, oriented_transcriptions)) - for transcription in oriented_transcriptions_response: - transcription["orientation"] = ( - transcription["orientation"].value - if "orientation" in transcription - else TextOrientation.HorizontalLeftToRight.value - ) - assert json.loads(responses.calls[-1].request.body) == { "element_type": "page", "worker_version": "12341234-1234-1234-1234-123412341234", - "transcriptions": oriented_transcriptions_response, + "transcriptions": [ + { + "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]], + "score": 0.5, + "text": "Animula vagula blandula", + "orientation": TextOrientation.HorizontalLeftToRight.value, + }, + { + "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]], + "score": 0.75, + "text": "Hospes comesque corporis", + "orientation": TextOrientation.VerticalLeftToRight.value, + }, + { + "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]], + "score": 0.9, + "text": "Quae nunc abibis in loca", + "orientation": TextOrientation.HorizontalRightToLeft.value, + }, + ], "return_elements": True, } assert annotations == [