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.

224 lines
8.3 KiB

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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import string
  2. from django.core.exceptions import ValidationError
  3. from django.db import models
  4. from django.urls import reverse
  5. from django.contrib.auth.models import User
  6. from django.contrib.staticfiles.storage import staticfiles_storage
  7. class Project(models.Model):
  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. name = models.CharField(max_length=100)
  17. description = models.CharField(max_length=500)
  18. guideline = models.TextField()
  19. created_at = models.DateTimeField(auto_now_add=True)
  20. updated_at = models.DateTimeField(auto_now=True)
  21. users = models.ManyToManyField(User, related_name='projects')
  22. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  23. def get_absolute_url(self):
  24. return reverse('upload', args=[self.id])
  25. def is_type_of(self, project_type):
  26. return project_type == self.project_type
  27. def get_progress(self, user):
  28. docs = self.get_documents(is_null=True, user=user)
  29. total = self.documents.count()
  30. remaining = docs.count()
  31. return {'total': total, 'remaining': remaining}
  32. @property
  33. def image(self):
  34. if self.is_type_of(self.DOCUMENT_CLASSIFICATION):
  35. url = staticfiles_storage.url('images/cat-1045782_640.jpg')
  36. elif self.is_type_of(self.SEQUENCE_LABELING):
  37. url = staticfiles_storage.url('images/cat-3449999_640.jpg')
  38. elif self.is_type_of(self.Seq2seq):
  39. url = staticfiles_storage.url('images/tiger-768574_640.jpg')
  40. return url
  41. def get_template_name(self):
  42. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  43. template_name = 'annotation/document_classification.html'
  44. elif self.is_type_of(Project.SEQUENCE_LABELING):
  45. template_name = 'annotation/sequence_labeling.html'
  46. elif self.is_type_of(Project.Seq2seq):
  47. template_name = 'annotation/seq2seq.html'
  48. else:
  49. raise ValueError('Template does not exist')
  50. return template_name
  51. def get_documents(self, is_null=True, user=None):
  52. docs = self.documents.all()
  53. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  54. if user:
  55. docs = docs.exclude(doc_annotations__user=user)
  56. else:
  57. docs = docs.filter(doc_annotations__isnull=is_null)
  58. elif self.is_type_of(Project.SEQUENCE_LABELING):
  59. if user:
  60. docs = docs.exclude(seq_annotations__user=user)
  61. else:
  62. docs = docs.filter(seq_annotations__isnull=is_null)
  63. elif self.is_type_of(Project.Seq2seq):
  64. if user:
  65. docs = docs.exclude(seq2seq_annotations__user=user)
  66. else:
  67. docs = docs.filter(seq2seq_annotations__isnull=is_null)
  68. else:
  69. raise ValueError('Invalid project_type')
  70. return docs
  71. def get_document_serializer(self):
  72. from .serializers import ClassificationDocumentSerializer
  73. from .serializers import SequenceDocumentSerializer
  74. from .serializers import Seq2seqDocumentSerializer
  75. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  76. return ClassificationDocumentSerializer
  77. elif self.is_type_of(Project.SEQUENCE_LABELING):
  78. return SequenceDocumentSerializer
  79. elif self.is_type_of(Project.Seq2seq):
  80. return Seq2seqDocumentSerializer
  81. else:
  82. raise ValueError('Invalid project_type')
  83. def get_annotation_serializer(self):
  84. from .serializers import DocumentAnnotationSerializer
  85. from .serializers import SequenceAnnotationSerializer
  86. from .serializers import Seq2seqAnnotationSerializer
  87. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  88. return DocumentAnnotationSerializer
  89. elif self.is_type_of(Project.SEQUENCE_LABELING):
  90. return SequenceAnnotationSerializer
  91. elif self.is_type_of(Project.Seq2seq):
  92. return Seq2seqAnnotationSerializer
  93. def get_annotation_class(self):
  94. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  95. return DocumentAnnotation
  96. elif self.is_type_of(Project.SEQUENCE_LABELING):
  97. return SequenceAnnotation
  98. elif self.is_type_of(Project.Seq2seq):
  99. return Seq2seqAnnotation
  100. def __str__(self):
  101. return self.name
  102. class Label(models.Model):
  103. KEY_CHOICES = ((u, c) for u, c in zip(string.ascii_lowercase, string.ascii_lowercase))
  104. COLOR_CHOICES = ()
  105. text = models.CharField(max_length=100)
  106. shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)
  107. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  108. background_color = models.CharField(max_length=7, default='#209cee')
  109. text_color = models.CharField(max_length=7, default='#ffffff')
  110. def __str__(self):
  111. return self.text
  112. class Meta:
  113. unique_together = (
  114. ('project', 'text'),
  115. ('project', 'shortcut')
  116. )
  117. class Document(models.Model):
  118. text = models.TextField()
  119. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  120. def get_annotations(self):
  121. if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  122. return self.doc_annotations.all()
  123. elif self.project.is_type_of(Project.SEQUENCE_LABELING):
  124. return self.seq_annotations.all()
  125. elif self.project.is_type_of(Project.Seq2seq):
  126. return self.seq2seq_annotations.all()
  127. def make_dataset(self):
  128. if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  129. return self.make_dataset_for_classification()
  130. elif self.project.is_type_of(Project.SEQUENCE_LABELING):
  131. return self.make_dataset_for_sequence_labeling()
  132. elif self.project.is_type_of(Project.Seq2seq):
  133. return self.make_dataset_for_seq2seq()
  134. def make_dataset_for_classification(self):
  135. annotations = self.get_annotations()
  136. dataset = [[a.document.id, a.document.text, a.label.text, a.user.username]
  137. for a in annotations]
  138. return dataset
  139. def make_dataset_for_sequence_labeling(self):
  140. annotations = self.get_annotations()
  141. dataset = [[self.id, ch, 'O'] for ch in self.text]
  142. for a in annotations:
  143. for i in range(a.start_offset, a.end_offset):
  144. if i == a.start_offset:
  145. dataset[i][2] = 'B-{}'.format(a.label.text)
  146. else:
  147. dataset[i][2] = 'I-{}'.format(a.label.text)
  148. return dataset
  149. def make_dataset_for_seq2seq(self):
  150. annotations = self.get_annotations()
  151. dataset = [[a.document.id, a.document.text, a.text, a.user.username]
  152. for a in annotations]
  153. return dataset
  154. def __str__(self):
  155. return self.text[:50]
  156. class Annotation(models.Model):
  157. prob = models.FloatField(default=0.0)
  158. manual = models.BooleanField(default=False)
  159. user = models.ForeignKey(User, on_delete=models.CASCADE)
  160. class Meta:
  161. abstract = True
  162. class DocumentAnnotation(Annotation):
  163. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  164. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  165. class Meta:
  166. unique_together = ('document', 'user', 'label')
  167. class SequenceAnnotation(Annotation):
  168. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  169. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  170. start_offset = models.IntegerField()
  171. end_offset = models.IntegerField()
  172. def clean(self):
  173. if self.start_offset >= self.end_offset:
  174. raise ValidationError('start_offset is after end_offset')
  175. class Meta:
  176. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  177. class Seq2seqAnnotation(Annotation):
  178. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  179. text = models.TextField()
  180. class Meta:
  181. unique_together = ('document', 'user', 'text')