Skip to content
Snippets Groups Projects
Commit 972cd4b6 authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Merge branch 'element-trs-conf' into 'master'

Add element confidence to CreateElementTranscriptions

Closes #1048

See merge request teklia/arkindex/backend!1811
parents ef01a7b7 4f16153b
No related branches found
No related tags found
1 merge request!1811Add element confidence to CreateElementTranscriptions
......@@ -253,6 +253,7 @@ class ElementTranscriptionsBulk(CreateAPIView):
mirrored=self.element.mirrored,
worker_version=worker_version,
worker_run=worker_run,
confidence=annotation.get('element_confidence'),
)
# Specify the annotated element has been created
annotation['created'] = True
......
......@@ -381,6 +381,11 @@ class SimpleTranscriptionSerializer(serializers.Serializer):
required=False,
)
orientation = EnumField(TextOrientation, default=TextOrientation.HorizontalLeftToRight, required=False)
element_confidence = serializers.FloatField(
min_value=0,
max_value=1,
required=False,
)
def validate(self, data):
if 'confidence' not in data:
......
......@@ -148,6 +148,66 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
]
)
def test_bulk_transcriptions_element_confidence(self):
"""
Bulk creates a list of elements, with a confidence score and an attached transcription
"""
# Create a manual transcription on the element
self.line.transcriptions.create(text='A manual transcription')
self.assertEqual(self.line.transcriptions.count(), 1)
existing_element_ids = list(Element.objects.get_descending(self.page.id).values_list('id', flat=True))
transcriptions = [
([[13, 37], [133, 37], [133, 137], [13, 137], [13, 37]], 'Hello world !', 0.1337, 'vertical-lr', 0.5),
([[24, 42], [64, 42], [64, 142], [24, 142], [24, 42]], 'I <3 JavaScript', 0.42, 'horizontal-lr', 0.7),
]
data = {
'element_type': 'text_line',
'worker_run_id': str(self.worker_run.id),
'transcriptions': [{
'polygon': poly,
'text': text,
'confidence': confidence,
'orientation': orientation,
'element_confidence': element_confidence
} for poly, text, confidence, orientation, element_confidence in transcriptions]
}
self.client.force_login(self.user)
response = self.client.post(
reverse('api:element-transcriptions-bulk', kwargs={'pk': self.page.id}),
format='json',
data=data
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
created_elts = Element.objects.get_descending(self.page.id).exclude(id__in=existing_element_ids)
self.assertEqual(created_elts.count(), 2)
self.assertTrue(all(map(lambda elt: elt.image == self.page.image, created_elts)))
self.assertListEqual(
[
(elt.paths.first().ordering, elt.name, elt.polygon.coords, elt.worker_version_id, elt.worker_run_id, elt.confidence)
for elt in created_elts
],
[
(1, '2', ((13, 37), (13, 137), (133, 137), (133, 37), (13, 37)), self.worker_version.id, self.worker_run.id, 0.5),
(2, '3', ((24, 42), (24, 142), (64, 142), (64, 42), (24, 42)), self.worker_version.id, self.worker_run.id, 0.7)
]
)
self.assertCountEqual(
created_elts.values_list(
'transcriptions__text',
'transcriptions__worker_version',
'transcriptions__worker_run',
'transcriptions__orientation',
'transcriptions__confidence'
),
[
('Hello world !', self.worker_version.id, self.worker_run.id, TextOrientation.VerticalLeftToRight, 0.1337),
('I <3 JavaScript', self.worker_version.id, self.worker_run.id, TextOrientation.HorizontalLeftToRight, 0.42)
]
)
@override_settings(ARKINDEX_FEATURES={'search': False})
def test_bulk_transcriptions_load(self):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment