You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.1 KiB

5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import string
  2. from django.db import models
  3. from django.urls import reverse
  4. from django.contrib.auth.models import User
  5. from django.contrib.staticfiles.storage import staticfiles_storage
  6. from django.core.exceptions import ValidationError
  7. from polymorphic.models import PolymorphicModel
  8. DOCUMENT_CLASSIFICATION = 'DocumentClassification'
  9. SEQUENCE_LABELING = 'SequenceLabeling'
  10. SEQ2SEQ = 'Seq2seq'
  11. PROJECT_CHOICES = (
  12. (DOCUMENT_CLASSIFICATION, 'document classification'),
  13. (SEQUENCE_LABELING, 'sequence labeling'),
  14. (SEQ2SEQ, 'sequence to sequence'),
  15. )
  16. class Project(PolymorphicModel):
  17. name = models.CharField(max_length=100)
  18. description = models.TextField(default='')
  19. guideline = models.TextField(default='')
  20. created_at = models.DateTimeField(auto_now_add=True)
  21. updated_at = models.DateTimeField(auto_now=True)
  22. users = models.ManyToManyField(User, related_name='projects')
  23. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  24. randomize_document_order = models.BooleanField(default=False)
  25. def get_absolute_url(self):
  26. return reverse('upload', args=[self.id])
  27. @property
  28. def image(self):
  29. raise NotImplementedError()
  30. def get_bundle_name(self):
  31. raise NotImplementedError()
  32. def get_bundle_name_upload(self):
  33. raise NotImplementedError()
  34. def get_bundle_name_download(self):
  35. raise NotImplementedError()
  36. def get_annotation_serializer(self):
  37. raise NotImplementedError()
  38. def get_annotation_class(self):
  39. raise NotImplementedError()
  40. def get_storage(self, data):
  41. raise NotImplementedError()
  42. def __str__(self):
  43. return self.name
  44. class TextClassificationProject(Project):
  45. @property
  46. def image(self):
  47. return staticfiles_storage.url('assets/images/cats/text_classification.jpg')
  48. def get_bundle_name(self):
  49. return 'document_classification'
  50. def get_bundle_name_upload(self):
  51. return 'upload_text_classification'
  52. def get_bundle_name_download(self):
  53. return 'download_text_classification'
  54. def get_annotation_serializer(self):
  55. from .serializers import DocumentAnnotationSerializer
  56. return DocumentAnnotationSerializer
  57. def get_annotation_class(self):
  58. return DocumentAnnotation
  59. def get_storage(self, data):
  60. from .utils import ClassificationStorage
  61. return ClassificationStorage(data, self)
  62. class SequenceLabelingProject(Project):
  63. @property
  64. def image(self):
  65. return staticfiles_storage.url('assets/images/cats/sequence_labeling.jpg')
  66. def get_bundle_name(self):
  67. return 'sequence_labeling'
  68. def get_bundle_name_upload(self):
  69. return 'upload_sequence_labeling'
  70. def get_bundle_name_download(self):
  71. return 'download_sequence_labeling'
  72. def get_annotation_serializer(self):
  73. from .serializers import SequenceAnnotationSerializer
  74. return SequenceAnnotationSerializer
  75. def get_annotation_class(self):
  76. return SequenceAnnotation
  77. def get_storage(self, data):
  78. from .utils import SequenceLabelingStorage
  79. return SequenceLabelingStorage(data, self)
  80. class Seq2seqProject(Project):
  81. @property
  82. def image(self):
  83. return staticfiles_storage.url('assets/images/cats/seq2seq.jpg')
  84. def get_bundle_name(self):
  85. return 'seq2seq'
  86. def get_bundle_name_upload(self):
  87. return 'upload_seq2seq'
  88. def get_bundle_name_download(self):
  89. return 'download_seq2seq'
  90. def get_annotation_serializer(self):
  91. from .serializers import Seq2seqAnnotationSerializer
  92. return Seq2seqAnnotationSerializer
  93. def get_annotation_class(self):
  94. return Seq2seqAnnotation
  95. def get_storage(self, data):
  96. from .utils import Seq2seqStorage
  97. return Seq2seqStorage(data, self)
  98. class Label(models.Model):
  99. PREFIX_KEYS = (
  100. ('ctrl', 'ctrl'),
  101. ('shift', 'shift'),
  102. ('ctrl shift', 'ctrl shift')
  103. )
  104. SUFFIX_KEYS = tuple(
  105. (c, c) for c in string.ascii_lowercase
  106. )
  107. text = models.CharField(max_length=100)
  108. prefix_key = models.CharField(max_length=10, blank=True, null=True, choices=PREFIX_KEYS)
  109. suffix_key = models.CharField(max_length=1, blank=True, null=True, choices=SUFFIX_KEYS)
  110. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  111. background_color = models.CharField(max_length=7, default='#209cee')
  112. text_color = models.CharField(max_length=7, default='#ffffff')
  113. created_at = models.DateTimeField(auto_now_add=True)
  114. updated_at = models.DateTimeField(auto_now=True)
  115. def __str__(self):
  116. return self.text
  117. def clean(self):
  118. # Don't allow shortcut key not to have a suffix key.
  119. if self.prefix_key and not self.suffix_key:
  120. raise ValidationError('Shortcut key may not have a suffix key.')
  121. super().clean()
  122. def validate_unique(self, exclude=None):
  123. # Don't allow to save same shortcut key when prefix_key is null.
  124. if Label.objects.exclude(id=self.id).filter(suffix_key=self.suffix_key,
  125. prefix_key__isnull=True).exists():
  126. raise ValidationError('Duplicate key.')
  127. super().validate_unique(exclude)
  128. class Meta:
  129. unique_together = (
  130. ('project', 'text'),
  131. ('project', 'prefix_key', 'suffix_key')
  132. )
  133. class Document(models.Model):
  134. text = models.TextField()
  135. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  136. meta = models.TextField(default='{}')
  137. created_at = models.DateTimeField(auto_now_add=True)
  138. updated_at = models.DateTimeField(auto_now=True)
  139. def __str__(self):
  140. return self.text[:50]
  141. class Annotation(models.Model):
  142. prob = models.FloatField(default=0.0)
  143. manual = models.BooleanField(default=False)
  144. user = models.ForeignKey(User, on_delete=models.CASCADE)
  145. created_at = models.DateTimeField(auto_now_add=True)
  146. updated_at = models.DateTimeField(auto_now=True)
  147. class Meta:
  148. abstract = True
  149. class DocumentAnnotation(Annotation):
  150. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  151. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  152. class Meta:
  153. unique_together = ('document', 'user', 'label')
  154. class SequenceAnnotation(Annotation):
  155. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  156. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  157. start_offset = models.IntegerField()
  158. end_offset = models.IntegerField()
  159. def clean(self):
  160. if self.start_offset >= self.end_offset:
  161. raise ValidationError('start_offset is after end_offset')
  162. class Meta:
  163. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  164. class Seq2seqAnnotation(Annotation):
  165. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  166. text = models.TextField()
  167. class Meta:
  168. unique_together = ('document', 'user', 'text')