Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ipipan/valunifer
1 result
Show changes
Commits on Source (38)
Showing
with 656 additions and 73 deletions
......@@ -67,3 +67,18 @@ Compiled application files will be located in `frontend/dist/`.
python manage.py makemigrations <module-name> (e.g. python manage.py makemigrations users)
python manage.py migrate <module-name> (e.g. python manage.py migrate users)
## Database update
export LOADING_THE_SLOWOSIEC_ONTOLOGY_HIERARCHY_DISABLED=true
export WALENTY_FILE_NAME=<new walenty file>
./update_db.sh
## Run tests
export LOADING_THE_SLOWOSIEC_ONTOLOGY_HIERARCHY_DISABLED=true
python manage.py test --settings=shellvalier.settings-test
## Database update for vertical relations
export LOADING_THE_SLOWOSIEC_ONTOLOGY_HIERARCHY_DISABLED=true
python manage.py db_import_relation_entries
......@@ -10,9 +10,9 @@ from unifier.models import UnifiedFrame
from django.contrib.auth.models import User
from users.models import Assignment
from common.valunifier_tei import createteixml
from common.management.commands.valunifier_tei import createteixml
BASEPATH = '.'
BASEPATH = 'data/tei/'
class Command(BaseCommand):
args = ''
......
#-*- coding:utf-8 -*-
import datetime
from collections import defaultdict
XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'
class SchemaPacker():
def __init__(self):
# packed_subentry_id -> packed_schema_id -> (Subentry, Schema) (any that matches)
self.unique_schemas = defaultdict(lambda: defaultdict(lambda: None))
# packed_subentry -> id
self.free_subentry_id = 0
self.unique_subentry_id = defaultdict(lambda: -1)
# packed_schema -> id
self.free_schema_id = 0
self.unique_schema_id = defaultdict(lambda: -1)
# subentry -> schema -> (packed_subentry, packed_schema)
self.packed_schemas = defaultdict(lambda: defaultdict(lambda: None))
def _pack_subentry(self, subentry):
sie = subentry.inherent_sie.name
aspect = ''
if subentry.aspect is not None:
aspect = subentry.aspect.name
negativity = ''
if subentry.aspect is not None:
negativity = subentry.negativity.name
predicativity = subentry.predicativity.name
return (sie, aspect, negativity, predicativity)
def _pack_schema(self, schema):
opinion = 'unk'
if schema.opinion.key is not None:
opinion = schema.opinion.key
positions = []
for position in schema.positions.all():
positions.append(position.id)
positions = tuple(sorted(positions))
return (opinion, positions)
def _pack(self, subentry, schema):
if self.packed_schemas[subentry][schema] is None:
self.packed_schemas[subentry][schema] = (self._pack_subentry(subentry), self._pack_schema(schema))
return self.packed_schemas[subentry][schema]
def add(self, subentry, schema):
packed_subentry, packed_schema = self._pack(subentry, schema)
if self.unique_subentry_id[packed_subentry] == -1:
self.unique_subentry_id[packed_subentry] = self.free_subentry_id
self.free_subentry_id += 1
packed_subentry_id = self.unique_subentry_id[packed_subentry]
if self.unique_schema_id[packed_schema] == -1:
self.unique_schema_id[packed_schema] = self.free_schema_id
self.free_schema_id += 1
packed_schema_id = self.unique_schema_id[packed_schema]
self.unique_schemas[packed_subentry_id][packed_schema_id] = (subentry, schema)
self.packed_schemas[subentry][schema] = (packed_subentry, packed_schema)
def get_ids(self, subentry, schema):
packed_subentry, packed_schema = self.packed_schemas[subentry][schema]
return (self.unique_subentry_id[packed_subentry], self.unique_schema_id[packed_schema])
......@@ -8,7 +8,9 @@ from xml.sax.saxutils import escape
from unifier.models import UnifiedFrame2SlowalFrameMapping, \
UnifiedFrameArgumentSlowalFrameMapping
from connections.models import ArgumentConnection
from connections.models import ArgumentConnection, ExampleConnection
from common.management.commands.schema_packer import SchemaPacker
from collections import defaultdict
......@@ -72,7 +74,7 @@ def write_content(root, unified_frames):
body = etree.SubElement(text, 'body')
schemata = etree.SubElement(body, 'div')
frames = etree.SubElement(body, 'div')
used_schemata = set()
used_schemata = SchemaPacker()
write_unified_frames(frames, unified_frames, used_schemata)
write_used_schemata(schemata, used_schemata)
......@@ -325,7 +327,7 @@ def write_lexical_unit(parent, lexical_unit, mapping):
opinion_symbol.attrib['value'] = slowal_frame.opinion.key
def write_alternations(parent, unified_frame, lexical_unit, mapping, used_schemata):
alternations = prepare_alternations(mapping, used_schemata)
alternations = prepare_alternations(lexical_unit, mapping, used_schemata)
for key in sorted(alternations.keys()):
alternation_fs = etree.SubElement(parent, 'fs')
alternation_fs.attrib['type'] = 'aternation'
......@@ -342,33 +344,79 @@ def write_alternations(parent, unified_frame, lexical_unit, mapping, used_schema
argument_fs.attrib['type'] = 'argument'
argument_fs.attrib['sameAs'] = u'#unif_%d.%d-arg' % (unified_frame.id, argument.id)
phrases_f = etree.SubElement(connection_fs, 'f')
write_phrases_coll(phrases_f, schema_hooks)
write_phrases_coll(phrases_f, schema_hooks, used_schemata)
def prepare_alternations(mapping, used_schemata):
def prepare_alternations(lexical_unit, mapping, used_schemata):
connections_info = analyse_connections(mapping)
argument_mappings = UnifiedFrameArgumentSlowalFrameMapping.objects.filter(unified_frame_mapping = mapping)
alternations = defaultdict(lambda: defaultdict(lambda: []))
for argument_mapping in argument_mappings:
uargument = argument_mapping.unified_agrument
sargument = argument_mapping.slowal_agrument
argument_realization = ArgumentConnection.objects.get(argument = sargument)
by_schema_realizations = argument_realization.schema_connections.all()
for schema_hook in by_schema_realizations:
subentry = schema_hook.subentry
schema = schema_hook.schema
used_schemata.add((subentry, schema))
alternation = schema_hook.alternation
alternations[(subentry.id, schema.id, alternation)][uargument].append(schema_hook)
success = False
try:
argument_realization = ArgumentConnection.objects.get(argument = sargument)
success = True
except:
print(sargument.id)
if success:
by_schema_realizations = argument_realization.schema_connections.all()
for schema_hook in by_schema_realizations:
if valid_connection(lexical_unit, schema_hook, connections_info):
subentry = schema_hook.subentry
schema = schema_hook.schema
used_schemata.add(subentry, schema)
alternation = schema_hook.alternation
alternations[(subentry.id, schema.id, alternation)][uargument].append(schema_hook)
return alternations
def write_phrases_coll(parent, phrases_list):
def analyse_connections(mapping):
sframe = mapping.slowal_frame
lus = sframe.lexical_units.all()
lu_schema_connections = defaultdict(lambda: set())
connected_lu_count = defaultdict(lambda: 0)
for lu in lus:
examples = ExampleConnection.objects.filter(lexical_unit=lu)
for example in examples:
scs = example.schema_connections.all()
if len(scs) > 0:
schema_hook = scs[0]
schema_id = (schema_hook.subentry.id, schema_hook.schema.id)
lu_schema_connections[lu].add(schema_id)
connected_lu_count[schema_id] += 1
return (lu_schema_connections, connected_lu_count)
def valid_connection(lexical_unit, schema_hook, connections_info):
schema_id = (schema_hook.subentry.id, schema_hook.schema.id)
lu_schema_connections, connected_lu_count = connections_info
if schema_id in lu_schema_connections[lexical_unit]:
return True
if connected_lu_count[schema_id] == 0:
in_lemma = False
in_lemma |= (' się ' in lexical_unit.base)
in_lemma |= (lexical_unit.base.endswith(' się'))
in_schema = False
in_schema |= (schema_hook.subentry.inherent_sie.name == 'true')
for position in schema_hook.schema.positions.all():
if position.phrases_count == 1:
phrase_type = position.phrase_types.all()[0].main_type
in_schema |= (phrase_type == 'refl')
in_schema |= (phrase_type == 'recip')
return (in_lemma == in_schema)
return False
def write_phrases_coll(parent, phrases_list, used_schemata):
vColl = etree.SubElement(parent, 'vColl')
vColl.attrib['org'] = 'set'
for phrase in phrases_list:
phrase_fs = etree.SubElement(vColl, 'fs')
phrase_fs.attrib['type'] = 'phrase'
phrase_fs.attrib['sameAs'] = u'#unif_%d.%d.%d.%d-phr' %(phrase.subentry.id, phrase.schema.id, phrase.position.id, phrase.phrase_type.id)
subentry_id, schema_id = used_schemata.get_ids(phrase.subentry, phrase.schema)
phrase_fs.attrib['sameAs'] = u'#unif_%d.%d.%d.%d-phr' %(subentry_id, schema_id, phrase.position.id, phrase.phrase_type.id)
#=================== DIV -- SYNTACTIC SCHEMATA ===================#
......@@ -377,23 +425,74 @@ def write_used_schemata(parent, used_schemata):
schemata_head = etree.SubElement(parent, 'head')
schemata_head.text = 'Syntactic Schemata'
for subentry, schema in used_schemata:
write_schema_entry(parent, subentry, schema)
for subentry_id in used_schemata.unique_schemas:
for schema_id in used_schemata.unique_schemas[subentry_id]:
subentry, schema = used_schemata.unique_schemas[subentry_id][schema_id]
write_schema_entry(parent, subentry, subentry_id, schema, schema_id)
def write_schema_entry(parent, subentry, schema):
entry_xml_id = u'unif_%d.%d-schent' %(subentry.id, schema.id)
def write_schema_entry(parent, subentry, subentry_id, schema, schema_id):
entry_xml_id = u'unif_%d.%d-schent' %(subentry_id, schema_id)
entry = etree.SubElement(parent, 'entry')
entry.attrib[etree.QName(XML_NAMESPACE, 'id')] = entry_xml_id
write_schema_definition(entry, subentry, schema)
write_schema_definition(entry, subentry, subentry_id, schema, schema_id)
write_schema(entry, subentry, subentry_id, schema, schema_id)
def write_schema_definition(parent, subentry, subentry_id, schema, schema_id):
d = etree.SubElement(parent, 'def')
d.text = schema_textual_representation(subentry, schema)
def schema_textual_representation(subentry, schema):
result = ''
if subentry.inherent_sie.name == 'true':
result += ' się:'
else:
rerult += ':'
opinion = schema.opinion.key
if opinion == 'vul':
result += ' wulgarny:'
elif opinion == 'col':
result += ' potoczny:'
elif opinion == 'dat':
result += ' archaiczny:'
elif opinion == 'bad':
result += ' zły:'
elif opinion == 'unc':
result += ' wątpliwy:'
elif opinion == 'cer':
result += ' pewny:'
else:
result += ' brak:'
if subentry.negativity is not None:
result += ' ' + subentry.negativity.name + ':'
else:
result += ' :'
write_schema(entry, subentry, schema)
if subentry.predicativity.name == 'true':
result += ' pred:'
else:
rerult += ':'
def write_schema_definition(parent, subentry, schema):
pass
if subentry.aspect is not None:
result += ' ' + subentry.aspect.name + ':'
else:
result += ' :'
def write_schema(parent, subentry, schema):
schema_xml_id = u'unif_%d.%d-sch' %(subentry.id, schema.id)
positions_rep = []
for position in schema.positions.all():
positions_rep.append(str(position))
result += ' ' + ' + '.join(positions_rep)
return result
def write_schema(parent, subentry, subentry_id, schema, schema_id):
schema_xml_id = u'unif_%d.%d-sch' %(subentry_id, schema_id)
schema_fs = etree.SubElement(parent, 'fs')
schema_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = schema_xml_id
......@@ -446,28 +545,33 @@ def write_schema(parent, subentry, schema):
predicativity_binary.attrib['value'] = predicativity
# positions
write_positions(schema_fs, subentry, schema)
write_positions(schema_fs, subentry_id, schema, schema_id)
def write_positions(parent, subentry, schema):
def write_positions(parent, subentry_id, schema, schema_id):
positions = schema.positions.all()
positions_f = etree.SubElement(parent, 'f')
positions_f.attrib['name'] = 'positions'
vColl = etree.SubElement(positions_f, 'vColl')
vColl.attrib['org'] = 'set'
for position in positions:
write_position(vColl, subentry, schema, position)
write_position(vColl, subentry_id, schema_id, position)
def write_position(parent, subentry, schema, position):
position_xml_id = u'unif_%d.%d.%d-psn' %(subentry.id, schema.id, position.id)
def write_position(parent, subentry_id, schema_id, position):
position_xml_id = u'unif_%d.%d.%d-psn' %(subentry_id, schema_id, position.id)
position_fs = etree.SubElement(parent, 'fs')
position_fs.attrib['type'] = 'position'
position_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = position_xml_id
text_rep_f = etree.SubElement(position_fs, 'f')
text_rep_f.attrib['name'] = 'textual_representation'
text_rep_string = etree.SubElement(text_rep_f, 'string')
text_rep_string.text = str(position)
write_function(position_fs, position)
write_control(position_fs, position)
write_phrases(position_fs, subentry, schema, position)
write_phrases(position_fs, subentry_id, schema_id, position)
def write_function(parent, position):
function = position.function
......@@ -494,17 +598,17 @@ def write_control(parent, position):
pred_control_symbol = etree.SubElement(vColl, 'symbol')
pred_control_symbol.attrib['value'] = control
def write_phrases(parent, subentry, schema, position):
def write_phrases(parent, subentry_id, schema_id, position):
phrases = position.phrase_types.all()
phrases_f = etree.SubElement(parent, 'f')
phrases_f.attrib['name'] = 'phrases'
vColl = etree.SubElement(phrases_f, 'vColl')
vColl.attrib['org'] = 'set'
for phrase in phrases:
write_phrase(vColl, subentry, schema, position, phrase)
write_phrase(vColl, subentry_id, schema_id, position, phrase)
def write_phrase(parent, subentry, schema, position, phrase):
phrase_xml_id = u'unif_%d.%d.%d.%d-phr' %(subentry.id, schema.id, position.id, phrase.id)
def write_phrase(parent, subentry_id, schema_id, position, phrase):
phrase_xml_id = u'unif_%d.%d.%d.%d-phr' %(subentry_id, schema_id, position.id, phrase.id)
phrase_fs = etree.SubElement(parent, 'fs')
phrase_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = phrase_xml_id
phrase_fs.attrib['type'] = phrase.main_type.name
......
MIEJSCE:
<lokal-1, budowla-1, rejon-1, obszar-1, państwo-1, jednostka administracyjna-1, woda-4>
select base, synset_id from meanings_lexicalunit where base='lokal' and sense='1';
4857
select base, synset_id from meanings_lexicalunit where base='budowla' and sense='1';
287
select base, synset_id from meanings_lexicalunit where base='rejon' and sense='1';
35149
select base, synset_id from meanings_lexicalunit where base='obszar' and sense='1';
2898
select base, synset_id from meanings_lexicalunit where base='państwo' and sense='1';
41068
select base, synset_id from meanings_lexicalunit where base='jednostka administracyjna' and sense='1';
5592
select base, synset_id from meanings_lexicalunit where base='woda' and sense='4';
31676
insert into semantics_selectivepreference_synsets (selectivepreference_id, synset_id) values (9, 4857),(9, 287),(9, 35149),(9, 2898),(9, 41068),(9, 5592),(9, 31676);
......@@ -281,6 +281,10 @@ legend {
background-color: rgba(138, 11, 17, 0.14) !important;
}
.vertical_rel_not_exists {
background-color: rgba(61, 22, 87, 0.14) !important;
}
.table-primary,
.table-primary>td,
.table-primary>th {
......@@ -347,3 +351,17 @@ legend {
display: none;
}
}
.attach-examples-custom-scrollbar {
position: relative;
height: 70%;
overflow: auto;
}
.attach-examples-table-wrapper-scroll-y {
display: block;
}
#attach-examples tr td {
padding: 2px;
}
......@@ -23,6 +23,18 @@ table.table-button-menu {
font-weight: bold;
}
#schemata-positions-table .active {
background-color: #dee1e4;
}
#unified-frame .argument.active {
background-color: #dee1e4;
}
#target-unified-frame .argument.active {
background-color: #dee1e4;
}
#free-lus-frame .argument.active {
background-color: black;
}
......@@ -12,6 +12,10 @@ var curr_examples_by_id = null;
var roles = []
var role_attributes = []
var role_sub_attributes = []
var frame_opinions = []
var main_vertical_relations = []
var temporal_vertical_relations = []
var vertical_relation_negations = []
function make_opinion_row(item, span, width) {
const opinion_row = document.createElement('tr');
......@@ -1008,11 +1012,16 @@ function setup_datatable(options) {
if (options.setup_hierarchy_marking === true && data.hierarchy_exists !== true) {
$(row).addClass('hierarchy_not_exists');
}
if (options.setup_vertical_rel_marking === true && data.has_vertical_relations !== true) {
$(row).addClass('vertical_rel_not_exists');
}
for (let i in options.columns) {
const column = options.columns[i];
if (column.data == 'status') {
$(cells)[i].innerHTML = "<img src='/static/entries/img_status/"+data['status']+"_status.svg' width='10' height='10'> "+data['status'];
if(!options.disable_display_graphical_status) {
for (let i in options.columns) {
const column = options.columns[i];
if (column.data == 'status') {
$(cells)[i].innerHTML = "<img src='/static/entries/img_status/" + data['status'] + "_status.svg' width='10' height='10'> " + data['status'];
}
}
}
},
......@@ -1131,6 +1140,50 @@ function getRoleSubAttributes() {
});
}
function getFrameOpinions() {
$.ajax({
dataType: "json",
url: '/' + lang + '/entries/frame_opinions',
success: function(data){
frame_opinions = data.result;
},
async: false
});
}
function getMainVerticalRelations() {
$.ajax({
dataType: "json",
url: '/' + lang + '/vertical_relations/ajax_main_vertical_relations',
success: function(data){
main_vertical_relations = data.main_vertical_relations;
},
async: false
});
}
function getTemporalVerticalRelations() {
$.ajax({
dataType: "json",
url: '/' + lang + '/vertical_relations/ajax_temporal_vertical_relations',
success: function(data){
temporal_vertical_relations = data.temporal_vertical_relations;
},
async: false
});
}
function getVerticalRelationNegations() {
$.ajax({
dataType: "json",
url: '/' + lang + '/vertical_relations/ajax_vertical_relation_negations',
success: function(data){
vertical_relation_negations = data.negations;
},
async: false
});
}
$(document).ready(function() {
bind_last_visited();
......@@ -1164,6 +1217,14 @@ $(document).ready(function() {
getRoleSubAttributes();
getFrameOpinions();
getMainVerticalRelations();
getTemporalVerticalRelations();
getVerticalRelationNegations();
// $.getJSON('relations', function(data){
// memorizeRelations(data.relations);
// });
......
function setup_free_lus_list(options) {
const can_see_assignees = has_permission("users.view_assignment");
const ajaxURL = can_see_assignees ? '/' + lang + '/entries/get_entries/?with_lexical_units=false&without_frames=true&show_linked_entries_disabled=true' : '/' + lang + '/unifier/get_unified_frames/?exclude_status=N&show_linked_entries_disabled=true&restrict_to_user='+window.USER_USERNAME;
const datatable = setup_datatable({
element: options.table,
url: ajaxURL,
columns: [
{ data: 'lemma' },
{ data: 'status' },
{ data: 'id' },
],
hidden_columns: [2],
selectEntryId: options.selectEntryId,
setup_hierarchy_marking: false,
disable_display_graphical_status: true,
});
datatable.on('click', 'tr.entry', function () {
const data = datatable.row(this).data();
if (!data) return;
const related = data.related === true;
$('.entry', options.table).removeClass('table-primary');
options.entrySelected(Number(data.id));
$(this).addClass('table-primary');
});
}
......@@ -8,7 +8,11 @@ function setup_frames_list(options) {
) ? gettext("tak") : gettext("nie");
}
const ajaxURL = can_see_assignees ? '/' + lang + '/unifier/get_unified_frames/' : '/' + lang + '/unifier/get_unified_frames/?exclude_status=N&restrict_to_user='+window.USER_USERNAME;
let ajaxURL = can_see_assignees ? '/' + lang + '/unifier/get_unified_frames/' : '/' + lang + '/unifier/get_unified_frames/?exclude_status=N&restrict_to_user='+window.USER_USERNAME;
if(options.include_unified_frame_status) {
ajaxURL += '?include_status=' + options.include_unified_frame_status;
}
const datatable = setup_datatable({
element: options.table,
......@@ -21,7 +25,8 @@ function setup_frames_list(options) {
],
hidden_columns: can_see_assignees ? [3] : [2,3],
selectEntryId: options.selectEntryId,
setup_hierarchy_marking: options.setupHierarchyMarking
setup_hierarchy_marking: options.setupHierarchyMarking,
setup_vertical_rel_marking: options.setup_vertical_rel_marking
});
datatable.on('click', 'tr.entry', function () {
const data = datatable.row(this).data();
......
......@@ -33,7 +33,12 @@
<li class="nav-item mr-1"><a
onclick='window.location.replace(window.currUnifiedFrameId ? "/pl/entries/hierarchy/?unified_frame_id="+window.currUnifiedFrameId : "/pl/entries/hierarchy")'
class="nav-link cursor-pointer">{% trans "Hierarchia" %}</a></li>
{% if is_superlexicograf %}
<li class="nav-item mr-1"><a href="{% url 'entries:lu_free' %}" class="nav-link">{% trans "Wolne jednostki" %}</a></li>
<li class="nav-item mr-1"><a
onclick='window.location.replace(window.currUnifiedFrameId ? "/pl/entries/vertical-relations/?unified_frame_id="+window.currUnifiedFrameId : "/pl/entries/vertical-relations")'
class="nav-link cursor-pointer">{% trans "Relacie poziome" %}</a></li>
{% endif %}
{% endif %}
<li class="nav-item dropdown mr-1">
<a class="nav-link dropdown-toggle mr-1" href="#" id="nav-filters" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
......
{% extends "entries_base.html" %}
{% load i18n %}
{% load static %}
{% block title %}{% trans "Hasła" %}{% endblock %}
{% block scripts %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'entries/css/unification_frames.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'common/css/role_colours.css' %}">
<script src="{% static 'entries/js/unification_entries_list.js' %}"></script>
<script src="{% static 'entries/js/unification_frames_list.js' %}"></script>
<script src="{% static 'entries/js/unification_entries_for_frames_list.js' %}"></script>
<script src="{% static 'entries/js/free_lus_list.js' %}"></script>
<script src="{% static 'entries/js/jquery-impromptu.min.js' %}"></script>
<script>
window.currUnifiedFrameId = {{ unified_frame_id|default:'null' }};
</script>
{% endblock %}
{% block modals %}
{{ block.super }}
<div id="lexical-unit-notes-template" class="d-none">{% include 'notes.html' %}</div>
{% endblock %}
{% extends "entries_base.html" %}
{% load i18n %}
{% load static %}
{% block title %}{% trans "Hasła" %}{% endblock %}
{% block scripts %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'entries/css/unification_frames.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'common/css/role_colours.css' %}">
<script src="{% static 'entries/js/unification_entries_list.js' %}"></script>
<script src="{% static 'entries/js/unification_frames_list.js' %}"></script>
<script src="{% static 'entries/js/unification_entries_for_frames_list.js' %}"></script>
<script src="{% static 'entries/js/jquery-impromptu.min.js' %}"></script>
<script>
window.currUnifiedFrameId = {{ unified_frame_id|default:'null' }};
</script>
{% endblock %}
{% block modals %}
{{ block.super }}
<div id="lexical-unit-notes-template" class="d-none">{% include 'notes.html' %}</div>
{% endblock %}
......@@ -2,7 +2,7 @@ from django.urls import path
from . import autocompletes, views
from .views import ajax_plWN_context_lookup, ajax_predefined_preferences, ajax_relations, ajax_synsets, ajax_roles, \
ajax_role_attributes, ajax_role_sub_attributes, ajax_free_slowal_frame_lookup
ajax_role_attributes, ajax_role_sub_attributes, ajax_free_slowal_frame_lookup, ajax_frame_opinions
app_name = 'entries'
......@@ -19,11 +19,15 @@ urlpatterns = [
path('change_show_linked_entries/', views.change_show_linked_entries, name='change_show_linked_entries'),
path('unification/', views.unification, name='unification'),
path('hierarchy/', views.hierarchy, name='hierarchy'),
path('vertical-relations/', views.vertical_relations, name='vertical-relations'),
path('lu_free/', views.lu_free, name='lu_free'),
path('autocomplete/', autocompletes.autocomplete, name='autocomplete'),
path('plWN_context_lookup/', ajax_plWN_context_lookup, name='plWN_context_lookup'),
path('predefined_preferences/', ajax_predefined_preferences, name='predefined_preferences'),
path('roles/', ajax_roles, name='roles'),
path('frame_opinions/', ajax_frame_opinions, name='frame_opinions'),
path('role_attributes/', ajax_role_attributes, name='role_attributes'),
path('role_sub_attributes/', ajax_role_sub_attributes, name='role_sub_attributes'),
path('relations/', ajax_relations, name='relations'),
......
......@@ -9,7 +9,7 @@ import simplejson
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.db.models import Prefetch, Q
from django.db.models import Prefetch, Q, Exists, OuterRef
from django.http import JsonResponse, QueryDict
from django.shortcuts import render
from django.template.context_processors import csrf
......@@ -21,7 +21,7 @@ from connections.models import Entry, Subentry, ArgumentConnection, RealisationD
from meanings.models import LexicalUnit
from syntax.models import NaturalLanguageDescription, Schema
from semantics.models import Frame, PredefinedSelectionalPreference, SelectivePreferenceRelations, SemanticRole, \
RoleAttribute, RoleSubAttribute, SelectionalPreferenceRelation
RoleAttribute, RoleSubAttribute, SelectionalPreferenceRelation, FrameOpinion
from common.decorators import ajax_required, ajax
from unifier.models import UnifiedFrame
......@@ -99,6 +99,53 @@ def hierarchy(request):
if "unified_frame_id" in request.GET:
unified_frame_id = request.GET.get("unified_frame_id")
user = request.user
return render(
request,
'hierarchy.html',
{
'is_vue_app': True,
'unified_frame_id': unified_frame_id,
'entries_form': EntryForm(),
'frames_form': FrameFormFactory.get_form(as_subform=False),
'schemata_form': SchemaFormFactory.get_form(as_subform=False),
'unified_frames_form': UnifiedFrameFormFactory.get_form(as_subform=False),
'is_superlexicograf': user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists()
},
)
@login_required
def lu_free(request):
unified_frame_id = None
if "unified_frame_id" in request.GET:
unified_frame_id = request.GET.get("unified_frame_id")
user = request.user
return render(
request,
'lu_free.html',
{
'is_vue_app': True,
'unified_frame_id': unified_frame_id,
'entries_form': EntryForm(),
'frames_form': FrameFormFactory.get_form(as_subform=False),
'schemata_form': SchemaFormFactory.get_form(as_subform=False),
'unified_frames_form': UnifiedFrameFormFactory.get_form(as_subform=False),
'is_superlexicograf': user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists()
},
)
@login_required
def vertical_relations(request):
unified_frame_id = None
if "unified_frame_id" in request.GET:
unified_frame_id = request.GET.get("unified_frame_id")
user = request.user
return render(
request,
'hierarchy.html',
......@@ -109,6 +156,7 @@ def hierarchy(request):
'frames_form': FrameFormFactory.get_form(as_subform=False),
'schemata_form': SchemaFormFactory.get_form(as_subform=False),
'unified_frames_form': UnifiedFrameFormFactory.get_form(as_subform=False),
'is_superlexicograf': user.groups.filter(name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME).exists()
},
)
......@@ -392,6 +440,18 @@ def get_scroller_params(POST_data):
# TODO restriction to >1 subentries for testing css – remove!!!
#from django.db.models import Count
def iter_lexical_units(lexical_units, has_unified_frame='false', exclude_status=None):
for lu in lexical_units:
lu._frame = lu._frames[0] if lu._frames and len(lu._frames) > 0 else None
if lu._frame is None or \
(not hasattr(lu._frame, 'slowal_frame_2_unified_frame') and has_unified_frame == 'true') or \
(exclude_status is not None and lu._frame.status == exclude_status):
continue
else:
yield lu
@ajax_required
@login_required
def get_entries(request):
......@@ -402,16 +462,21 @@ def get_entries(request):
assert(not errors_dict)
scroller_params = get_scroller_params(request.POST)
with_lexical_units = request.GET.get('with_lexical_units') == 'true'
without_frames = request.GET.get('without_frames') == 'true'
exclude_status = request.GET.get('exclude_status')
restrict_to_user = request.GET.get('restrict_to_user')
has_unified_frame = request.GET.get('has_unified_frame')
show_linked_entries_disabled = request.GET.get('show_linked_entries_disabled')
entries = get_filtered_objects(forms).filter(import_error=False)
# TODO restrictions for testing – remove!!!
#entries = entries.annotate(nsub=Count('subentries')).filter(nsub__gt=1)
#entries = entries.filter(subentries__schemata__opinion__key__in=('vul', 'col')).filter(status__key__in=('(S) gotowe', '(S) sprawdzone'))
#entries = entries.filter(subentries__schema_hooks__alternation=2)
if without_frames:
entries = entries.filter(name__in=(LexicalUnit.objects.filter(Q(frames__isnull=True) | Q(frames__in_building=True)).values("base")))
total = entries.count()
if scroller_params['filter']:
entries = entries.filter(name__startswith=scroller_params['filter'])
......@@ -423,7 +488,7 @@ def get_entries(request):
assert(not errors_dict)
linked_ids = set()
if request.session['show_linked_entries'] and has_unified_frame != 'true':
if request.session['show_linked_entries'] and has_unified_frame != 'true' and not show_linked_entries_disabled:
entries_linked = Entry.objects.filter(pk__in=(
Entry.objects
.filter(subentries__schema_hooks__argument_connections__schema_connections__subentry__entry__in=entries)
......@@ -468,7 +533,7 @@ def get_entries(request):
)
)
if exclude_status is not None:
entries = entries.filter(lexical_units__frames__status__iexact=exclude_status)
entries = entries.exclude(lexical_units__frames__status=exclude_status)
entries = entries.filter(lexical_units__frames__isnull=False)
if has_unified_frame == 'true':
entries = entries.filter(lexical_units__frames__slowal_frame_2_unified_frame__isnull=False)
......@@ -478,14 +543,6 @@ def get_entries(request):
status_names = STATUS()
POS_names = POS()
def iter_lexical_units(e):
for lu in e.lexical_units.all():
lu._frame = lu._frames[0] if lu._frames and len(lu._frames) > 0 else None
if lu._frame is None or (not hasattr(lu._frame, 'slowal_frame_2_unified_frame') and has_unified_frame == 'true'):
continue
else:
yield lu
result = {
'draw' : scroller_params['draw'],
'recordsTotal': total,
......@@ -501,15 +558,7 @@ def get_entries(request):
**(
{
'lexical_units': [
{
'pk': lu.pk,
'display': str(lu),
'assignee_username': (
lu._frame._assignments[0].user.username if lu._frame and lu._frame._assignments else None
),
'status': lu._frame.status if lu._frame else "",
'unified_frame_id': lu._frame.slowal_frame_2_unified_frame.unified_frame_id if lu._frame and hasattr(lu._frame, 'slowal_frame_2_unified_frame') else -1,
} for lu in iter_lexical_units(e)
lu2dict(lu) for lu in iter_lexical_units(e.lexical_units.all(), has_unified_frame, exclude_status=exclude_status)
]
}
if with_lexical_units else {}
......@@ -521,6 +570,18 @@ def get_entries(request):
return JsonResponse({})
def lu2dict(lu):
return {
'pk': lu.pk,
'display': str(lu),
'assignee_username': (
(lu._frame._assignments[0].user.username if lu._frame and lu._frame._assignments else None) if hasattr(lu, '_frame') else None
),
'status': lu._frame.status if hasattr(lu, '_frame') and lu._frame else "",
'unified_frame_id': lu._frame.slowal_frame_2_unified_frame.unified_frame_id if hasattr(lu, '_frame') and lu._frame and hasattr(lu._frame, 'slowal_frame_2_unified_frame') else -1,
}
def subentry2str(subentry):
ret = subentry.entry.name
if subentry.inherent_sie.name == 'true':
......@@ -639,6 +700,7 @@ def frame2dict(frame, entry_meanings):
'opinion_key' : frame.opinion.key,
'id' : frame.id,
'status' : frame.status,
'in_building' : frame.in_building,
'lexical_units' : [
{
'str' : lu.text_rep,
......@@ -723,10 +785,13 @@ def get_examples(entry):
if connection.lexical_unit:
lu_ids.add(connection.lexical_unit.id)
for hook in connection.schema_connections.all():
schema_ids.add(hook.schema.id);
schema_ids.add(hook.schema.id)
phrases.add('{}-{}-{}-{}'.format(hook.schema.id, hook.position.id, hook.phrase_type.id, hook.alternation - 1))
phrases_syntax.add('{}-{}-{}'.format(hook.schema.id, hook.position.id, hook.phrase_type.id))
positions.add('{}-{}'.format(hook.schema.id, hook.position.id))
for argument_connection in hook.argument_connections.all():
frame_ids.add(argument_connection.argument.frame.id)
argument_ids.add('{}-{}'.format(argument_connection.argument.frame.id, argument_connection.argument.id))
examples.append({
'id' : str(example.id),
'sentence' : example.sentence,
......@@ -831,7 +896,8 @@ def get_entry(request):
apply_filters = not simplejson.loads(request.POST['no_filters'])
filter_schemata = apply_filters and entry_form.cleaned_data['filter_schemata']
filter_frames = apply_filters and entry_form.cleaned_data['filter_frames']
lexical_unit = LexicalUnit.objects.get(pk=lu_id) if (lu_id := request.POST.get("lexical_unit_id")) else None
lu_id = request.POST.get("lexical_unit_id")
lexical_unit = LexicalUnit.objects.get(pk=lu_id) if lu_id else None
if filter_schemata:
schema_forms = []
# e.g. entry has schema that satisfies X & entry has schema that satisfies Y
......@@ -888,8 +954,9 @@ def get_entry(request):
schemata.append(schema2dict(schema, subentry.negativity, request.LANGUAGE_CODE))
if schemata:
all_schema_objects += list(schema_objects)
subentries.append({ 'str' : subentry2str(subentry), 'schemata' : schemata })
frame_objects = Frame.objects.filter(arguments__argument_connections__schema_connections__subentry__entry=entry).distinct()
subentries.append({'str': subentry2str(subentry), 'id': subentry.id, 'schemata': schemata})
# frame_objects = Frame.objects.filter(arguments__argument_connections__schema_connections__subentry__entry=entry).distinct()
frame_objects = Frame.objects.filter(lexical_units__entry=entry).distinct()
# filter out frames by frame properties
if filter_frames:
frame_objects = get_filtered_objects2(frame_forms, frame_objects)
......@@ -897,7 +964,8 @@ def get_entry(request):
frame_objects = frame_objects.filter(lexical_units=lexical_unit)
if local_frame_filter_form:
frame_objects = get_filtered_objects(local_frame_filter_form, frame_objects)
frames = [frame2dict(frame, entry.lexical_units.all()) for frame in frame_objects]
frames = [frame2dict(frame, entry.lexical_units.all()) for frame in filter(lambda frame: not frame.in_building, frame_objects.all())]
frames_in_building_process = [frame2dict(frame, entry.lexical_units.all()) for frame in filter(lambda frame: frame.in_building, frame_objects.all())]
alternations, realisation_phrases, realisation_descriptions = get_alternations(all_schema_objects, frame_objects)
examples = get_examples(entry)
unified_frame = None
......@@ -913,7 +981,13 @@ def get_entry(request):
request.session['last_visited'].insert(0, (entry.name, entry.id))
request.session['last_visited'] = request.session['last_visited'][:(MAX_LAST_VISITED + 1)]
request.session.modified = True
return JsonResponse({ 'subentries' : subentries, 'frames' : frames, 'alternations' : alternations, 'realisation_phrases' : realisation_phrases, 'realisation_descriptions' : realisation_descriptions, 'examples' : examples, 'unified_frame': unified_frame, 'last_visited' : request.session['last_visited'] })
frame_lexical_unit_ids = set(map(lambda lu: lu['id'], (lu for iterable in frames for lu in iterable['lexical_units'])))
frame_lexical_unit_ids = frame_lexical_unit_ids | set(map(lambda lu: lu['id'], (lu for iterable in frames_in_building_process for lu in iterable['lexical_units'])))
free_lexical_units = [lu2dict(lu) for lu in filter(lambda lu: lu.id not in frame_lexical_unit_ids, LexicalUnit.objects.filter(Q(base=entry.name) | Q(base__startswith=entry.name+' ')).all())]
return JsonResponse({'free_lexical_units': free_lexical_units, 'subentries' : subentries, 'frames' : frames, 'frames_in_building_process': frames_in_building_process, 'alternations' : alternations, 'realisation_phrases' : realisation_phrases, 'realisation_descriptions' : realisation_descriptions, 'examples' : examples, 'unified_frame': unified_frame, 'last_visited' : request.session['last_visited'] })
return JsonResponse({})
......@@ -1028,6 +1102,15 @@ def ajax_roles(request):
return context
@ajax(method='get', encode_result=True)
def ajax_frame_opinions(request):
opinions = []
for opinion in FrameOpinion.objects.exclude(key='unk').filter(frame__isnull=False).distinct().order_by('priority'):
opinions.append({"id": opinion.id, "key": str(FRAME_OPINION()[opinion.key])})
return {'result': opinions}
@ajax(method='get', encode_result=True)
def ajax_role_attributes(request):
roleAttributes = []
......@@ -1112,3 +1195,5 @@ def ajax_free_slowal_frame_lookup(request, term):
lexical_unit_str.append(str(lexical_unit))
return {'result': lexical_unit_str}
from django.apps import AppConfig
class FreeLusConfig(AppConfig):
name = 'freelus'
import json
from django.test import Client
from django.test import RequestFactory, TestCase
# initialize the APIClient app
from connections.models import Entry, POS, Status, ExampleConnection
from examples.models import Example, ExampleOpinion, ExampleSource
from freelus.views import create_new_slowal_frame, add_argument_to_frame, attach_examples_to_frame
from meanings.models import LexicalUnit, Synset
from semantics.models import FrameOpinion, Frame, SemanticRole, ArgumentRole
client = Client()
class ApiTest(TestCase):
def tearDown(self) -> None:
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
self.factory = RequestFactory()
FrameOpinion.objects.create(key='unk', priority=1)
self.synset = Synset.objects.create(id=1, definition='definition')
self.lu = LexicalUnit.objects.create(base='test', sense=1, pos='A', synset=self.synset)
pos = POS.objects.create(tag='test_tag')
status = Status.objects.create(key='status_key', priority=1)
self.entry = Entry.objects.create(name='test', status=status, pos=pos)
self.semantic_role = SemanticRole.objects.create(role='test_role', priority=1)
self.argument_role = ArgumentRole.objects.create(role=self.semantic_role)
example_opinion = ExampleOpinion.objects.create(key='key', priority=1)
example_source = ExampleSource.objects.create(key='key', priority=1)
self.example = Example.objects.create(entry=self.entry, sentence='test sentence', opinion=example_opinion, source=example_source)
def test_create_new_slowal_frame(self):
request = self.factory.post("/pl/freelus/create_new_slowal_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'entry_id': self.entry.pk,
'lu_ids': json.dumps([self.lu.pk], separators=(',', ':'))})
response = create_new_slowal_frame(request)
self.assertEqual(1, Frame.objects.all().count())
frame = Frame.objects.all()[0]
self.assertEqual(0, len(frame.arguments.all()))
def test_add_argument_to_frame(self):
request = self.factory.post("/pl/freelus/create_new_slowal_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'entry_id': self.entry.pk,
'lu_ids': json.dumps([self.lu.pk], separators=(',', ':'))})
response = create_new_slowal_frame(request)
frame = Frame.objects.all()[0]
request = self.factory.post("/pl/freelus/add_argument_to_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'frame_id': frame.pk,
'role_id': self.argument_role.pk,
'role_type': 1})
response = add_argument_to_frame(request)
frame = Frame.objects.all()[0]
self.assertEqual(1, len(frame.arguments.all()))
def test_attach_examples_to_frame(self):
request = self.factory.post("/pl/freelus/create_new_slowal_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'entry_id': self.entry.pk,
'lu_ids': json.dumps([self.lu.pk], separators=(',', ':'))})
response = create_new_slowal_frame(request)
frame = Frame.objects.all()[0]
request = self.factory.post("/pl/freelus/add_argument_to_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'frame_id': frame.pk,
'role_id': self.argument_role.pk,
'role_type': 1})
response = add_argument_to_frame(request)
argument = frame.arguments.all()[0]
request = self.factory.post("/pl/freelus/attach_examples_to_frame/",
HTTP_X_REQUESTED_WITH='XMLHttpRequest',
data={'frame_id': frame.pk,
'selected_lus': json.dumps([self.lu.pk], separators=(',', ':')),
'example_ids': json.dumps([self.example.pk], separators=(',', ':'))})
response = attach_examples_to_frame(request)
examples = ExampleConnection.objects.filter(lexical_unit=self.lu)
self.assertEqual(1, examples.count())