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.

220 lines
6.6 KiB

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
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 rest_framework.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. def get_key_choices():
  17. selectKey, shortKey = [c for c in string.ascii_lowercase], [c for c in string.ascii_lowercase]
  18. checkKey = 'ctrl shift'
  19. shortKey += [ck + ' ' + sk for ck in checkKey.split() for sk in selectKey]
  20. shortKey += [checkKey + ' ' + sk for sk in selectKey]
  21. shortKey += ['']
  22. KEY_CHOICES = ((u, c) for u, c in zip(shortKey, shortKey))
  23. return KEY_CHOICES
  24. class Project(PolymorphicModel):
  25. name = models.CharField(max_length=100)
  26. description = models.TextField(default='')
  27. guideline = models.TextField(default='')
  28. created_at = models.DateTimeField(auto_now_add=True)
  29. updated_at = models.DateTimeField(auto_now=True)
  30. users = models.ManyToManyField(User, related_name='projects')
  31. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  32. def get_absolute_url(self):
  33. return reverse('upload', args=[self.id])
  34. @property
  35. def image(self):
  36. raise NotImplementedError()
  37. def get_template_name(self):
  38. raise NotImplementedError()
  39. def get_upload_template(self):
  40. raise NotImplementedError()
  41. def get_download_template(self):
  42. raise NotImplementedError()
  43. def get_annotation_serializer(self):
  44. raise NotImplementedError()
  45. def get_annotation_class(self):
  46. raise NotImplementedError()
  47. def get_storage(self, data):
  48. raise NotImplementedError()
  49. def __str__(self):
  50. return self.name
  51. class TextClassificationProject(Project):
  52. @property
  53. def image(self):
  54. return staticfiles_storage.url('images/cats/text_classification.jpg')
  55. def get_template_name(self):
  56. return 'annotation/document_classification.html'
  57. def get_upload_template(self):
  58. return 'admin/upload/text_classification.html'
  59. def get_download_template(self):
  60. return 'admin/download/text_classification.html'
  61. def get_annotation_serializer(self):
  62. from .serializers import DocumentAnnotationSerializer
  63. return DocumentAnnotationSerializer
  64. def get_annotation_class(self):
  65. return DocumentAnnotation
  66. def get_storage(self, data):
  67. from .utils import ClassificationStorage
  68. return ClassificationStorage(data, self)
  69. class SequenceLabelingProject(Project):
  70. @property
  71. def image(self):
  72. return staticfiles_storage.url('images/cats/sequence_labeling.jpg')
  73. def get_template_name(self):
  74. return 'annotation/sequence_labeling.html'
  75. def get_upload_template(self):
  76. return 'admin/upload/sequence_labeling.html'
  77. def get_download_template(self):
  78. return 'admin/download/sequence_labeling.html'
  79. def get_annotation_serializer(self):
  80. from .serializers import SequenceAnnotationSerializer
  81. return SequenceAnnotationSerializer
  82. def get_annotation_class(self):
  83. return SequenceAnnotation
  84. def get_storage(self, data):
  85. from .utils import SequenceLabelingStorage
  86. return SequenceLabelingStorage(data, self)
  87. class Seq2seqProject(Project):
  88. @property
  89. def image(self):
  90. return staticfiles_storage.url('images/cats/seq2seq.jpg')
  91. def get_template_name(self):
  92. return 'annotation/seq2seq.html'
  93. def get_upload_template(self):
  94. return 'admin/upload/seq2seq.html'
  95. def get_download_template(self):
  96. return 'admin/download/seq2seq.html'
  97. def get_annotation_serializer(self):
  98. from .serializers import Seq2seqAnnotationSerializer
  99. return Seq2seqAnnotationSerializer
  100. def get_annotation_class(self):
  101. return Seq2seqAnnotation
  102. def get_storage(self, data):
  103. from .utils import Seq2seqStorage
  104. return Seq2seqStorage(data, self)
  105. class Label(models.Model):
  106. KEY_CHOICES = get_key_choices()
  107. COLOR_CHOICES = ()
  108. text = models.CharField(max_length=100)
  109. shortcut = models.CharField(max_length=15, blank=True, null=True, choices=KEY_CHOICES)
  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. class Meta:
  118. unique_together = (
  119. ('project', 'text'),
  120. ('project', 'shortcut')
  121. )
  122. class Document(models.Model):
  123. text = models.TextField()
  124. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  125. meta = models.TextField(default='{}')
  126. created_at = models.DateTimeField(auto_now_add=True)
  127. updated_at = models.DateTimeField(auto_now=True)
  128. def __str__(self):
  129. return self.text[:50]
  130. class Annotation(models.Model):
  131. prob = models.FloatField(default=0.0)
  132. manual = models.BooleanField(default=False)
  133. user = models.ForeignKey(User, on_delete=models.CASCADE)
  134. created_at = models.DateTimeField(auto_now_add=True)
  135. updated_at = models.DateTimeField(auto_now=True)
  136. class Meta:
  137. abstract = True
  138. class DocumentAnnotation(Annotation):
  139. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  140. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  141. class Meta:
  142. unique_together = ('document', 'user', 'label')
  143. class SequenceAnnotation(Annotation):
  144. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  145. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  146. start_offset = models.IntegerField()
  147. end_offset = models.IntegerField()
  148. def clean(self):
  149. if self.start_offset >= self.end_offset:
  150. raise ValidationError('start_offset is after end_offset')
  151. class Meta:
  152. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  153. class Seq2seqAnnotation(Annotation):
  154. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  155. text = models.TextField()
  156. class Meta:
  157. unique_together = ('document', 'user', 'text')