Add first query result function

This commit is contained in:
Stephan Porada 2019-11-18 16:07:07 +01:00
parent 5c8ca4e22d
commit 0ca4bf6030
4 changed files with 19 additions and 191 deletions

View File

@ -51,7 +51,7 @@ class CQiWrapper(CQiClient):
self.attr_strings['positional_attrs'][p_attr] = (self.corpus_name
+ '.'
+ p_attr)
for struct_attr in struct_attrs[:-1]:
for struct_attr in struct_attrs[2:-1]:
self.attr_strings['struct_attrs'][struct_attr] = (self.corpus_name
+ '.'
+ struct_attr)
@ -64,7 +64,6 @@ class CQiWrapper(CQiClient):
self.__create_attribute_strings()
logger.warning('{} does exist.'.format(corpus_name))
else:
self.disconnect()
logger.warning('{} does not exist.'.format(corpus_name))
def disconnect(self):
@ -126,7 +125,6 @@ class CQiWrapper(CQiClient):
self.nr_matches = min(result_len, self.nr_matches)
if self.nr_matches == 0:
logger.warning('Query resulted in 0 matches.')
self.disconnect
return None
else:
# Get match cpos boundries
@ -227,11 +225,14 @@ class CQiWrapper(CQiClient):
all_cpos)
cpos_infos[p_attr_key] = match_str
elif attr_dict == 'struct_attrs':
for struct_attr_key in self.attr_strings[attr_dict].keys():
struct_entry = self.cl_cpos2struc(self.attr_strings['struct_attrs'][self.meta_struct_element],
all_cpos)
match_str = self.cl_struc2str(self.attr_strings[attr_dict][struct_attr_key], struct_entry)
cpos_infos[struct_attr_key] = match_str
# for struct_attr_key in self.attr_strings[attr_dict].keys():
# logger.warning('HIER:' + struct_attr_key + " " + " " + self.attr_strings[attr_dict][struct_attr_key])
# struct_entry = self.cl_cpos2struc(self.attr_strings['struct_attrs'][self.meta_struct_element],
# all_cpos)
# logger.warning(struct_entry)
# match_str = self.cl_struc2str(self.attr_strings[attr_dict][struct_attr_key], struct_entry)
# cpos_infos[struct_attr_key] = match_str
pass
tmp_list = []
attr_key_list = []
for key in cpos_infos.keys():

View File

@ -37,10 +37,10 @@ def init_corpus_analysis(corpus_id):
while corpus.status != 'analysing':
db.session.refresh(corpus)
socketio.sleep(3)
analysis_clients[request.sid] = CQiClient(
analysis_clients[request.sid] = CQiWrapper(
host='{}_analysis_container{}'.format(corpus.creator.username,
corpus.id))
analysis_clients[request.sid].ctrl_connect('opaque', 'opaque')
analysis_clients[request.sid].connect()
socketio.emit('init_corpus_analysis', 'Ready', room=request.sid)
socketio.start_background_task(observe_corpus_analysis_connection,
current_app._get_current_object(),
@ -58,41 +58,10 @@ def recv_query(message):
""" Prepare and execute a query """
corpus_name = 'CORPUS'
query = message['query']
query_subcorpus = 'Results'
analysis_client.cqp_query(corpus, query_subcorpus, query)
""" Evaluate query results """
match_corpus = '{}:{}'.format(corpus, query_subcorpus)
match_num = min(int(message['hits_per_page']) - 1,
analysis_client.cqp_subcorpus_size(match_corpus))
if match_num == 0:
print('No matches found.')
exit()
if not analysis_client.cqp_subcorpus_has_field(match_corpus, CONST_FIELD_MATCH):
print('Error.')
exit()
if not analysis_client.cqp_subcorpus_has_field(match_corpus, CONST_FIELD_MATCHEND):
print('Error')
exit()
match_boundaries = zip(analysis_client.cqp_dump_subcorpus(match_corpus, CONST_FIELD_MATCH, 0, match_num - 1),
analysis_client.cqp_dump_subcorpus(match_corpus, CONST_FIELD_MATCHEND, 0, match_num - 1))
matches = []
for match_start, match_end in match_boundaries:
matches.append({'cpos_list': list(range(match_start, match_end + 1))})
cpos_list = []
for match in matches:
cpos_list = cpos_list + match['cpos_list']
cpos_list = list(set(cpos_list))
pos_list = analysis_client.cl_cpos2str('{}.pos'.format(corpus), cpos_list)
word_list = analysis_client.cl_cpos2str('{}.word'.format(corpus), cpos_list)
foo = {}
for cpos, pos, word in zip(cpos_list, pos_list, word_list):
foo[cpos] = {'pos': pos, 'word': word}
for match in matches:
match['pos_list'] = [foo[cpos]['pos'] for cpos in match['cpos_list']]
match['word_list'] = [foo[cpos]['word'] for cpos in match['cpos_list']]
match.pop('cpos_list', None)
logger.warning(matches)
socketio.emit('query', matches, room=request.sid)
analysis_client.select_corpus(corpus_name)
analysis_client.query_subcorpus(query)
results = analysis_client.show_query_results(result_len=2)
socketio.emit('query', results, room=request.sid)
def observe_corpus_analysis_connection(app, corpus_id, session_id):
@ -101,7 +70,7 @@ def observe_corpus_analysis_connection(app, corpus_id, session_id):
socketio.sleep(3)
analysis_client = analysis_clients.pop(session_id, None)
if analysis_client is not None:
analysis_client.ctrl_bye()
analysis_client.disconnect()
analysis_sessions[corpus_id].remove(session_id)
if not analysis_sessions[corpus_id]:
analysis_sessions.pop(corpus_id, None)

View File

@ -117,8 +117,9 @@
});
socket.on('query', function(results) {
queryResultsElement.innerHTML = '';
for (let result of results) {
queryResultsElement.innerHTML += '<p>' + result['word_list'] + '</p>';
for (let key in results) {
console.log(results[key]);
queryResultsElement.innerHTML += '<p>' + results[key]['match_cpos_list'] + '</p>';
}
});
</script>

View File

@ -1,143 +0,0 @@
{% extends "full_width.html.j2" %}
{% block page_content %}
<div class="col s12 m3 l3 sticky">
<a class="waves-effect waves-light btn" href="{{ url_for('corpora.corpus', corpus_id=corpus_id) }}"><i class="material-icons left">arrow_back</i>Back to corpus overview</a>
<div class="card">
<form id="query_form" method="POST">
<div class="card-content">
{{ form.hidden_tag() }}
<span class="card-title">Query and analysis</span>
<br>
<div class="input-field">
{{ form.query(class='materialize-textarea') }}
{{ form.query.label }}
{% for error in form.query.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="right-align">
<button id="sent_query" class="btn" type="submit" name="action">Start Query
<i class="material-icons left">search</i>
</button>
</div>
<br>
<span class="card-title">Help</span>
<ul>
<li>
<a href="http://cwb.sourceforge.net/files/CQP_Tutorial/">
CQP Query Language Tutorial</a>
</li>
</ul>
<br>
<span class="card-title">Options</span>
<br>
<div class="input-field">
<i class="material-icons prefix">format_list_numbered</i>
{{ form.hits_per_page() }}
{{ form.hits_per_page.label }}
{% for error in form.hits_per_page.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<br>
<div class="input-field">
<i class="material-icons prefix">short_text</i>
{{ form.context() }}
{{ form.context.label }}
{% for error in form.context.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<br>
</form>
<span class="card-title">Download Results</span>
<p>Downlaod all results of the current query as csv, excel or json file.</p>
<form method="POST">
{{ form.hidden_tag() }}
<div class="input-field">
<i class="material-icons prefix">insert_drive_file</i>
{{ dl_form.file_type() }}
{{ dl_form.file_type.label }}
{% for error in dl_form.file_type.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
<div class="right-align">
<button class="btn" type="submit" name="action">Download
<i class="material-icons left">file_download</i>
</button>
</div>
</form>
</div>
</div>
</div>
<script>
socket.on('query_results', function(json_results) {
console.log('Results recieved');
console.log(json_results);
});
</script>
<div class="col s12 m9 l9">
<div class="card">
<div class="card-content">
<span class="card-title">Query Results</span>
<p id="query-results"></p>
</div>
</div>
</div>
<script>
let queryData = document.forms['query_form'].elements;
var queryBtn = document.getElementById('sent_query');
queryBtn.addEventListener('click', function(event) {
event.preventDefault();
sendQueryData();
});
function sendQueryData() {
var query_data = {};
for (element of queryData) {
if (!element.value || element.name == 'csrf_token' || !element.id) {
continue;
}
query_data[element.id] = element.value
}
console.log(query_data)
socket.emit('query_event', query_data);
M.toast({html: 'Query has been sent'});
console.log('Query data has been sent!');
}
</script>
<div id="loading-modal" class="modal no-autoinit">
<div class="modal-content">
<h4>Waiting for analysis software</h4>
<div class="preloader-wrapper big active">
<div class="spinner-layer spinner-blue-only">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</div>
<script>
var loadingModal;
document.addEventListener('DOMContentLoaded', function() {
loadingModal = M.Modal.init(document.getElementById("loading-modal"), {"dismissible": false});
loadingModal.open();
});
socket.emit('init_corpus_analysis', {{ corpus_id }});
socket.on('init_corpus_analysis', function(msg) {
if (msg === 'Ready') {loadingModal.close();}
});
</script>
{% endblock %}