models.py
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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