Skip to content
Snippets Groups Projects
Commit 38a1cab7 authored by Bastien Abadie's avatar Bastien Abadie Committed by Erwan Rouchet
Browse files

Bulk create Transcription

parent e42911d4
No related branches found
No related tags found
1 merge request!22Add score to transcriptions
......@@ -104,35 +104,56 @@ def bulk_transcriptions(image, page, items):
# Calc needed TrBox to build
needed = required.difference(existing)
if not needed:
return []
zones = []
transcriptions = []
with transaction.atomic():
# Create transcriptions and linked zones
for n in needed:
tr = Transcription.objects.create(
line=n.line,
text=n.text,
score=n.score,
# Raw elements
elements = Element.objects.bulk_create(
Element(type=ElementType.Transcription)
for _ in needed
)
# Build transcriptions & zones instances at the same time
transcriptions, zones = zip(*[
(
Transcription(
element_ptr_id=elt.id,
line=n.line,
text=n.text,
score=n.score,
),
Zone(
element_id=elt.id,
image=image,
polygon=n.box.to_polygon(),
)
)
transcriptions.append(tr)
for elt, n in zip(elements, needed)
])
# Create transcriptions using a low-level bulk_create
# as multi table is not supported yet by Django
Transcription.objects.none()._batched_insert(
transcriptions,
zones.append(Zone(
element_id=tr.id,
image=image,
polygon=n.box.to_polygon(),
))
# Here is the magic: we need only to insert the fields from documents_transcription
fields=set(Transcription._meta.concrete_fields).difference(Element._meta.concrete_fields),
# Default
batch_size=None,
)
# Build zones in bulk
# Create zones in bulk
Zone.objects.bulk_create(zones)
# Create all links between transcription and page
max_order_dl = ElementLink.objects.filter(parent=page).order_by('-order').first()
max_order = 0 if max_order_dl is None else max_order_dl.order + 1
ElementLink.objects.bulk_create(
ElementLink(parent=page, child=tr, order=i)
for i, tr in enumerate(transcriptions, max_order)
ElementLink(parent=page, child=elt, order=i)
for i, elt in enumerate(elements, max_order)
)
return transcriptions
......
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