Browse Source

Add TextLabelManager

pull/1650/head
Hironsan 2 years ago
parent
commit
4dcd43bedb
3 changed files with 99 additions and 1 deletions
  1. 10
      backend/api/managers.py
  2. 6
      backend/api/models.py
  3. 84
      backend/api/tests/test_text_label.py

10
backend/api/managers.py

@ -66,6 +66,16 @@ class SpanManager(AnnotationManager):
return True
class TextLabelManager(AnnotationManager):
def can_annotate(self, label, project) -> bool:
texts = self.get_labels(label, project)
for text in texts:
if text.is_same_text(label):
return False
return True
class ExampleManager(Manager):
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):

6
backend/api/models.py

@ -9,7 +9,7 @@ from django.db import models
from polymorphic.models import PolymorphicModel
from .managers import (AnnotationManager, CategoryManager, ExampleManager,
ExampleStateManager, SpanManager)
ExampleStateManager, SpanManager, TextLabelManager)
DOCUMENT_CLASSIFICATION = 'DocumentClassification'
SEQUENCE_LABELING = 'SequenceLabeling'
@ -400,6 +400,7 @@ class Span(Annotation):
class TextLabel(Annotation):
objects = TextLabelManager()
example = models.ForeignKey(
to=Example,
on_delete=models.CASCADE,
@ -407,6 +408,9 @@ class TextLabel(Annotation):
)
text = models.TextField()
def is_same_text(self, other: 'TextLabel'):
return self.text == other.text
class Meta:
unique_together = (
'example',

84
backend/api/tests/test_text_label.py

@ -0,0 +1,84 @@
import abc
from django.test import TestCase
from model_mommy import mommy
from api.models import SEQ2SEQ, TextLabel
from .api.utils import prepare_project
class TestTextLabelAnnotation(abc.ABC, TestCase):
collaborative = False
@classmethod
def setUpTestData(cls):
cls.project = prepare_project(
SEQ2SEQ,
collaborative_annotation=cls.collaborative
)
cls.example = mommy.make('Example', project=cls.project.item)
users = cls.project.users
cls.user = users[0]
cls.another_user = users[1]
cls.text_label = TextLabel(
example=cls.example,
user=cls.user,
text='foo'
)
def test_can_annotate_category_to_unannotated_data(self):
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertTrue(can_annotate)
class TestNonCollaborativeTextLabelAnnotation(TestTextLabelAnnotation):
collaborative = False
def test_cannot_annotate_same_text_to_annotated_data(self):
mommy.make(
'TextLabel',
example=self.example,
user=self.user,
text=self.text_label.text
)
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertFalse(can_annotate)
def test_can_annotate_different_text_to_annotated_data(self):
mommy.make('TextLabel', example=self.example, user=self.user)
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertTrue(can_annotate)
def test_allow_another_user_to_annotate_same_text(self):
mommy.make(
'TextLabel',
example=self.example,
user=self.another_user,
text=self.text_label.text
)
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertTrue(can_annotate)
class TestCollaborativeTextLabelAnnotation(TestTextLabelAnnotation):
collaborative = True
def test_deny_another_user_to_annotate_same_text(self):
mommy.make(
'TextLabel',
example=self.example,
user=self.another_user,
text=self.text_label.text
)
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertFalse(can_annotate)
def test_allow_another_user_to_annotate_different_text(self):
mommy.make(
'TextLabel',
example=self.example,
user=self.another_user
)
can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
self.assertTrue(can_annotate)
Loading…
Cancel
Save