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.

230 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
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. def get_absolute_url(self):
  25. return reverse('upload', args=[self.id])
  26. @property
  27. def image(self):
  28. raise NotImplementedError()
  29. def get_template_name(self):
  30. raise NotImplementedError()
  31. def get_upload_template(self):
  32. raise NotImplementedError()
  33. def get_download_template(self):
  34. raise NotImplementedError()
  35. def get_annotation_serializer(self):
  36. raise NotImplementedError()
  37. def get_annotation_class(self):
  38. raise NotImplementedError()
  39. def get_storage(self, data):
  40. raise NotImplementedError()
  41. def __str__(self):
  42. return self.name
  43. class TextClassificationProject(Project):
  44. @property
  45. def image(self):
  46. return staticfiles_storage.url('images/cats/text_classification.jpg')
  47. def get_template_name(self):
  48. return 'annotation/document_classification.html'
  49. def get_upload_template(self):
  50. return 'admin/upload/text_classification.html'
  51. def get_download_template(self):
  52. return 'admin/download/text_classification.html'
  53. def get_annotation_serializer(self):
  54. from .serializers import DocumentAnnotationSerializer
  55. return DocumentAnnotationSerializer
  56. def get_annotation_class(self):
  57. return DocumentAnnotation
  58. def get_storage(self, data):
  59. from .utils import ClassificationStorage
  60. return ClassificationStorage(data, self)
  61. class SequenceLabelingProject(Project):
  62. @property
  63. def image(self):
  64. return staticfiles_storage.url('images/cats/sequence_labeling.jpg')
  65. def get_template_name(self):
  66. return 'annotation/sequence_labeling.html'
  67. def get_upload_template(self):
  68. return 'admin/upload/sequence_labeling.html'
  69. def get_download_template(self):
  70. return 'admin/download/sequence_labeling.html'
  71. def get_annotation_serializer(self):
  72. from .serializers import SequenceAnnotationSerializer
  73. return SequenceAnnotationSerializer
  74. def get_annotation_class(self):
  75. return SequenceAnnotation
  76. def get_storage(self, data):
  77. from .utils import SequenceLabelingStorage
  78. return SequenceLabelingStorage(data, self)
  79. class Seq2seqProject(Project):
  80. @property
  81. def image(self):
  82. return staticfiles_storage.url('images/cats/seq2seq.jpg')
  83. def get_template_name(self):
  84. return 'annotation/seq2seq.html'
  85. def get_upload_template(self):
  86. return 'admin/upload/seq2seq.html'
  87. def get_download_template(self):
  88. return 'admin/download/seq2seq.html'
  89. def get_annotation_serializer(self):
  90. from .serializers import Seq2seqAnnotationSerializer
  91. return Seq2seqAnnotationSerializer
  92. def get_annotation_class(self):
  93. return Seq2seqAnnotation
  94. def get_storage(self, data):
  95. from .utils import Seq2seqStorage
  96. return Seq2seqStorage(data, self)
  97. class Label(models.Model):
  98. PREFIX_KEYS = (
  99. ('ctrl', 'ctrl'),
  100. ('shift', 'shift'),
  101. ('ctrl shift', 'ctrl shift')
  102. )
  103. SUFFIX_KEYS = (
  104. (c, c) for c in string.ascii_lowercase
  105. )
  106. text = models.CharField(max_length=100)
  107. prefix_key = models.CharField(max_length=10, blank=True, null=True, choices=PREFIX_KEYS)
  108. suffix_key = models.CharField(max_length=1, blank=True, null=True, choices=SUFFIX_KEYS)
  109. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  110. background_color = models.CharField(max_length=7, default='#209cee')
  111. text_color = models.CharField(max_length=7, default='#ffffff')
  112. created_at = models.DateTimeField(auto_now_add=True)
  113. updated_at = models.DateTimeField(auto_now=True)
  114. def __str__(self):
  115. return self.text
  116. def clean(self):
  117. # Don't allow shortcut key not to have a suffix key.
  118. if self.prefix_key and not self.suffix_key:
  119. raise ValidationError('Shortcut key may not have a suffix key.')
  120. super().clean()
  121. def validate_unique(self, exclude=None):
  122. # Don't allow to save same shortcut key when prefix_key is null.
  123. if Label.objects.exclude(id=self.id).filter(suffix_key=self.suffix_key,
  124. prefix_key__isnull=True).exists():
  125. raise ValidationError('Duplicate key.')
  126. super().validate_unique(exclude)
  127. class Meta:
  128. unique_together = (
  129. ('project', 'text'),
  130. ('project', 'prefix_key', 'suffix_key')
  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. 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.TextField()
  166. class Meta:
  167. unique_together = ('document', 'user', 'text')