views.py 36.4 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
# -*- coding: utf-8 -*-

import locale
from semantics.models import SemanticRole, SemanticFrame, Complement, \
                             LexicalUnit, FrameRankings, SemanticRolesDisplay, \
                             LexicalUnitExamples, FramePosition, SelectivePreference, \
                             RelationalSelectivePreference, SelectivePreferenceRelations, \
                             GeneralSelectivePreference
from dictionary.models import Frame_Char_Model, Lemma, Lemma_Status, \
                              sort_arguments, sort_positions, NKJP_Example, \
                              Frame, Position, Argument
from wordnet.models import Hypernymy, Synonymy, Synset
from dictionary.ajax_lemma_view import user_can_modify
from django.core.exceptions import SuspiciousOperation
from django.core.urlresolvers import reverse
from django.db.models import Q
from datetime import datetime
from django.db.models import Min

from common.decorators import render, ajax, AjaxError
from semantics.change_log import store_old_versions
from semantics.validation import validate_schemas, validate_frames, validate_lexical_units


####################################### render #####################################

@render('semantics.html')
@ajax(method='get', encode_result=False)
def ajax_semantics(request, id):
    context = {}
    lemma = Lemma.objects.get(id=id)
    context['lemma'] = lemma
    context['can_modify'] = (user_can_modify(lemma, request.user) and 
                             lemma.status.stage_of_work.sym_name == 'semantics')
    context['js_vars'] = {
        'ajax_frames': reverse('ajax_frames'),
        'ajax_schemas': reverse('ajax_schemas'),
        'ajax_units': reverse('ajax_units'),
        'ajax_roles': reverse('ajax_roles'),
        'ajax_create_frame': reverse('ajax_create_frame'),
        'ajax_create_complement': reverse('ajax_create_complement'),
        'ajax_examples': reverse('ajax_examples'),
        'ajax_update_meanings': reverse('ajax_update_meanings'),
        'ajax_modify_frames': reverse('ajax_modify_frames'),
        'ajax_synsets': reverse('ajax_synsets'),
        'ajax_relations': reverse('ajax_relations'),
        'ajax_predefined_preferences': reverse('ajax_predefined_preferences'),
        'ajax_plWN_context_lookup': reverse('ajax_plWN_context_lookup'),
    }
    return context

def reorder_history(frames_list):
    next_ids = set()
    for frame in frames_list:
        if frame.next is not None:
            next_ids.add(frame.next.id)
    
    first = set()
    for frame in frames_list:
        if frame.id not in next_ids:
            first.add(frame)
            
    result = []
    for frame in first:
        current = frame
        while current is not None:
            result.append(current)
            current = current.next
            
    result.reverse()
    return result


@render('frames.json')
@ajax(method='get', encode_result=False)
def ajax_frames(request, lemma_id):

    lemma = Lemma.objects.get(id=lemma_id, old=False)
    lexical_units = LexicalUnit.objects.filter(Q(base = lemma.entry)|Q(base = lemma.entry + u' się')).order_by('sense')
    
    alternations = {}        
    frames_dict = {}
    frame_units = {}
    for lexical_unit in lexical_units:
        frames = lexical_unit.actual_frames()
        for frame in frames:
            alternations[frame.id] = {}
            frames_dict[frame.id] = frame
            if frame.id not in frame_units:
                frame_units[frame.id] = []
            frame_units[frame.id].append(lexical_unit)
            
    type_frames = {}
    for frame_id in frame_units:
        t = tuple(frame_units[frame_id])
        if t not in type_frames:
            type_frames[t] = []
        type_frames[tuple(frame_units[frame_id])].append(frames_dict[frame_id])
    
        
    frames_display = []
    complement_arguments = {}
    arguments_frame_connected = {}
    
    for t in type_frames:
        type_frames[t] = reorder_history(type_frames[t])
    
    for t in type_frames:
        frame_display = {"lexical_units": [], "frames": []}
        for lu in list(t):
            frame_display["lexical_units"].append({"id": str(lu.id), "base": lu.base, "sense": str(lu.sense)})
        for frame in type_frames[t]:
            # frame_complements = Complement.objects.filter(frame=frame)
            frame_complements = frame.complements.all()
        
            # classes and first frame/position part of identifiers
            frame_ids = [u'frame_' + str(frame.id) + '_comp_' + str(complement.id) + '_' for complement in frame_complements]

            # names of roles
            frame_roles = [{'csv_id': i, 'csv_class': c, 'argument': a} for i, c, a in zip(frame_ids, frame_ids, [[role.id for role in complement.roles.all()] for complement in frame_complements])]
            
            # rowspan for 'Argumenty:'
            # frame_preferences_rowspan = max(max([len(preference) for preference in [complement.selective_preference.all() for complement in frame_complements]] + [0]), 1)
            frame_preferences_rowspan = max(max([len(complement.selective_preference.generals.all()) + len(complement.selective_preference.synsets.all()) + len(complement.selective_preference.relations.all()) for complement in frame_complements if complement.selective_preference is not None] +[0]), 1)
            
            frame_preferences = []
            for i in range(frame_preferences_rowspan):
                row = []
                idents = []
                for frame_id, complement in zip(frame_ids, frame_complements):
                    if complement.selective_preference is not None:
                        generals = complement.selective_preference.generals.all()
                        synsets = complement.selective_preference.synsets.all()
                        relations = complement.selective_preference.relations.all()
                        synset_relations = complement.selective_preference.synset_relations.all()
                        generals_len = len(generals)
                        synsets_len = len(synsets)
                        relations_len = len(relations)
                        synset_relations_len = len(synset_relations)
                        if generals_len > i:
                            general = generals[i]
                            row.append(general.name)
                            idents.append(frame_id + 'pref_g' + str(general.id) + '_')
                        elif generals_len + synsets_len > i:
                            synset = synsets[i - generals_len]
                            # lexical_units_preference = LexicalUnit.objects.filter(synset=synset)
                            row.append(unicode(synset))
                            idents.append(frame_id + 'pref_s' + str(synset.id) + '_')
                        elif generals_len + synsets_len + relations_len > i:
                            relation = relations[i - generals_len - synsets_len]
                            row.append(relation.relation.name + '<br /> -> [' + ', '.join([r.role for r in relation.to.roles.all()]) + ']')
                            idents.append(frame_id + 'pref_r' + str(relation.id) + '_')
                        elif generals_len + synsets_len + relations_len + synset_relations_len > i:
                            relation = relations[i - generals_len - synsets_len - relations_len]
                            synset = relation.to
                            row.append(relation.relation.name + '<br /> -> [' + unicode(synset) + ']')
                            idents.append(frame_id + 'pref_R' + str(relation.id) + '_')
                        else:
                            row.append('')
                            idents.append(frame_id + 'pref_-' + str(i + 1) + '_')
                    else:
                        row.append('')
                        idents.append(frame_id + 'pref_-' + str(i + 1) + '_')
                frame_preferences.append([{'csv_id': i, 'csv_class': c, 'preference': p} for i, c, p in zip(idents, frame_ids, row)])
            
            display = {"roles": frame_roles, "preferences": frame_preferences}
            frame_display["frames"].append({"frame_id": str(frame.id), "colspan": str(max(len(frame_roles), 1)), "rowspan": str(frame_preferences_rowspan), "status": u'pewna', "display": display})
            
            for complement, complement_class in zip(frame_complements, frame_ids):
                if complement_class not in complement_arguments:
                    complement_arguments[complement_class] = []
                
                for schema_position in complement.realizations.all():
                    schema = schema_position.frame
                    position = schema_position.position
                    argument = schema_position.argument
                    alternation = schema_position.alternation
                    realization_id = u'schema_' + str(schema.id) + u'_pos_' + str(position.id) + '_arg_' + str(argument.id) + '_' + 'alt_' + str(alternation) + '_'
                    complement_arguments[complement_class].append(realization_id)
                    if realization_id not in arguments_frame_connected:
                        arguments_frame_connected[realization_id] = []
                    arguments_frame_connected[realization_id].append('frame_' + str(frame.id) + '_')
                    if schema.id in alternations[frame.id]:
                        alternations[frame.id][schema.id] = max(alternations[frame.id][schema.id], alternation)
                    else:
                        alternations[frame.id][schema.id] = alternation
                    # alternations[frame.id] = {}
        

        frames_display.append(frame_display)

    # ala["ma"] = "kot"

    context = {
               'frames_display': frames_display, 
               'connections': {'connected': complement_arguments, 'connected_reverse': arguments_frame_connected},
               'frames_count': lemma.entry_obj.actual_frames().count(),
               'alternations': alternations
              }

    return context

@render('units.json')
@ajax(method='get', encode_result=False)   
def ajax_units(request, lemma_id):
    lemma = Lemma.objects.get(id=lemma_id, old=False)
    lexical_units = LexicalUnit.objects.filter(Q(base = lemma.entry, pos="czasownik")|Q(base = lemma.entry + u' się', pos="czasownik")).order_by('base', 'sense')

    context = {
               'lexical_units': [{"id": lu.id, "luid": lu.luid, "base": lu.base, "sense": lu.sense, "pos": lu.pos, "glossa": lu.glossa, "definition": lu.definition} for lu in lexical_units],
               'informations': {'base': lemma.entry, 'sense': max(['A'] + [chr(ord(lu.sense) + 1) for lu in lexical_units.filter(luid=-1)])}, # TODO: 2 different free senses for with/whthout 'się'
              }

    return context

@ajax(method='get')   
def ajax_predefined_preferences(request):
    predefined = []
    for preference in GeneralSelectivePreference.objects.order_by('name'):
        if preference.members:
            members = [member.name for member in preference.members.generals.order_by('name')]
            members.extend([unicode(synset) for synset in preference.members.synsets.all()])
            content = u'%s: (%s)' % (preference.name, ', '.join(members))
        else:
            content = u'%s' % (preference.name)
        predefined.append({"id": preference.id, "content": content})

    context = {
               'predefined': predefined,
              }

    return context

@render('synsets.json')
@ajax(method='get', encode_result=False)   
def ajax_synsets(request, base, pos):
    
    if pos == '_':
        lexical_units = LexicalUnit.objects.filter(base=base).order_by('pos', 'base', 'sense')
    else:
        lexical_units = LexicalUnit.objects.filter(base=base, pos=pos).order_by('pos', 'base', 'sense')
    
    synsets = []
    for representative in lexical_units:
        synset = [{"id": lu.id, "luid": lu.luid, "base": lu.base, "sense": lu.sense, "pos": lu.pos, "glossa": lu.definition} for lu in LexicalUnit.objects.filter(synset=representative.synset)]
        synsets.append({"id": representative.synset.id, "content": synset})

    context = {
               'synsets': synsets,
              }

    return context

@render('relations.json')
@ajax(method='get', encode_result=False)   
def ajax_relations(request):
    
    relations = [{"id": relation.id, "content": relation.name} for relation in SelectivePreferenceRelations.objects.all()]

    context = {
               'relations': relations,
              }

    return context

@render('roles.json')  
@ajax(method='get', encode_result=False)
def ajax_roles(request):       
    roles_display = []
    maxrow = SemanticRolesDisplay.objects.all().order_by('-row')[0].row
    for i in range(1, maxrow + 1):
        row = []
        cells = SemanticRolesDisplay.objects.filter(row=i).order_by('column')
        for cell in cells:
            content = cell.roles.all().order_by('id')
            commas = [','] * max(len(content) - 1, 0) + ['']
            row.append((cell.caption, cell.rowspan, cell.colspan, zip(content, commas)))
        commas = [','] * max(len(row) - 1, 0) + ['']
        captions, rowspans, colspans, contents = zip(*row)
        roles_display.append(zip(list(captions), list(rowspans), list(colspans), list(contents), commas))
    commas = [','] * max(len(roles_display) - 1, 0) + ['']

    context = {
               'roles_display': zip(roles_display, commas),
              }

    return context


@render('schemas.json')   
@ajax(method='get', encode_result=False)
def ajax_schemas(request, lemma_id):

    lemma = Lemma.objects.get(id=lemma_id, old=False)
    
    schemas_all = lemma.frames.all()
    
    characteristics = {}
    characteristics_ids = []
    schemas_by_characteristic = {}
    for schema in schemas_all:
        chars = schema.characteristics.all()
        char_types = [c.type for c in chars]
        l = [(Frame_Char_Model.objects.get(model_name=c.type).priority, c) for c in chars]
        l.sort()
        _, chars = zip(*l)
        chars = list(chars)
        characteristic_id = tuple([char.id for char in chars])
        if characteristic_id not in characteristics:
            characteristics_ids.append(characteristic_id)
            values = [char.value.value for char in chars]
            if values[0]:
                characteristics[characteristic_id] = "%s (%s)" % (values[0], ','.join(values[1:]))
            else:
                characteristics[characteristic_id] = "(%s)" % (','.join(values[1:]))
            schemas_by_characteristic[characteristic_id] = []
        schemas_by_characteristic[characteristic_id].append(schema)
    
    lexical_units = LexicalUnit.objects.filter(Q(base=lemma.entry) | Q(base=lemma.entry+u' się'))

    schemas_display = []
    schema_unit_rank = {}
    for characteristic_id in characteristics_ids:
        schema_display = {"characteristic_id": '_'.join([str(i) for i in characteristic_id]), "characteristic_display": characteristics[characteristic_id], "schemas": []}
        schemas = schemas_by_characteristic[characteristic_id]
        
        for schema in schemas: # row by row display for schema tables
            ordered_positions = sort_positions(schema.positions.all())
            # classes and first frame/position part of identifiers
            schema_ids = [u'schema_' + str(schema.id) + '_pos_' + str(position.id) + '_' for position in ordered_positions]

            # names of categories (along with class names
            schema_categories = zip(schema_ids, [','.join([category.category for category in position.ordered_categories()]) for position in ordered_positions])
            
            # value of rowspan for 'Argumenty:' header
            schema_arguments_rowspan = max(max([position.arguments.count() for position in ordered_positions] + [0]), 1)
            
            display = {"categories": [{"csv_id": c, "csv_class": c, "argument": a} for c, a in schema_categories], "arguments": []}
            for i in range(schema_arguments_rowspan):
            
                row = []
                idents = []
                for schema_id, position in zip(schema_ids, ordered_positions):
                    if position.arguments.count() > i:
                        ordered_arguments = sort_arguments(position.arguments.all())
                        row.append(unicode(ordered_arguments[i]))
                        idents.append(schema_id + 'arg_' + str(ordered_arguments[i].id) + '_')
                    else: # this category has fewer posible argument realizations
                        row.append('')
                        idents.append(schema_id + 'arg_-' + str(i + 1) + '_')
                # identifier, class, argument
                display["arguments"].append([{"csv_id": i, "csv_class": c, "argument": a} for i, c, a in zip(idents, schema_ids, row)])
            
            schema_display["schemas"].append({"schema_id": str(schema.id), "grade": lemma.get_schema_opinion(schema), "colspan": str(max(len(schema_categories), 1)), "rowspan": str(schema_arguments_rowspan), "display": display, "phraseologic": schema.phraseologic})
            
            ranks = FrameRankings.objects.filter(frame=schema)
            for rank in ranks:
                if rank.lexical_unit.id not in schema_unit_rank:
                    schema_unit_rank[rank.lexical_unit.id] = {}
                schema_unit_rank[rank.lexical_unit.id]["schema_" + str(schema.id) + "_"] = str(rank.rank)
        
        schemas_display.append(schema_display)
    
    
    context = {
               'lemma': lemma, 
               'schemas_display': schemas_display,
               'ranks': schema_unit_rank
              }

    return context
  
@render('examples.json')   
@ajax(method='get', encode_result=False)
def ajax_examples(request, lemma_id):

    lemma = Lemma.objects.get(id=lemma_id, old=False)
    nkjp_examples = lemma.nkjp_examples.all()
    lexical_units = LexicalUnit.objects.filter(Q(base = lemma.entry)|Q(base = lemma.entry + u' się'))
    
    lexical_units_ids = [lu.id for lu in lexical_units]
    
    examples_linked = []
    
    for nkjp_example in nkjp_examples:
        
        linked = LexicalUnitExamples.objects.filter(example=nkjp_example)
        meanings = []
        
        for link in linked:
            if link.lexical_unit.id in lexical_units_ids:
                meanings.append(link.lexical_unit.id)
        
        all_argselection = nkjp_example.arguments.all()
        arguments = []
        for argselection in all_argselection:
            pos = argselection.position.id
            args = argselection.arguments.all()
            for arg in args:
                arguments.append({'id': 'schema_' + str(nkjp_example.frame.id) + '_pos_' + str(pos) + '_arg_' + str(arg.id) + '_'})
        
        examples_linked.append({'schema': 'schema_' + str(nkjp_example.frame.id) + '_', 'arguments': arguments, 'id': nkjp_example.id, 'sentence': nkjp_example.sentence, 'source': nkjp_example.source.source, 'opinion': nkjp_example.opinion.opinion, 'meanings': meanings})
    
    
    context = {
                'examples': examples_linked
              }

    return context

  
@ajax(method='get', encode_result=False)  
def ajax_create_frame(request, lexical_units):
    #lexical_unit_id_list = request.GET.get('lexical_units', '')
    lexical_unit_id_list = lexical_units
    lexical_units_ids = [int(id) for id in lexical_unit_id_list.split(',')]
    lexical_units = []
    for lexical_unit_id in lexical_units_ids:
        lexical_units.append(LexicalUnit.objects.get(id=lexical_unit_id))
    unit_bases = [lexical_unit.base for lexical_unit in lexical_units]
    base = unit_bases[0].split(' ')[0]
    for unit_base in unit_bases:
        if unit_base != base and unit_base != base + u' się':
             raise SuspiciousOperation
    frame = SemanticFrame()
    frame.save()
    for lexical_unit in lexical_units:
        frame.lexical_units.add(lexical_unit)
    lemma = Lemma.objects.get(entry=lexical_unit.base, old=False)
    return ajax_frames(request, lemma.id)

@ajax(method='get', encode_result=False)
def ajax_create_complement(request, lemma_id, frame, roles):
    #lemma_id = int(request.GET.get('lemma_id', ''))
    lemma = Lemma.objects.get(id=lemma_id, old=False)
    frame_id = frame
    #frame_id = int(request.GET.get('frame', ''))
    frame = SemanticFrame.objects.get(id=frame_id)
    #role_id_list = request.GET.get('roles', '')
    role_id_list = roles
    role_ids = [int(id) for id in role_id_list.split(',')]
    roles = []
    for role_id in role_ids:
        roles.append(SemanticRole.objects.get(id=role_id))
    complement = Complement(frame=frame)
    complement.save()
    for role in roles:
        complement.roles.add(role)
    return ajax_frames(request, lemma.id)
    
@ajax(method='get', encode_result=False)
def ajax_update_meanings(request, operations, lemma_id):
    translation = {}
    for operation in operations:
        if operation['operation'] == "set_glossa":
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            set_glossa(unit, operation['value'])
        elif operation['operation'] == "add_example":
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            add_example(unit, operation['example'])
        elif operation['operation'] == "remove_example": 
            if int(operation['unit']) in translation:
                unit = translation[int(operation['unit'])]
            else:
                unit = int(operation['unit'])
            remove_example(unit, operation['example'])
        elif operation['operation'] == "add_unit":
            translation[operation['unit']['id']] = add_unit(operation['unit'])
        elif operation['operation'] == "remove_unit":
            luid = int(operation['luid'])
            if luid in translation:
                remove_unit(translation[luid])
            else:
                remove_unit(luid)
        else:
            pass
    
    return ajax_units(request)
            
def set_glossa(unit_id, new_glossa):
    unit = LexicalUnit.objects.get(id=unit_id)
    unit.glossa = new_glossa
    unit.save()

def add_example(unit_id, example_id):
    unit = LexicalUnit.objects.get(id=unit_id)
    nkjp_example = NKJP_Example.objects.get(id=example_id)
    lue = LexicalUnitExamples(example=nkjp_example, lexical_unit=unit)
    lue.save()

def remove_example(unit_id, example_id):
    unit = LexicalUnit.objects.get(id=unit_id)
    nkjp_example = NKJP_Example.objects.get(id=example_id)
    lue = LexicalUnitExamples.objects.get(example=nkjp_example, lexical_unit=unit)
    lue.delete()

def add_unit(unit): # returns new id
    
    s1 = Synset(id=(min(Synset.objects.all().aggregate(Min('id'))['id__min'], 0) - 1))
    s1.save()
    lu = LexicalUnit(base=unit['base'], sense=unit['sense'], pos=unit['pos'], glossa=unit['glossa'], luid=-1, synset=s1)
    lu.save()
    
    
    if int(unit['relation']) == 1:
        s2 = Synset.objects.get(id=int(unit['to']))
        r = Synonymy(parent=s1, child=s2)
        r.save()
        r = Synonymy(parent=s2, child=s1)
        r.save()
    elif int(unit['relation']) == 0:
        s2 = Synset.objects.get(id=int(unit['to']))
        r = Hypernymy(parent=s1, child=s2)
        r.save()
    else:
        pass
    
    return lu.id
    
def remove_unit(luid):
    lu = LexicalUnit.objects.get(id=luid)
    if lu.luid is not None and lu.luid >= 0:
        return
    else:
        lu.delete()
    
@ajax(method='get', encode_result=False)
def ajax_modify_frames(request, operations, lemma_id):
    
    if not request.user.is_authenticated():
        return 'user logged out'

    store_old_versions(lemma_id, operations, request.user)

    translation = {'frame_id': {}, 'complement_id': {}, 'preference_id': {}}
    for operation in operations:
        if operation['operation'] == "create_frame":
            luids = [int(m['id']) for m in operation['meanings']]
            translation['frame_id'][int(operation['id'])] = create_frame(luids)
        elif operation['operation'] == "remove_frame":
            if int(operation['id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['id'])]
            else:
                frame_id = int(operation['id'])
            remove_frame(frame_id)
        elif operation['operation'] == "add_argument": 
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            roles = [int(r) for r in operation['role']]
            translation['complement_id'][int(operation['id'])] = add_argument(frame_id, roles)
        elif operation['operation'] == "remove_argument":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            remove_argument(frame_id, complement_id)
        elif operation['operation'] == "connect":
            frame_data = operation['arg'].split('_')
            if int(frame_data[1]) in translation['frame_id']:
                frame_id = translation['frame_id'][int(frame_data[1])]
            else:
                frame_id = int(frame_data[1])
            if int(frame_data[3]) in translation['complement_id']:
                complement_id = translation['complement_id'][int(frame_data[3])]
            else:
                complement_id = int(frame_data[3])
            schema_data = operation['connect'].split('_')
            schema_id = int(schema_data[1])
            position_id = int(schema_data[3])
            argument_id = int(schema_data[5])
            alternation = int(schema_data[7])
            connect(frame_id, complement_id, schema_id, position_id, argument_id, alternation)
        elif operation['operation'] == "disconnect":
            frame_data = operation['arg'].split('_')
            if int(frame_data[1]) in translation['frame_id']:
                frame_id = translation['frame_id'][int(frame_data[1])]
            else:
                frame_id = int(frame_data[1])
            if int(frame_data[3]) in translation['complement_id']:
                complement_id = translation['complement_id'][int(frame_data[3])]
            else:
                complement_id = int(frame_data[3])
            schema_data = operation['connect'].split('_')
            schema_id = int(schema_data[1])
            position_id = int(schema_data[3])
            argument_id = int(schema_data[5])
            alternation = int(schema_data[7])
            disconnect(frame_id, complement_id, schema_id, position_id, argument_id, alternation)
        elif operation['operation'] == "assign_role":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            roles = [int(r) for r in operation['role']]
            assign_role(frame_id, complement_id, roles)
        elif operation['operation'] == "change_units":
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            luids = [int(m) for m in operation['units']]
            change_units(frame_id, luids)
        elif operation['operation'] == "add_preference":
            # {operation: 'add_preference', frame_id: frame_id, complement_id: complement_id, preference_id: preference_id, preference: preference}
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            preference_id = add_preference(frame_id, complement_id, operation['preference']['type'], operation['preference']['content'])
            translation['preference_id'][operation['preference_id']] = preference_id
        elif operation['operation'] == "remove_preference":
            # {operation: 'remove_preference', frame_id: frame_id, complement_id: complement_id, preference_id: preference_id}
            if int(operation['frame_id']) in translation['frame_id']:
                frame_id = translation['frame_id'][int(operation['frame_id'])]
            else:
                frame_id = int(operation['frame_id'])
            if int(operation['complement_id']) in translation['complement_id']:
                complement_id = translation['complement_id'][int(operation['complement_id'])]
            else:
                complement_id = int(operation['complement_id'])
            if operation['preference_id'] in translation['preference_id']:
                preference_id = translation['preference_id'][operation['preference_id']]
            else:
                preference_id = (operation['preference_id'][0], int(operation['preference_id'][1:]))
                
            remove_preference(frame_id, complement_id, preference_id)
        else:
            pass
    
    return ajax_frames(request)
    
def create_frame(luids):
    frame = SemanticFrame()
    frame.save()
    for id in luids:
        lu = LexicalUnit.objects.get(id=id)
        frame.lexical_units.add(lu)
    return frame.id

def change_units(frame_id, luids):
    frame = SemanticFrame.objects.get(id=frame_id)
    frame.lexical_units = []
    for id in luids:
        lu = LexicalUnit.objects.get(id=id)
        frame.lexical_units.add(lu)

def remove_frame(frame_id):
    frame = SemanticFrame.objects.get(id=frame_id)
    frame.removed = True
    frame.save()
    
def add_argument(frame_id, roles):
    if validate_roles(roles):
        frame = SemanticFrame.objects.get(id=frame_id)
        complement = Complement(frame=frame)
        complement.save()
        frame.complements.add(complement)
        role_objects = []
        for r in roles:
            role_objects.append(SemanticRole.objects.get(id=r))
        complement.roles = role_objects
        return complement.id

def remove_argument(frame_id, complement_id):
    Complement.objects.get(id=complement_id).delete()
    
def connect(frame_id, complement_id, schema_id, position_id, argument_id, alternation):
    schema = Frame.objects.get(id=schema_id)
    position = Position.objects.get(id=position_id)
    argument = Argument.objects.get(id=argument_id)
    fpas = FramePosition.objects.filter(frame=schema, position=position, argument=argument, alternation=alternation)
    if len(fpas) > 0:
        fpa = fpas[0]
    else:
        fpa = FramePosition(frame=schema, position=position, argument=argument, alternation=alternation)
        fpa.save()
    complement = Complement.objects.get(id=complement_id)
    complement.realizations.add(fpa)
    
def disconnect(frame_id, complement_id, schema_id, position_id, argument_id, alternation):
    schema = Frame.objects.get(id=schema_id)
    position = Position.objects.get(id=position_id)
    argument = Argument.objects.get(id=argument_id)
    fpas = FramePosition.objects.filter(frame=schema, position=position, argument=argument, alternation=alternation)
    if len(fpas) > 0:
        fpa = fpas[0]
    else:
        return
    complement = Complement.objects.get(id=complement_id)
    complement.realizations.remove(fpa)

def assign_role(frame_id, complement_id, roles):
    if validate_roles(roles):
        role_objects = []
        for r in roles:
            role_objects.append(SemanticRole.objects.get(id=r))
        complement = Complement.objects.get(id=complement_id)
        complement.roles = role_objects
        
def validate_roles(roles):
    role_objects = []
    for r in roles:
        role_objects.append(SemanticRole.objects.get(id=r))
    if len(role_objects) > 2:
        return False
    ok = False
    for r in role_objects:
        if not r.color == None:
            ok = not ok
    return ok
    
# preference_id = add_preference(frame_id, complement_id, operation['preference']['type'], operation['preference']['content'])
def add_preference(frame_id, complement_id, preference_type, preference_content):

    complement = Complement.objects.get(id=complement_id)
    if complement.selective_preference is None:
        sp = SelectivePreference()
        sp.save()
        complement.selective_preference = sp
        complement.save()
    
    if preference_type == 'g':    
        general = GeneralSelectivePreference.objects.get(id=int(preference_content))
        complement.selective_preference.generals.add(general)
        return ('g', general.id)
    elif preference_type == 's':
        synset = Synset.objects.get(id=int(preference_content))
        complement.selective_preference.synsets.add(synset)
        return ('s', synset.id)
    elif preference_type == 'r':
        relation = SelectivePreferenceRelations.objects.get(id=int(preference_content['relation']))
        argument = [int(a) for a in preference_content['to'].split(',')]
        frame = SemanticFrame.objects.get(id=frame_id)
        candidates = Complement.objects.filter(frame=frame)
        found = None
        for c in candidates:
            if len(c.roles.all()) == len(argument):
                roles = [r.id for r in c.roles.all()]
                ok = True
                for a in argument:
                    if a not in roles:
                        ok = False
                if ok:
                    found = c
                    break
        if found is not None:
            rsp = RelationalSelectivePreference(relation=relation, to=found)
            rsp.save()
            complement.selective_preference.relations.add(rsp)
            return ('r', rsp.id)
        else:
            return -1
    else:
        return -1

# remove_preference(frame_id, complement_id, preference)
def remove_preference(frame_id, complement_id, preference):
    preference_type, preference_id = preference
    if preference_type == 'g':
        complement = Complement.objects.get(id=complement_id)
        g = complement.selective_preference.generals.get(id = int(preference_id))
        complement.selective_preference.generals.remove(g)
    elif preference_type == 's':
        complement = Complement.objects.get(id=complement_id)
        s = complement.selective_preference.synsets.get(id = int(preference_id))
        complement.selective_preference.synsets.remove(s)
    elif preference_type == 'r':
        complement = Complement.objects.get(id=complement_id)
        r = complement.selective_preference.relations.get(id = int(preference_id))
        complement.selective_preference.relations.remove(r)
        
@ajax(method='get', encode_result=True)         
def ajax_plWN_context_lookup(request, term):
    results = []
    term = unicode(term)
    if len(term) > 0:
        obj_results = LexicalUnit.objects.filter(base__startswith=term)
        results = get_ordered_lexical_units_bases(obj_results)
    return {'result': results}

def get_ordered_lexical_units_bases(lexical_units_query):
    last_unit_base = ''
    lexical_unit_bases = []
    ordered_lexical_units = lexical_units_query.order_by('base')
    for lexical_unit in ordered_lexical_units:
        if lexical_unit.base != last_unit_base:
            lexical_unit_bases.append(lexical_unit.base)
        last_unit_base = lexical_unit.base
    return lexical_unit_bases

@ajax(method='get')
def validate_semantics(request, lemma_id, new_status_id):
    error_msg = ''
    try:
        status = Lemma_Status.objects.get(id=new_status_id)
    except Lemma_Status.DoesNotExist:
        status = None
    if status and status.check_semantics:
        error_msg = validate_frames(lemma_id)
        if not error_msg:
            error_msg = validate_lexical_units(lemma_id)
        if not error_msg:
            error_msg = validate_schemas(lemma_id)
    return {'error_message': error_msg}