models.py 1.22 KB
from django.contrib.postgres.fields import JSONField
from django.db import models

from storage.models import Chunk, Participant


class Utterance(models.Model):
    chunk = models.ForeignKey(Chunk, related_name='utterances', on_delete=models.CASCADE)
    sequence = models.PositiveIntegerField()
    speaker = models.ForeignKey(Participant, related_name='utterances', on_delete=models.CASCADE)
    original_text = models.TextField(blank=True)
    text = models.TextField()
    anno = JSONField(blank=True, null=True)

    def words_count(self):
        return len(self.text.split())

    def segments_count(self):
        segments_count = 0
        for chunk in self.anno['chunks']:
            for sent in chunk['sentences']:
                segments_count += len(sent['tokens'])
        return segments_count

    class Meta:
        db_table = 'utterance'
        ordering = ['sequence']

    def __str__(self):
        return '(%s) %s' % (str(self.speaker), self.text)


class Function(models.Model):
    iri = models.URLField(unique=True)
    participants = models.ManyToManyField(Participant, related_name='functions')

    class Meta:
        db_table = 'function'
        ordering = ['iri']

    def __str__(self):
        return self.iri