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.

172 lines
5.9 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
  1. import string
  2. from django.core.exceptions import ValidationError
  3. from django.db import models
  4. from django.contrib.auth.models import User
  5. from django.contrib.staticfiles.storage import staticfiles_storage
  6. class Project(models.Model):
  7. DOCUMENT_CLASSIFICATION = 'DocumentClassification'
  8. SEQUENCE_LABELING = 'SequenceLabeling'
  9. Seq2seq = 'Seq2seq'
  10. PROJECT_CHOICES = (
  11. (DOCUMENT_CLASSIFICATION, 'document classification'),
  12. (SEQUENCE_LABELING, 'sequence labeling'),
  13. (Seq2seq, 'sequence to sequence'),
  14. )
  15. name = models.CharField(max_length=100)
  16. description = models.CharField(max_length=500)
  17. guideline = models.TextField()
  18. created_at = models.DateTimeField(auto_now_add=True)
  19. updated_at = models.DateTimeField(auto_now=True)
  20. users = models.ManyToManyField(User)
  21. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  22. def is_type_of(self, project_type):
  23. return project_type == self.project_type
  24. @property
  25. def image(self):
  26. if self.is_type_of(self.DOCUMENT_CLASSIFICATION):
  27. url = staticfiles_storage.url('images/cat-1045782_640.jpg')
  28. elif self.is_type_of(self.SEQUENCE_LABELING):
  29. url = staticfiles_storage.url('images/cat-3449999_640.jpg')
  30. elif self.is_type_of(self.Seq2seq):
  31. url = staticfiles_storage.url('images/tiger-768574_640.jpg')
  32. return url
  33. def __str__(self):
  34. return self.name
  35. class Label(models.Model):
  36. KEY_CHOICES = ((u, c) for u, c in zip(string.ascii_lowercase, string.ascii_lowercase))
  37. COLOR_CHOICES = ()
  38. text = models.CharField(max_length=100)
  39. shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)
  40. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  41. background_color = models.CharField(max_length=7, default='#209cee')
  42. text_color = models.CharField(max_length=7, default='#ffffff')
  43. def __str__(self):
  44. return self.text
  45. class Meta:
  46. unique_together = (
  47. ('project', 'text'),
  48. ('project', 'shortcut')
  49. )
  50. class Document(models.Model):
  51. text = models.TextField()
  52. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  53. def __str__(self):
  54. return self.text[:50]
  55. class Annotation(models.Model):
  56. prob = models.FloatField(default=0.0)
  57. manual = models.BooleanField(default=False)
  58. user = models.ForeignKey(User, on_delete=models.CASCADE)
  59. class Meta:
  60. abstract = True
  61. class DocumentAnnotation(Annotation):
  62. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  63. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  64. class Meta:
  65. unique_together = ('document', 'user', 'label')
  66. class SequenceAnnotation(Annotation):
  67. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  68. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  69. start_offset = models.IntegerField()
  70. end_offset = models.IntegerField()
  71. def clean(self):
  72. if self.start_offset >= self.end_offset:
  73. raise ValidationError('start_offset is after end_offset')
  74. class Meta:
  75. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  76. class Seq2seqAnnotation(Annotation):
  77. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  78. text = models.TextField()
  79. class Meta:
  80. unique_together = ('document', 'user', 'text')
  81. from .serializers import *
  82. # temporary solution
  83. class Factory(object):
  84. @classmethod
  85. def get_template(cls, project):
  86. if project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  87. template_name = 'annotation/document_classification.html'
  88. elif project.is_type_of(Project.SEQUENCE_LABELING):
  89. template_name = 'annotation/sequence_labeling.html'
  90. elif project.is_type_of(Project.Seq2seq):
  91. template_name = 'annotation/seq2seq.html'
  92. else:
  93. raise ValueError('Template does not exist')
  94. return template_name
  95. @classmethod
  96. def get_documents(cls, project, is_null=True):
  97. docs = project.documents.all()
  98. if project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  99. docs = docs.filter(doc_annotations__isnull=is_null)
  100. elif project.is_type_of(Project.SEQUENCE_LABELING):
  101. docs = docs.filter(seq_annotations__isnull=is_null)
  102. elif project.is_type_of(Project.Seq2seq):
  103. docs = docs.filter(seq2seq_annotations__isnull=is_null)
  104. else:
  105. raise ValueError('Invalid project_type')
  106. return docs
  107. @classmethod
  108. def get_project_serializer(cls, project):
  109. if project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  110. return DocumentSerializer
  111. elif project.is_type_of(Project.SEQUENCE_LABELING):
  112. return SequenceSerializer
  113. elif project.is_type_of(Project.Seq2seq):
  114. return Seq2seqSerializer
  115. else:
  116. raise ValueError('Invalid project_type')
  117. @classmethod
  118. def get_annotation_serializer(cls, project):
  119. if project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  120. return DocumentAnnotationSerializer
  121. elif project.is_type_of(Project.SEQUENCE_LABELING):
  122. return SequenceAnnotationSerializer
  123. elif project.is_type_of(Project.Seq2seq):
  124. return Seq2seqAnnotationSerializer
  125. @classmethod
  126. def get_annotations_by_doc(cls, document):
  127. if document.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
  128. return document.doc_annotations.all()
  129. elif document.project.is_type_of(Project.SEQUENCE_LABELING):
  130. return document.seq_annotations.all()
  131. elif document.project.is_type_of(Project.Seq2seq):
  132. return document.seq2seq_annotations.all()