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.

109 lines
3.5 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')