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.

211 lines
7.7 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
  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_annotation_serializer(self):
  72. from .serializers import DocumentAnnotationSerializer
  73. from .serializers import SequenceAnnotationSerializer
  74. from .serializers import Seq2seqAnnotationSerializer
  75. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  76. return DocumentAnnotationSerializer
  77. elif self.is_type_of(Project.SEQUENCE_LABELING):
  78. return SequenceAnnotationSerializer
  79. elif self.is_type_of(Project.Seq2seq):
  80. return Seq2seqAnnotationSerializer
  81. def get_annotation_class(self):
  82. if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  83. return DocumentAnnotation
  84. elif self.is_type_of(Project.SEQUENCE_LABELING):
  85. return SequenceAnnotation
  86. elif self.is_type_of(Project.Seq2seq):
  87. return Seq2seqAnnotation
  88. def __str__(self):
  89. return self.name
  90. class Label(models.Model):
  91. KEY_CHOICES = ((u, c) for u, c in zip(string.ascii_lowercase, string.ascii_lowercase))
  92. COLOR_CHOICES = ()
  93. text = models.CharField(max_length=100)
  94. shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)
  95. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  96. background_color = models.CharField(max_length=7, default='#209cee')
  97. text_color = models.CharField(max_length=7, default='#ffffff')
  98. def __str__(self):
  99. return self.text
  100. class Meta:
  101. unique_together = (
  102. ('project', 'text'),
  103. ('project', 'shortcut')
  104. )
  105. class Document(models.Model):
  106. text = models.TextField()
  107. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  108. def get_annotations(self):
  109. if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  110. return self.doc_annotations.all()
  111. elif self.project.is_type_of(Project.SEQUENCE_LABELING):
  112. return self.seq_annotations.all()
  113. elif self.project.is_type_of(Project.Seq2seq):
  114. return self.seq2seq_annotations.all()
  115. def make_dataset(self):
  116. if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  117. return self.make_dataset_for_classification()
  118. elif self.project.is_type_of(Project.SEQUENCE_LABELING):
  119. return self.make_dataset_for_sequence_labeling()
  120. elif self.project.is_type_of(Project.Seq2seq):
  121. return self.make_dataset_for_seq2seq()
  122. def make_dataset_for_classification(self):
  123. annotations = self.get_annotations()
  124. dataset = [[a.document.id, a.document.text, a.label.text, a.user.username]
  125. for a in annotations]
  126. return dataset
  127. def make_dataset_for_sequence_labeling(self):
  128. annotations = self.get_annotations()
  129. dataset = [[self.id, ch, 'O'] for ch in self.text]
  130. for a in annotations:
  131. for i in range(a.start_offset, a.end_offset):
  132. if i == a.start_offset:
  133. dataset[i][2] = 'B-{}'.format(a.label.text)
  134. else:
  135. dataset[i][2] = 'I-{}'.format(a.label.text)
  136. return dataset
  137. def make_dataset_for_seq2seq(self):
  138. annotations = self.get_annotations()
  139. dataset = [[a.document.id, a.document.text, a.text, a.user.username]
  140. for a in annotations]
  141. return dataset
  142. def __str__(self):
  143. return self.text[:50]
  144. class Annotation(models.Model):
  145. prob = models.FloatField(default=0.0)
  146. manual = models.BooleanField(default=False)
  147. user = models.ForeignKey(User, on_delete=models.CASCADE)
  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. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  167. text = models.TextField()
  168. class Meta:
  169. unique_together = ('document', 'user', 'text')