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.

97 lines
3.0 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. class Project(models.Model):
  6. DOCUMENT_CLASSIFICATION = 'DocumentClassification'
  7. SEQUENCE_LABELING = 'SequenceLabeling'
  8. Seq2seq = 'Seq2seq'
  9. PROJECT_CHOICES = (
  10. (DOCUMENT_CLASSIFICATION, 'document classification'),
  11. (SEQUENCE_LABELING, 'sequence labeling'),
  12. (Seq2seq, 'sequence to sequence'),
  13. )
  14. name = models.CharField(max_length=100)
  15. description = models.CharField(max_length=500)
  16. guideline = models.TextField()
  17. created_at = models.DateTimeField(auto_now_add=True)
  18. updated_at = models.DateTimeField(auto_now=True)
  19. users = models.ManyToManyField(User)
  20. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  21. def is_type_of(self, project_type):
  22. return project_type == self.project_type
  23. def __str__(self):
  24. return self.name
  25. class Label(models.Model):
  26. KEY_CHOICES = ((U, c) for U, c in zip(string.ascii_uppercase, string.ascii_lowercase))
  27. COLOR_CHOICES = ()
  28. text = models.CharField(max_length=100)
  29. shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)
  30. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  31. background_color = models.CharField(max_length=7, default='#209cee')
  32. text_color = models.CharField(max_length=7, default='#ffffff')
  33. def __str__(self):
  34. return self.text
  35. class Meta:
  36. unique_together = (
  37. ('project', 'text'),
  38. ('project', 'shortcut')
  39. )
  40. class Document(models.Model):
  41. text = models.TextField()
  42. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  43. def __str__(self):
  44. return self.text[:50]
  45. class Annotation(models.Model):
  46. prob = models.FloatField(default=0.0)
  47. manual = models.BooleanField(default=False)
  48. user = models.ForeignKey(User, on_delete=models.CASCADE)
  49. class Meta:
  50. abstract = True
  51. class DocumentAnnotation(Annotation):
  52. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  53. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  54. class Meta:
  55. unique_together = ('document', 'user', 'label')
  56. class SequenceAnnotation(Annotation):
  57. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  58. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  59. start_offset = models.IntegerField()
  60. end_offset = models.IntegerField()
  61. def clean(self):
  62. if self.start_offset >= self.end_offset:
  63. raise ValidationError('start_offset is after end_offset')
  64. class Meta:
  65. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  66. class Seq2seqAnnotation(Annotation):
  67. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  68. text = models.TextField()
  69. class Meta:
  70. unique_together = ('document', 'user', 'text')