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.

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