models.py
6.96 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
# -*- 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')
lexical_class = ForeignKey(InflectionType)
index = IntegerField()
def __unicode__(self):
return '%s/%s' % (self.symbol, self.lexical_class.symbol)
class Meta:
db_table = 'efobazy'
unique_together = ['symbol', 'lexical_class']
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):
lexical_class = 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.lexical_class_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.lexical_class.symbol, (pt.id, unicode(pt)))
return sorted(pattern_types.items())
def __unicode__(self):
return self.symbol.replace('"', "''") or '[%s]' % self.lexical_class_id
# '%s (%s)' % (self.symbol, self.lexical_class_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()
from dictionary.models import Ending
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, PatternExample
lexemes = Lexeme.objects.filter(
patterns=self,
lexemeinflectionpattern__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):
from dictionary.models import PatternExample
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_lips = lexeme.lexemeinflectionpattern_set.filter(
pattern=self, gender=gender)
if example_lips:
return lexeme, example_lips[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.lexical_class.symbol == 'v':
basic_endings = self.endings.filter(base_form_label__symbol='5')
elif self.type.lexical_class.symbol == '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
class Meta:
db_table = 'wzory'
ordering = ['name']
permissions = (
('view_pattern', _(u'Can view patterns')),
)
def reader_patterns(patterns):
return patterns.exclude(status__in=Pattern.HIDDEN_STATUSES)