mirror of https://github.com/doccano/doccano.git
pythondatasetsactive-learningtext-annotationdatasetnatural-language-processingdata-labelingmachine-learningannotation-tool
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.
112 lines
3.5 KiB
112 lines
3.5 KiB
from rest_framework import serializers
|
|
|
|
from .models import Label, Project, Document
|
|
from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
|
|
|
|
|
|
class LabelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Label
|
|
fields = ('id', 'text', 'shortcut', 'background_color', 'text_color')
|
|
|
|
|
|
class DocumentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Document
|
|
fields = ('id', 'text')
|
|
|
|
|
|
class ProjectSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Project
|
|
fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
|
|
|
|
|
|
class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
|
|
|
|
def get_queryset(self):
|
|
view = self.context.get('view', None)
|
|
request = self.context.get('request', None)
|
|
queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
|
|
if not request or not queryset or not view:
|
|
return None
|
|
return queryset.filter(project=view.kwargs['project_id'])
|
|
|
|
|
|
class DocumentAnnotationSerializer(serializers.ModelSerializer):
|
|
label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
|
|
|
|
class Meta:
|
|
model = DocumentAnnotation
|
|
fields = ('id', 'prob', 'label')
|
|
|
|
def create(self, validated_data):
|
|
annotation = DocumentAnnotation.objects.create(**validated_data)
|
|
return annotation
|
|
|
|
|
|
class SequenceAnnotationSerializer(serializers.ModelSerializer):
|
|
label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
|
|
|
|
class Meta:
|
|
model = SequenceAnnotation
|
|
fields = ('id', 'prob', 'label', 'start_offset', 'end_offset')
|
|
|
|
def create(self, validated_data):
|
|
annotation = SequenceAnnotation.objects.create(**validated_data)
|
|
return annotation
|
|
|
|
|
|
class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Seq2seqAnnotation
|
|
fields = ('id', 'text')
|
|
|
|
|
|
class ClassificationDocumentSerializer(serializers.ModelSerializer):
|
|
annotations = serializers.SerializerMethodField()
|
|
|
|
def get_annotations(self, instance):
|
|
request = self.context.get('request')
|
|
if request:
|
|
annotations = instance.doc_annotations.filter(user=request.user)
|
|
serializer = DocumentAnnotationSerializer(annotations, many=True)
|
|
return serializer.data
|
|
|
|
class Meta:
|
|
model = Document
|
|
fields = ('id', 'text', 'annotations')
|
|
|
|
|
|
class SequenceDocumentSerializer(serializers.ModelSerializer):
|
|
annotations = serializers.SerializerMethodField()
|
|
|
|
def get_annotations(self, instance):
|
|
request = self.context.get('request')
|
|
if request:
|
|
annotations = instance.seq_annotations.filter(user=request.user)
|
|
serializer = SequenceAnnotationSerializer(annotations, many=True)
|
|
return serializer.data
|
|
|
|
class Meta:
|
|
model = Document
|
|
fields = ('id', 'text', 'annotations')
|
|
|
|
|
|
class Seq2seqDocumentSerializer(serializers.ModelSerializer):
|
|
annotations = serializers.SerializerMethodField()
|
|
|
|
def get_annotations(self, instance):
|
|
request = self.context.get('request')
|
|
if request:
|
|
annotations = instance.seq2seq_annotations.filter(user=request.user)
|
|
serializer = Seq2seqAnnotationSerializer(annotations, many=True)
|
|
return serializer.data
|
|
|
|
class Meta:
|
|
model = Document
|
|
fields = ('id', 'text', 'annotations')
|