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
  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. # each shortcut (prefix key + suffix key) can only be assigned to one label
  122. if self.suffix_key or self.prefix_key:
  123. other_labels = self.project.labels.exclude(id=self.id)
  124. if other_labels.filter(suffix_key=self.suffix_key, prefix_key=self.prefix_key).exists():
  125. raise ValidationError('A label with this shortcut already exists in the project')
  126. super().clean()
  127. class Meta:
  128. unique_together = (
  129. ('project', 'text'),
  130. )
  131. class Document(models.Model):
  132. text = models.TextField()
  133. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  134. meta = models.TextField(default='{}')
  135. created_at = models.DateTimeField(auto_now_add=True)
  136. updated_at = models.DateTimeField(auto_now=True)
  137. annotations_approved_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
  138. def __str__(self):
  139. return self.text[:50]
  140. class Annotation(models.Model):
  141. prob = models.FloatField(default=0.0)
  142. manual = models.BooleanField(default=False)
  143. user = models.ForeignKey(User, on_delete=models.CASCADE)
  144. created_at = models.DateTimeField(auto_now_add=True)
  145. updated_at = models.DateTimeField(auto_now=True)
  146. class Meta:
  147. abstract = True
  148. class DocumentAnnotation(Annotation):
  149. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  150. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  151. class Meta:
  152. unique_together = ('document', 'user', 'label')
  153. class SequenceAnnotation(Annotation):
  154. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  155. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  156. start_offset = models.IntegerField()
  157. end_offset = models.IntegerField()
  158. def clean(self):
  159. if self.start_offset >= self.end_offset:
  160. raise ValidationError('start_offset is after end_offset')
  161. class Meta:
  162. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  163. class Seq2seqAnnotation(Annotation):
  164. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  165. text = models.CharField(max_length=500)
  166. class Meta:
  167. unique_together = ('document', 'user', 'text')