From 4aae77e5ed4f60257da281f6be982de167079bcb Mon Sep 17 00:00:00 2001 From: dcz <dcz@ipipan.waw.pl> Date: Fri, 1 Jul 2022 14:06:49 +0200 Subject: [PATCH] Dinamic uniframe cration on take event. --- .../js/components/LexicalUnitDisplay.js | 17 +++- .../unification/UnificationPreprocessXML.py | 4 +- importer/unification/UnifiedFrameImport.py | 24 +++-- requirements.txt | 1 + semantics/urls.py | 1 - semantics/views.py | 19 ---- unifier/urls.py | 2 + unifier/views.py | 99 ++++++++++++++++--- 8 files changed, 118 insertions(+), 49 deletions(-) diff --git a/entries/static/entries/js/components/LexicalUnitDisplay.js b/entries/static/entries/js/components/LexicalUnitDisplay.js index adec5a7..da0ec6b 100644 --- a/entries/static/entries/js/components/LexicalUnitDisplay.js +++ b/entries/static/entries/js/components/LexicalUnitDisplay.js @@ -22,10 +22,17 @@ export default { take () { $.ajax({ type : 'post', - url : `/${lang}/semantics/frame_assign/${this.unifiedFrame.pk}/`, + url : `/${lang}/unifier/frame_assign/${this.lexicalUnitId}/`, dataType : 'json', timeout : 60000, - }).then(() => { this.$emit('goToEdit', this.unifiedFrame.pk); }); + success: function (response) { + show_info('Rama została stworzona oraz przypisana użytkownikowi.'); + this.$emit('goToEdit', response.unified_frame_id); + }.bind(this), + error: function (request, errorType, errorMessage) { + show_error(errorType + ' (' + errorMessage + ')'); + } + }); }, confirmInvalid () { $.ajax({ @@ -86,9 +93,9 @@ export default { <spinner /> <div id="semantics-frames"></div> <div class="text-center mb-3"> - <div v-if="unifiedFrame"> + <div> <a class="btn btn-sm btn-outline-dark mr-2" v-if="frame?.status === 'N'" @click="markAsInvalid">{{ !isLeksykograf() ? gettext('Zła') : gettext('Błędna') }}</a> - <a class="btn btn-sm btn-outline-dark mr-2" v-if="isReadyToGet()" @click="take">{{ gettext('Pobierz') }}</a> + <a class="btn btn-sm btn-outline-dark mr-2" v-if="isReadyToGet() && unifiedFrame === null" @click="take">{{ gettext('Pobierz') }}</a> <a class="btn btn-sm btn-outline-dark mr-2" v-if="isReadyToProcess()" @@ -118,7 +125,7 @@ export default { {{ gettext('Odrzuć') }} </a> </div> - <span v-if="unifiedFrame === null">{{ gettext('Brak ramy unifikacyjnej') }}</span> +<!-- <span v-if="unifiedFrame === null">{{ gettext('Brak ramy unifikacyjnej') }}</span>--> </div> <div id="lexical-unit-notes"></div> </div> diff --git a/importer/unification/UnificationPreprocessXML.py b/importer/unification/UnificationPreprocessXML.py index 8f3f09b..f1d86dd 100644 --- a/importer/unification/UnificationPreprocessXML.py +++ b/importer/unification/UnificationPreprocessXML.py @@ -37,6 +37,7 @@ class UnificationPreprocessHandler(handler.ContentHandler): self.entry_meanings = {} self.meanings = {} self.frames = {} + self.unified_frame_ids = [] def startElement(self, name, attrs): if name == 'date': @@ -57,7 +58,8 @@ class UnificationPreprocessHandler(handler.ContentHandler): self._current.setContent(self._content.strip()) self._current = self._current._parent if name == 'unifier_frame': - UnifiedFrameImport.UnifiedFrameImport.storeUnifiedFrame(self._subtree) + pk = UnifiedFrameImport.UnifiedFrameImport.storeUnifiedFrame(self._subtree) + self.unified_frame_ids.append(pk) self._content = '' def characters(self, content): diff --git a/importer/unification/UnifiedFrameImport.py b/importer/unification/UnifiedFrameImport.py index cef6d69..bdf8deb 100644 --- a/importer/unification/UnifiedFrameImport.py +++ b/importer/unification/UnifiedFrameImport.py @@ -16,7 +16,7 @@ class UnifiedFrameImport: return role_type @classmethod - def storeUnifiedFrameArgument(cls, tree, unifiedFrame): + def storeUnifiedFrameArgument(cls, tree, unifiedFrame, argumentIdsMapping): id = tree._attrs['id'] attribute = None semantic_roles = [] @@ -35,20 +35,23 @@ class UnifiedFrameImport: role_type_obj = UnifiedFrameImport.storeAndGetRoleType(role_type) role_type_obj.save() - argument = UnifiedFrameArgument(pk=int(id), - role_type=role_type_obj, + argument = UnifiedFrameArgument(role_type=role_type_obj, unified_frame=unifiedFrame) + argument.save() + + argumentIdsMapping[id] = argument.pk + argument.proposed_roles.set([]) argument.role = None - argument.save() for semantic_role in semantic_roles: semantic_roles_obj = semantic_role.store() semantic_roles_obj.save() argument.proposed_roles.add(semantic_roles_obj) + argument.save() @classmethod - def storeSlowalFrame(cls, tree, unifiedFrame): + def storeSlowalFrame(cls, tree, unifiedFrame, argumentIdsMapping): slowal_frame_id = tree._attrs['id'] unifiedFrame2SlowalFrameMapping = UnifiedFrame2SlowalFrameMapping(unified_frame=unifiedFrame, @@ -62,7 +65,7 @@ class UnifiedFrameImport: unifier_argument_id = subsubtree._attrs['unifier_argument_id'] slowal_id = subsubtree._attrs['slowal_id'] unifiedFrameArgumentSlowalFrameMapping = UnifiedFrameArgumentSlowalFrameMapping(unified_frame_mapping=unifiedFrame2SlowalFrameMapping, - unified_agrument_id=unifier_argument_id, + unified_agrument_id=argumentIdsMapping[unifier_argument_id], slowal_agrument_id=slowal_id) unifiedFrameArgumentSlowalFrameMapping.save() @@ -72,11 +75,16 @@ class UnifiedFrameImport: unifiedFrame = UnifiedFrame() unifiedFrame.save() + argumentIdsMapping = {} for node in frame_tree._children: if node._name == 'argument': - UnifiedFrameImport.storeUnifiedFrameArgument(node, unifiedFrame) + UnifiedFrameImport.storeUnifiedFrameArgument(node, unifiedFrame, argumentIdsMapping) + + for node in frame_tree._children: if node._name == 'connections': for subnode in node._children: if subnode._name == 'slowal_frame': - UnifiedFrameImport.storeSlowalFrame(subnode, unifiedFrame) + UnifiedFrameImport.storeSlowalFrame(subnode, unifiedFrame, argumentIdsMapping) + + return unifiedFrame.pk diff --git a/requirements.txt b/requirements.txt index b01fc60..ee136c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,4 @@ simplejson==3.17.2 six==1.15.0 soupsieve==2.0.1 sqlparse==0.3.0 +requests diff --git a/semantics/urls.py b/semantics/urls.py index 92a20ad..8386f8f 100644 --- a/semantics/urls.py +++ b/semantics/urls.py @@ -6,7 +6,6 @@ app_name = 'semantics' urlpatterns = [ path('frame_mark_as_invalid/<int:frame_pk>/', views.frame_mark_as_invalid, name='frame_mark_as_invalid'), - path('frame_assign/<int:unified_frame_pk>/', views.frame_assign, name='frame_assign'), path('frame_confirm_invalid/<int:frame_pk>/', views.frame_confirm_invalid, name='frame_confirm_invalid'), path('frame_reject_invalid/<int:frame_pk>/', views.frame_reject_invalid, name='frame_reject_invalid'), ] diff --git a/semantics/views.py b/semantics/views.py index d1c3537..f2043b9 100644 --- a/semantics/views.py +++ b/semantics/views.py @@ -19,25 +19,6 @@ def frame_mark_as_invalid(request, frame_pk): frame.save() return {} - -@ajax(login_required=True, method='post') -@transaction.atomic -def frame_assign(request, unified_frame_pk): - unifiedFrame = get_object_or_404( - UnifiedFrame.objects, - pk=unified_frame_pk, - ) - - slowal_frames = [connection.slowal_frame for connection in unifiedFrame.unified_frame_2_slowal_frame.all()] - - for slowal_frame in slowal_frames: - slowal_frame.status = choices.FrameStatus.PROCESSING - Assignment.assign(user=request.user, subject=slowal_frame) - slowal_frame.save() - - return {} - - @ajax(login_required=True, method='post') @transaction.atomic @permission_required('semantics.manage_invalid_lexical_units') diff --git a/unifier/urls.py b/unifier/urls.py index 3ea83f5..6fd291e 100644 --- a/unifier/urls.py +++ b/unifier/urls.py @@ -21,5 +21,7 @@ urlpatterns = [ path('duplicate_unified_frame/', views.duplicate_unified_frame, name='duplicate_unified_frame'), path('change_unified_frame_status_to_ready/', views.change_unified_frame_status_to_ready, name='change_unified_frame_status_to_ready'), path('remove_selectional_preference/', views.remove_selectional_preference, name='remove_selectional_preference'), + path('build_unified_frame_xml/', views.build_unified_frame_xml, name='build_unified_frame_xml'), + path('frame_assign/<int:lu_id>/', views.frame_assign, name='frame_assign'), ] diff --git a/unifier/views.py b/unifier/views.py index abe7ec8..beb6b49 100644 --- a/unifier/views.py +++ b/unifier/views.py @@ -1,16 +1,25 @@ import json - +import requests from django.db import transaction -from django.http import JsonResponse +from django.http import JsonResponse, HttpResponse +from django.shortcuts import get_object_or_404 +from django.views.decorators.csrf import csrf_exempt -from common.decorators import ajax_required +from common.decorators import ajax_required, ajax from entries.polish_strings import EXAMPLE_SOURCE, EXAMPLE_OPINION from entries.views import get_scroller_params, get_alternations, get_prefs_list, schema2dict, frame2dict +from importer.unification.UnificationPreprocessXML import UnificationPreprocessHandler +from meanings.models import LexicalUnit +from semantics.choices import FrameStatus from semantics.models import Frame, ArgumentRole, SemanticRole, RoleAttribute, RoleType from syntax.models import Schema from unifier.models import UnifiedFrameArgument, UnifiedRelationalSelectionalPreference, UnifiedFrame, \ UnifiedFrame2SlowalFrameMapping, UnifiedFrameArgumentSlowalFrameMapping +from users.models import Assignment from . import choices +from xml.etree.ElementTree import Element, SubElement, tostring +import io +from xml.sax import handler, make_parser @ajax_required @transaction.atomic @@ -388,19 +397,79 @@ def change_unified_frame_status_to_ready(request): unifiedFrame.save() return JsonResponse({}) +def create_unified_frame(lu_id): + response = requests.post('http://127.0.0.1:8000/en/unifier/build_unified_frame_xml/?lu_id='+str(lu_id)) + + parser = make_parser() + parser.setFeature(handler.feature_external_ges, False) + + unifiedFrameXMLHandler = UnificationPreprocessHandler() + parser.setContentHandler(unifiedFrameXMLHandler) + + f = io.StringIO(response.content.decode("utf-8")) + + parser.parse(f) + + unified_frame_id = unifiedFrameXMLHandler.unified_frame_ids[0] + + return unified_frame_id + +@csrf_exempt +def build_unified_frame_xml(request): + if request.method == 'POST': + lu_id = request.GET.get('lu_id') + + lu = LexicalUnit.objects.get(pk=lu_id) + frames = list(lu.frames.all()) + if len(frames) > 0: + matchingElem = Element('matching') + unifier_frameElem = SubElement(matchingElem, 'unifier_frame') + + frame = frames[0] + + arguments = list(frame.arguments.all()) + argCnt = len(arguments) + for id in range(argCnt): + argumentElem = SubElement(unifier_frameElem, 'argument', attrib={'id': str(id)}) + semantic_role_typeElem = SubElement(argumentElem, 'semantic_role', attrib={'type': 'role'}) + rolesElem = SubElement(argumentElem, 'roles') + roleElem1 = SubElement(rolesElem, 'role', attrib={'name': 'Initiator'}) + roleElem2 = SubElement(rolesElem, 'role', attrib={'name': 'Manner'}) + + connectionsElem = SubElement(unifier_frameElem, 'connections') + + slowal_frameElem = SubElement(connectionsElem, 'slowal_frame', attrib={'id': str(frame.id)}) + + arguments_connectionsElem = SubElement(slowal_frameElem, 'arguments_connections') + + for id, argument in enumerate(arguments): + arguments_connectionElem = SubElement(arguments_connectionsElem, 'arguments_connection', + attrib={'unifier_argument_id': str(id), 'slowal_id': str(argument.id)}) + xml = tostring(matchingElem) + return HttpResponse(xml) + + return HttpResponse() @ajax_required @transaction.atomic -def create_simple_unified_frame(request): - if request.method == 'POST': - lu_id = request.POST['lu_id'] - # unifiedFrame = UnifiedFrame.objects.get(pk=unified_frame_id) - # unifiedFrame.status = choices.UnifiedFrameStatus.READY - # - # for mapping in unifiedFrame.unified_frame_2_slowal_frame.all(): - # mapping.verified = True - # mapping.save() - # - # unifiedFrame.save() - return JsonResponse({}) +def frame_assign(request, lu_id): + + unified_frame_pk = create_unified_frame(lu_id) + + unifiedFrame = get_object_or_404( + UnifiedFrame.objects, + pk=unified_frame_pk, + ) + + slowal_frames = [connection.slowal_frame for connection in unifiedFrame.unified_frame_2_slowal_frame.all()] + + for slowal_frame in slowal_frames: + slowal_frame.status = FrameStatus.PROCESSING + Assignment.assign(user=request.user, subject=slowal_frame) + slowal_frame.save() + + ret = { + 'unified_frame_id': unified_frame_pk + } + return JsonResponse(ret) -- GitLab