From 9cc75e4c43555c9ffce32daddb1ca426f3349d7c Mon Sep 17 00:00:00 2001
From: janek37 <none@none>
Date: Wed, 30 Sep 2015 15:57:42 +0200
Subject: [PATCH] poprawione proponowanie automatycznych derywatów (adjnie)

---
 dictionary/ajax_lexeme_view.py      | 19 ++++++++++---------
 dictionary/auto_derivatives.py      | 63 ++++++++++++++++++++++++++++++++++++++-------------------------
 dictionary/static/js/lexeme-edit.js |  4 ++--
 3 files changed, 50 insertions(+), 36 deletions(-)

diff --git a/dictionary/ajax_lexeme_view.py b/dictionary/ajax_lexeme_view.py
index a588e58..c3d8a47 100644
--- a/dictionary/ajax_lexeme_view.py
+++ b/dictionary/ajax_lexeme_view.py
@@ -418,11 +418,11 @@ def update_lexeme(request, form_data):
     if new_lexeme:
         insert_new_lexeme(l, request)
     computed_derivatives = lexeme_derivatives(l)
-    crs = l.refs_to.values_list(
-        'to_lexeme__entry', 'to_lexeme__part_of_speech_id')
+    crs = l.refs_to.values_list('to_lexeme__entry', 'type__symbol')
     cr_set = set(tuple(cr) for cr in crs)
     needed_derivatives = [
-        d for d in computed_derivatives if (d['entry'], d['pos']) not in cr_set]
+        d for d in computed_derivatives
+        if (d['entry'], d['cr_type']) not in cr_set]
     return {'derivatives': needed_derivatives}
 
 
@@ -434,20 +434,21 @@ def create_derivatives(request, lexeme_id, chosen_derivatives):
     new_derivatives = {}
     for der_data in chosen_derivatives:
         pos = der_data['pos']
+        cr_type = der_data['cr_type']
         entry = der_data['entry']
         if entry == '':
             raise AjaxError(_(u'Nie wpisano hasła'))
         new_lexemes = create_derivative(
-            lexeme, pos, entry, pl=der_data.get('pl'),
+            lexeme, pos, cr_type, entry, pl=der_data.get('pl'),
             index=der_data['index'])
         for der in new_lexemes:
             insert_new_lexeme(der, request)
-        new_derivatives[pos] = new_lexemes
-    chosen_poses = [der_data['pos'] for der_data in chosen_derivatives]
+        new_derivatives[cr_type] = new_lexemes
+    chosen_types = [der_data['cr_type'] for der_data in chosen_derivatives]
     if lexeme.part_of_speech_id == 'adj':
-        if 'adv' in chosen_poses and 'advcom' in chosen_poses:
-            adv = new_derivatives['adv'][0]
-            advcom = new_derivatives['advcom'][0]
+        if 'adjadv' in chosen_types and 'adjadvc' in chosen_types:
+            adv = new_derivatives['adjadv'][0]
+            advcom = new_derivatives['adjadvc'][0]
             adv.add_cross_reference(advcom, 'advcom')
             advcom.add_cross_reference(adv, 'comadv')
     return {}
diff --git a/dictionary/auto_derivatives.py b/dictionary/auto_derivatives.py
index 987d4d2..f413a46 100644
--- a/dictionary/auto_derivatives.py
+++ b/dictionary/auto_derivatives.py
@@ -1,10 +1,12 @@
 # -*- coding: utf-8 -*-
 from django.db.models import Max
 from dictionary.models import Ending, Lexeme, LexemeInflectionPattern, \
-    Pattern, Gender, LexemeAttributeValue
+    Pattern, Gender, LexemeAttributeValue, LexemeAttribute
 
 VOWELS = u'aeiouyąęó'
 
+ADJ_POS = ('ppas', 'appas', 'pact', 'adjcom', 'nieadj')
+
 Ppact = Pattern.objects.get(name='P2')  # hardcoded pattern
 Pppas_ty = Pattern.objects.get(name='P4t')
 Pppas_ny_ni = Pattern.objects.get(name='P4')
@@ -17,23 +19,25 @@ Pcom = Pattern.objects.get(name='P4zs')
 Pndm = Pattern.objects.get(name='ndm')
 n2 = Gender.objects.get(symbol='n2')
 f = Gender.objects.get(symbol='f')
+ATTR_POPRZ = LexemeAttribute.objects.get(name=u'forma poprz.')
+ATTR_ZLOZ = LexemeAttribute.objects.get(name=u'forma złoż.')
 NO_POPRZ = LexemeAttributeValue.objects.get(
     value=u'nieobecna', attribute__name=u'forma poprz.')
 NO_ZLOZ = LexemeAttributeValue.objects.get(
     value=u'nieobecna', attribute__name=u'forma złoż.')
 
 CR_TYPES = {
-    'pact': ('verpact', 'pactver'),
-    'ppas': ('verppas', 'ppasver'),
-    'appas': ('verppas', 'ppasver'),
-    'ger': ('verger', 'gerver'),
-    'osc': ('adjosc', 'oscadj'),
-    'adv': ('adjadv', 'advadj'),
-    'advcom': ('adjadvc', 'advcadj'),
+    'verpact': ('verpact', 'pactver'),
+    'verppas': ('verppas', 'ppasver'),
+    # 'verappas': ('verppas', 'ppasver'),
+    'verger': ('verger', 'gerver'),
+    'adjosc': ('adjosc', 'oscadj'),
+    'adjadv': ('adjadv', 'advadj'),
+    'adjadvc': ('adjadvc', 'advcadj'),
     'adjcom': ('adjcom', 'comadj'),
-    'nieadj': ('adjnie', 'nieadj'),
-    'nieadv': ('adjnie', 'nieadj'),
-    'nieosc': ('adjnie', 'nieadj'),
+    'adjnie': ('adjnie', 'nieadj'),
+    'advnie': ('adjnie', 'nieadj'),
+    'oscnie': ('adjnie', 'nieadj'),
 }
 
 
@@ -48,6 +52,7 @@ def ppas_data(lips, pos='ppas'):
             for ending12 in endings12:
                 yield {
                     'pos': pos,
+                    'cr_type': 'verppas',
                     'entry': lip.root + ending.string + 'y',
                     'pl': lip.root + ending12.string,
                     'index': lip.index,
@@ -62,6 +67,7 @@ def pact_data(lips):
         for ending in endings3:
             yield {
                 'pos': 'pact',
+                'cr_type': 'verpact',
                 'entry': lip.root + ending.string + 'cy',
                 'index': lip.index,
             }
@@ -75,6 +81,7 @@ def ger_data(lips):
         for ending in endings11:
             yield {
                 'pos': 'ger',
+                'cr_type': 'verger',
                 'entry': lip.root + ending.string + 'ie',
                 'index': lip.index,
             }
@@ -131,33 +138,37 @@ def lexeme_derivatives(lexeme):
                 yield data
     elif lexeme.part_of_speech.symbol == 'adj':
         # adjcom, adv, advcom, osc, nieadj
-        for pos in ('adjcom', 'adv', 'advcom'):
+        pos_types = (
+            ('adjcom', 'adjcom'),
+            ('adv', 'adjadv'),
+            ('advcom', 'adjadvc')
+        )
+        for pos, cr_type in pos_types:
             yield {
                 'pos': pos,
+                'cr_type': cr_type,
                 'entry': None,
                 'index': 1,
             }
         yield {
             'pos': 'osc',
+            'cr_type': 'adjosc',
             'entry': guess_osc(lexeme.entry),
             'index': 1,
         }
         yield {
-            'pos': 'nieadj',
+            'pos': 'adj',
+            'cr_type': 'adjnie',
             'entry': make_negation(lexeme.entry),
             'index': 1,
         }
 
 
-def create_derivative(lexeme, part_of_speech, entry, index, pl=None):
-    negation = part_of_speech.startswith('nie')
-    if negation:
-        pos = part_of_speech[3:]
-    else:
-        pos = part_of_speech
+def create_derivative(lexeme, part_of_speech, cr_type, entry, index, pl=None):
+    negation = cr_type.endswith('nie')
     next_id = Lexeme.all_objects.aggregate(Max('id'))['id__max'] + 1
     der = Lexeme.objects.create(
-        id=next_id, entry=entry, part_of_speech_id=pos,
+        id=next_id, entry=entry, part_of_speech_id=part_of_speech,
         status=lexeme.status, owner_vocabulary_id=lexeme.owner_vocabulary_id,
         specialist=lexeme.specialist,
         borrowing_source_id=lexeme.borrowing_source_id)
@@ -211,18 +222,20 @@ def create_derivative(lexeme, part_of_speech, entry, index, pl=None):
             for q in orig_lip.qualifiers.all():
                 lip.qualifiers.add(q)
     for attr, attr_val in lexeme.attributes_values():
-        if attr_val and attr.parts_of_speech.filter(symbol=part_of_speech):
+        if attr not in (ATTR_POPRZ, ATTR_ZLOZ) and attr_val \
+                and attr.parts_of_speech.filter(symbol=part_of_speech):
             attr_val.add_lexeme(der)
     if part_of_speech in ('ppas', 'appas', 'pact', 'adjcom', 'nieadj'):
         NO_POPRZ.add_lexeme(der)
         NO_ZLOZ.add_lexeme(der)
     for q in lexeme.qualifiers.all():
         der.qualifiers.add(q)
-    cr_to, cr_from = CR_TYPES[part_of_speech]
+    cr_to, cr_from = CR_TYPES[cr_type]
     lexeme.add_cross_reference(der, cr_to)
     der.add_cross_reference(lexeme, cr_from)
     new_lexemes = [der]
-    if part_of_speech in ('osc', 'adv'):
+    if cr_type in ('adjosc', 'adjadv'):
         new_lexemes.extend(create_derivative(
-            der, 'nie' + part_of_speech, make_negation(entry), index))
-    return new_lexemes
\ No newline at end of file
+            der, part_of_speech, part_of_speech + 'nie', make_negation(entry),
+            index))
+    return new_lexemes
diff --git a/dictionary/static/js/lexeme-edit.js b/dictionary/static/js/lexeme-edit.js
index 83ad7ae..4969acb 100644
--- a/dictionary/static/js/lexeme-edit.js
+++ b/dictionary/static/js/lexeme-edit.js
@@ -432,11 +432,11 @@ $.extend(edit, {
                         var label = $('<label/>').attr('for', name);
                         li.append(input).append(label).appendTo(ul);
                         if (der_data.entry) {
-                            label.text(der_data.pos + ', ' + der_data.entry);
+                            label.text(der_data.cr_type + ', ' + der_data.entry);
                         } else {
                             var der_input = $('<input/>').attr('type', 'text')
                                 .addClass('derivative-input');
-                            label.text(der_data.pos + ', ');
+                            label.text(der_data.cr_type + ', ');
                             li.append(der_input);
                         }
                     });
--
libgit2 0.22.2