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.

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