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.

117 lines
4.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from django.contrib.auth.models import User
  2. from django.core.exceptions import ValidationError
  3. from django.db import models
  4. from .managers import (
  5. CategoryManager,
  6. LabelManager,
  7. RelationManager,
  8. SpanManager,
  9. TextLabelManager,
  10. )
  11. from examples.models import Example
  12. from label_types.models import CategoryType, RelationType, SpanType
  13. class Label(models.Model):
  14. objects = LabelManager()
  15. prob = models.FloatField(default=0.0)
  16. manual = models.BooleanField(default=False)
  17. user = models.ForeignKey(User, on_delete=models.CASCADE)
  18. created_at = models.DateTimeField(auto_now_add=True)
  19. updated_at = models.DateTimeField(auto_now=True)
  20. class Meta:
  21. abstract = True
  22. class Category(Label):
  23. objects = CategoryManager()
  24. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="categories")
  25. label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE)
  26. class Meta:
  27. unique_together = ("example", "user", "label")
  28. class Span(Label):
  29. objects = SpanManager()
  30. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="spans")
  31. label = models.ForeignKey(to=SpanType, on_delete=models.CASCADE)
  32. start_offset = models.IntegerField()
  33. end_offset = models.IntegerField()
  34. def validate_unique(self, exclude=None):
  35. allow_overlapping = getattr(self.example.project, "allow_overlapping", False)
  36. is_collaborative = self.example.project.collaborative_annotation
  37. if allow_overlapping:
  38. super().validate_unique(exclude=exclude)
  39. return
  40. overlapping_span = (
  41. Span.objects.exclude(id=self.id)
  42. .filter(example=self.example)
  43. .filter(
  44. models.Q(start_offset__gte=self.start_offset, start_offset__lt=self.end_offset)
  45. | models.Q(end_offset__gt=self.start_offset, end_offset__lte=self.end_offset)
  46. | models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset)
  47. )
  48. )
  49. if is_collaborative:
  50. if overlapping_span.exists():
  51. raise ValidationError("This overlapping is not allowed in this project.")
  52. else:
  53. if overlapping_span.filter(user=self.user).exists():
  54. raise ValidationError("This overlapping is not allowed in this project.")
  55. def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
  56. self.full_clean()
  57. super().save(force_insert, force_update, using, update_fields)
  58. def is_overlapping(self, other: "Span"):
  59. return (
  60. (other.start_offset <= self.start_offset < other.end_offset)
  61. or (other.start_offset < self.end_offset <= other.end_offset)
  62. or (self.start_offset < other.start_offset and other.end_offset < self.end_offset)
  63. )
  64. class Meta:
  65. constraints = [
  66. models.CheckConstraint(check=models.Q(start_offset__gte=0), name="startOffset >= 0"),
  67. models.CheckConstraint(check=models.Q(end_offset__gte=0), name="endOffset >= 0"),
  68. models.CheckConstraint(check=models.Q(start_offset__lt=models.F("end_offset")), name="start < end"),
  69. ]
  70. class TextLabel(Label):
  71. objects = TextLabelManager()
  72. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="texts")
  73. text = models.TextField()
  74. def is_same_text(self, other: "TextLabel"):
  75. return self.text == other.text
  76. class Meta:
  77. unique_together = ("example", "user", "text")
  78. class Relation(Label):
  79. objects = RelationManager()
  80. from_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="from_relations")
  81. to_id = models.ForeignKey(Span, on_delete=models.CASCADE, related_name="to_relations")
  82. type = models.ForeignKey(RelationType, on_delete=models.CASCADE)
  83. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="relations")
  84. def __str__(self):
  85. return self.__dict__.__str__()
  86. def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
  87. self.full_clean()
  88. super().save(force_insert, force_update, using, update_fields)
  89. def clean(self):
  90. same_example = self.from_id.example == self.to_id.example == self.example
  91. if not same_example:
  92. raise ValidationError("You need to label the same example.")
  93. return super().clean()