models.py
8.19 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
from django.db.models import Model, CharField, IntegerField, ForeignKey, \
ManyToManyField, TextField, BooleanField, Manager
from django.utils.translation import ugettext_lazy as _
from common.util import GroupDict
from common.models import NotDeletedManager
class InflectionType(Model):
symbol = CharField(primary_key=True, max_length=16, db_column='czm')
color_scheme = IntegerField()
full_name = CharField(max_length=128)
def __unicode__(self):
return self.symbol
class Meta:
db_table = 'czescimowy'
class BaseFormLabel(Model):
symbol = CharField(max_length=32, blank=True, db_column='efobaz')
inflection_type = ForeignKey(InflectionType)
index = IntegerField()
def __unicode__(self):
return '%s/%s' % (self.symbol, self.inflection_type.symbol)
class Meta:
db_table = 'efobazy'
unique_together = ['symbol', 'inflection_type']
ordering = ['index']
class DummyEnding(object):
def __init__(self, string):
self.index = 0
self.string = string
from dictionary.models import Qualifier
self.qualifiers = Qualifier.objects.none()
class PatternType(Model):
inflection_type = ForeignKey(
InflectionType, db_column='czm', verbose_name=_(u'inflection type'))
# typ wzoru (np. dla rzeczowników: odmiana męska, żeńska lub nijaka)
symbol = CharField(
max_length=32, blank=True, db_column='wtyp',
verbose_name=_(u'pattern type'))
base_form_labels = ManyToManyField(BaseFormLabel)
def dummy_base_endings(self):
bfl_dict = dict(
(bfl, [DummyEnding(bfl.symbol or self.inflection_type_id)])
for bfl in self.base_form_labels.all())
return bfl_dict
@classmethod
def options(cls):
pattern_types = GroupDict()
for pt in cls.objects.all():
pattern_types.add(pt.inflection_type_id, (pt.id, unicode(pt)))
return sorted(pattern_types.items())
def __unicode__(self):
return self.symbol.replace('"', "''") or \
'[%s]' % self.inflection_type_id
# '%s (%s)' % (self.symbol, self.inflection_type_id)
class Meta:
db_table = 'typywzorow'
ordering = ['symbol']
class Pattern(Model):
STATUS_NEW = 'nowy'
STATUS_CONFIRMED = 'conf'
STATUS_CANDIDATE = 'cand'
STATUS_CHOICES = (
(STATUS_NEW, _(u'new')),
(STATUS_CONFIRMED, _(u'confirmed')),
(STATUS_CANDIDATE, _(u'candidate')),
)
HIDDEN_STATUSES = (STATUS_CANDIDATE, STATUS_NEW)
name = CharField(
max_length=32, unique=True, db_column='w_id', verbose_name=_(u'name'))
old_name = CharField(
max_length=32, verbose_name=_(u'old name'), null=True)
type = ForeignKey(
PatternType, db_column='typ', verbose_name=_(u'type'))
# rdzeń przykładowej formy hasłowej
example = CharField(
max_length=64, db_column='przyklad', verbose_name=_(u'example'),
blank=True)
basic_form_ending = CharField(
max_length=32, db_column='zakp', blank=True,
verbose_name=_(u'basic form ending'))
status = CharField(
max_length=8, choices=STATUS_CHOICES, verbose_name=_(u'status'))
comment = TextField(
blank=True, db_column='komentarz', verbose_name=_(u'comment'))
deleted = BooleanField(default=False)
objects = NotDeletedManager()
all_objects = Manager()
def ending_set(self, subroot='', tag_prefix=None):
endings = self.endings
if tag_prefix:
endings = endings.filter(
base_form_label__symbol__startswith=tag_prefix)
return set(subroot + e
for e in endings.values_list('string', flat=True))
def base_endings(self, label_filter=None):
bfls = self.type.base_form_labels.all()
endings = Ending.objects.filter(
base_form_label__patterntype=self.type, pattern=self) \
.select_related('base_form_label').prefetch_related('qualifiers')
if label_filter is not None:
endings = endings.filter(
base_form_label__symbol__regex=label_filter)
bfl_dict = GroupDict((bfl, []) for bfl in bfls)
for ending in endings:
bfl_dict.add(ending.base_form_label, ending)
return bfl_dict
def create_example(self, gender):
from dictionary.models import Lexeme
lexemes = Lexeme.objects.filter(
patterns=self, inflection__gender=gender)
reader_lexemes = Lexeme.filter_reader(lexemes)
if reader_lexemes:
lexeme = reader_lexemes[0]
PatternExample.objects.create(
lexeme=lexeme, pattern=self, gender=gender)
def get_example(self, gender, refresh=False):
examples = PatternExample.objects.filter(pattern=self, gender=gender)
if not examples or refresh:
examples.delete()
self.create_example(gender)
examples = PatternExample.objects.filter(
pattern=self, gender=gender)
if examples:
lexeme = examples.get().lexeme
example_inflections = lexeme.inflection_set.filter(
pattern=self, gender=gender)
if example_inflections:
return lexeme, example_inflections[0].root
else:
return self.get_example(gender, refresh=True)
def is_public(self):
return self.status not in Pattern.HIDDEN_STATUSES
def get_root(self, basic_form, gender, use_pattern_ending=False):
ends = []
basic_endings = []
if gender:
bfl = gender.basic_form_label
basic_endings = self.endings.filter(base_form_label=bfl)
elif self.type.inflection_type_id == 'v':
basic_endings = self.endings.filter(base_form_label__symbol='5')
elif self.type.inflection_type_id == 'num':
basic_endings = self.endings.filter(
base_form_label__symbol__in=('1', '1,z1'))
else:
use_pattern_ending = True
if use_pattern_ending:
ends.append(self.basic_form_ending)
else:
ends += [e.string for e in basic_endings]
good_ends = [end for end in ends if basic_form.endswith(end)]
assert len(set(good_ends)) <= 1 # inaczej rdzeń nie jest jednoznaczny
if good_ends:
return basic_form[:len(basic_form) - len(good_ends[0])]
else:
if not use_pattern_ending:
return self.get_root(
basic_form, gender, use_pattern_ending=True)
else:
return None
def __unicode__(self):
return self.name
@staticmethod
def filter_reader(patterns):
return patterns.exclude(status__in=Pattern.HIDDEN_STATUSES)
class Meta:
db_table = 'wzory'
ordering = ['name']
permissions = (
('view_pattern', _(u'Can view patterns')),
)
class PatternNotDeletedManager(Manager):
use_for_related_field = True
def get_queryset(self):
return super(PatternNotDeletedManager, self).get_queryset().filter(
pattern__deleted=False)
# zakonczenie formy bazowej
class Ending(Model):
pattern = ForeignKey(Pattern, related_name='endings', db_column='w_id')
# etykieta (tag) formy bazowej
base_form_label = ForeignKey(BaseFormLabel, db_column='efobaz')
# kolejnosc dla zakonczen o tym samym base_form_label
index = IntegerField(db_column='zind')
string = CharField(max_length=16, db_column='zak', blank=True)
qualifiers = ManyToManyField(
'dictionary.Qualifier', blank=True, db_table='kwalifikatory_zakonczen')
objects = PatternNotDeletedManager()
all_objects = Manager()
def __unicode__(self):
return '%s : %s : %s' % (
self.pattern.name, self.string, self.base_form_label)
class Meta:
db_table = 'zakonczenia'
unique_together = ('pattern', 'base_form_label', 'index')
ordering = ['base_form_label__index', 'index']
class PatternExample(Model):
pattern = ForeignKey(Pattern)
gender = ForeignKey('dictionary.Gender')
lexeme = ForeignKey('dictionary.Lexeme')
class Meta:
unique_together = ('pattern', 'gender')
ordering = ['gender']