0004_auto_20151210_2115.py
2.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from accounts.util import bot_history
def adjsubst_crs(apps, schema_editor):
bot_history()
Lexeme = apps.get_model('dictionary', 'Lexeme')
CrossReferenceType = apps.get_model('dictionary', 'CrossReferenceType')
CrossReference = apps.get_model('dictionary', 'CrossReference')
LexemeAttribute = apps.get_model('dictionary', 'LexemeAttribute')
LexemeAttributeValue = apps.get_model('dictionary', 'LexemeAttributeValue')
LexemeAV = apps.get_model('dictionary', 'LexemeAV')
adjsubst, created = CrossReferenceType.objects.get_or_create(
symbol='adjsubst', desc=u'od rzeczownika', index=30,
from_pos_id='adj', to_pos_id='subst')
substadj, created = CrossReferenceType.objects.get_or_create(
symbol='substadj', desc=u'przymiotnik', index=30,
from_pos_id='subst', to_pos_id='adj')
rzeczownik = LexemeAttribute.objects.get(name='rzeczownik')
nouns = LexemeAttributeValue.objects.filter(attribute=rzeczownik)
empty_value = nouns.get(value=u'')
def make_references(adj, subst):
CrossReference.objects.get_or_create(
from_lexeme=adj, to_lexeme=subst, type=adjsubst)
CrossReference.objects.get_or_create(
from_lexeme=subst, to_lexeme=adj, type=substadj)
for noun in nouns.exclude(value__contains=' '):
matching = Lexeme.objects.filter(
entry=noun.value, part_of_speech='subst')
if len(matching) == 1:
match = matching.get()
for l in noun.lexemes.all():
make_references(l, match)
LexemeAV.objects.filter(attribute_value=noun).update(
attribute_value=empty_value)
for multinoun in nouns.filter(value__regex='^\w+(; \w+)+$'):
bad_entries = []
entries = multinoun.value.split('; ')
for entry in entries:
matching = Lexeme.objects.filter(
entry=entry, part_of_speech='subst')
if len(matching) == 1:
match = matching.get()
for l in multinoun.lexemes.all():
make_references(l, match)
else:
bad_entries.append(entry)
if bad_entries != entries:
new_value = '; '.join(bad_entries)
val, created = LexemeAttributeValue.objects.get_or_create(
value=new_value, attribute=rzeczownik)
LexemeAV.objects.filter(attribute_value=multinoun).update(
attribute_value=val)
class Migration(migrations.Migration):
dependencies = [
('dictionary', '0003_auto_20151202_1539'),
]
operations = [
migrations.RunPython(adjsubst_crs),
]