models.py
3.72 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
from django.db.models import AutoField, BooleanField, CharField, \
FloatField, ForeignKey, Model, \
PositiveIntegerField, TextField, URLField
from django.db.models.fields.related import ManyToManyField
class Mngset(Model):
id = AutoField(primary_key=True)
def __unicode__(self):
return ' # '.join([unicode(mng) for mng in self.meanings.all()])
def merge_sets(pks):
merged_set = Mngset.objects.get(pk=pks[-1])
for mngset_pk in pks[:-1]:
mngset = Mngset.objects.get(pk=mngset_pk)
for mng in mngset.meanings.all():
mng.mngset = merged_set
mng.save()
mngset.delete()
class Meaning(Model):
id = AutoField(primary_key=True)
plWN_synset = PositiveIntegerField(blank=True, null=True)
wikilink = URLField(blank=True, null=True)
description = TextField(blank=True, null=True)
category = ForeignKey('Category', related_name='meanings')
mngset = ForeignKey('Mngset', related_name='meanings')
def update_mngset(self):
matching_sets = []
for expr in self.expressions.filter(main_expression=None):
matching_exprs = Expression.objects.filter(text=expr.text, main_expression=None)
for mexpr in matching_exprs:
matching_sets.append(mexpr.meaning.mngset.pk)
matching_sets = list(set(matching_sets))
if len(matching_sets) > 1:
merge_sets(matching_sets)
def __unicode__(self):
return ' = '.join(['<%s>' % expr.text if expr.main_expression else expr.text
for expr in self.expressions.all()])
def get_or_create_meaning(plWN_synset, wikilink, mngset):
created = False
meaning = None
if plWN_synset:
try:
meaning = Meaning.objects.get(plWN_synset=plWN_synset)
except Meaning.DoesNotExist:
pass
if wikilink and meaning == None:
try:
meaning = Meaning.objects.get(wikilink=wikilink)
except Meaning.DoesNotExist:
pass
if meaning == None:
category, _ = Category.objects.get_or_create(key='unk', name='unk', description='niezdefiniowana')
meaning = Meaning.objects.create(plWN_synset=plWN_synset, wikilink=wikilink,
category=category, mngset=mngset)
created = True
return meaning, created
class Expression(Model):
id = AutoField(primary_key=True)
text = TextField()
meaning = ForeignKey('Meaning', related_name='expressions')
sources = ManyToManyField('Source', related_name='expressions')
score = FloatField()
NKJP_freq = PositiveIntegerField()
is_catchword = BooleanField(default=False)
main_expression = ForeignKey('Expression', related_name='mentions',
blank=True, null=True)
def __unicode__(self):
return self.text
class Source(Model):
key = CharField(max_length=16, primary_key=True)
name = CharField(max_length=32)
description = TextField(blank=True, null=True)
url = URLField(blank=True, null=True)
def __unicode__(self):
return self.name
class Segment(Model):
position_in_expr = PositiveIntegerField()
expression = ForeignKey('Expression', related_name='segments')
orth = CharField(max_length=32)
base = CharField(max_length=32)
ctag = CharField(max_length=8)
msd = CharField(max_length=64)
is_head = BooleanField(default=False)
def __unicode__(self):
return self.orth
class Category(Model):
key = CharField(max_length=16, primary_key=True)
name = CharField(max_length=32)
description = TextField(blank=True, null=True)
def __unicode__(self):
return self.name