From d23656ae808b2f2d6f5459b80de2ab25f870164c Mon Sep 17 00:00:00 2001 From: Stephan Porada Date: Mon, 25 Nov 2019 11:51:56 +0100 Subject: [PATCH] Some fixes --- app/corpora/CQiWrapper/CQiWrapper.py | 30 +++++++-------- app/templates/corpora/analyse_corpus.html.j2 | 40 +++++++++++--------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/app/corpora/CQiWrapper/CQiWrapper.py b/app/corpora/CQiWrapper/CQiWrapper.py index ff5773f1..06457ad3 100644 --- a/app/corpora/CQiWrapper/CQiWrapper.py +++ b/app/corpora/CQiWrapper/CQiWrapper.py @@ -144,28 +144,28 @@ class CQiWrapper(CQiClient): # Save them as list into on match entry at serial number 'i' ordered_matches = collections.OrderedDict() for i, match_pair in enumerate(match_boundaries): - ordered_matches[i] = ({'match_cpos_list': + ordered_matches[i] = ({'match_cpos': list(range(match_pair[0], match_pair[1] + 1))}) # Saves cpos form all match entries into one list all_cpos_list = [] for key in ordered_matches.keys(): - all_cpos_list += ordered_matches[key]['match_cpos_list'] + all_cpos_list += ordered_matches[key]['match_cpos'] # Saves all cpos from before and after context into the list: # all_context_cpos_list all_context_cpos_list = [] for key in ordered_matches.keys(): - cpos_list = ordered_matches[key]['match_cpos_list'] + cpos_list = ordered_matches[key]['match_cpos'] before_index = max([0, cpos_list[0] - self.context_len]) after_index = min([self.corpus_max_len, cpos_list[-1] + self.context_len]) - ordered_matches[key]['context_before_cpos_list'] = list(range(before_index, + ordered_matches[key]['left_context_cpos'] = list(range(before_index, cpos_list[0])) - ordered_matches[key]['context_after_cpos_list'] = list(range(cpos_list[-1] + 1, + ordered_matches[key]['right_context_cpos'] = list(range(cpos_list[-1] + 1, after_index + 1)) - all_context_cpos_list += ordered_matches[key]['context_before_cpos_list'] - all_context_cpos_list += ordered_matches[key]['context_after_cpos_list'] + all_context_cpos_list += ordered_matches[key]['left_context_cpos'] + all_context_cpos_list += ordered_matches[key]['right_context_cpos'] # Combines all_cpos_list with all_context_cpos_list as a sorted set all_cpos_list += all_context_cpos_list all_cpos_list = sorted(list(set(all_cpos_list))) @@ -178,37 +178,37 @@ class CQiWrapper(CQiClient): # loops over cpos in cpos_list which holds all match cpos # Replaces one cpos with the corresponding cpos information created # by self.get_cpos_infos(all_cpos_list) - cpos_list = ordered_matches[key]['match_cpos_list'] + cpos_list = ordered_matches[key]['match_cpos'] infos = [] for cpos in cpos_list: info = {cpos: all_cpos_infos.get(cpos)} infos.append(info) - ordered_matches[key]['match_cpos_list'] = infos + ordered_matches[key]['match_cpos'] = infos try: - # loops over cpos in ordered_matches[key]['context_before_cpos_list'] + # loops over cpos in ordered_matches[key]['left_context_cpos'] # which holds all cpos of the before context # Replaces one cpos with the corresponding cpos information created # by self.get_cpos_infos(all_cpos_list) before_context_infos = [] - for context_before_cpos in ordered_matches[key]['context_before_cpos_list']: + for context_before_cpos in ordered_matches[key]['left_context_cpos']: before_context_info = {context_before_cpos: all_cpos_infos.get(context_before_cpos)} before_context_infos.append(before_context_info) - ordered_matches[key]['context_before_cpos_list'] = before_context_infos + ordered_matches[key]['left_context_cpos'] = before_context_infos except UnboundLocalError: logger.warning('Context before cpos list is empty.') pass try: - # loops over cpos in ordered_matches[key]['context_after_cpos_list'] + # loops over cpos in ordered_matches[key]['right_context_cpos'] # which holds all cpos of the before context # Replaces one cpos with the corresponding cpos information created # by self.get_cpos_infos(all_cpos_list) after_context_infos = [] - for context_after_cpos in ordered_matches[key]['context_after_cpos_list']: + for context_after_cpos in ordered_matches[key]['right_context_cpos']: after_context_info = {context_after_cpos: all_cpos_infos.get(context_after_cpos)} after_context_infos.append(after_context_info) - ordered_matches[key]['context_after_cpos_list'] = after_context_infos + ordered_matches[key]['right_context_cpos'] = after_context_infos except UnboundLocalError: logger.warning('Context after cpos list is empty.') pass diff --git a/app/templates/corpora/analyse_corpus.html.j2 b/app/templates/corpora/analyse_corpus.html.j2 index a1fc48fd..d3eb24a4 100644 --- a/app/templates/corpora/analyse_corpus.html.j2 +++ b/app/templates/corpora/analyse_corpus.html.j2 @@ -119,33 +119,39 @@ if (results === null) { M.toast({html: 'Query has no results!'}); } else { - html_txt = ''; + console.log(results); + html_txt = '
Left contextMatchRight Context
'; for (let [key, hit] of Object.entries(results)) { - var left_context = hit['context_before_cpos_list'] - var match = hit['match_cpos_list'] - var right_context = hit['context_after_cpos_list'] - var l_text = getResultInfos(left_context) - var m_text = getResultInfos(match) - var r_text = getResultInfos(right_context) - html_txt += ''; + var left_context = hit['left_context_cpos'] + var match = hit['match_cpos'] + var right_context = hit['right_context_cpos'] + var l_text = getResultInfos(left_context, 'word') + var m_text = getResultInfos(match, 'word') + var r_text = getResultInfos(right_context, 'word') + var match_source = getResultInfos(match, 'text_title', 1) + html_txt += ''; l_text = ''; m_text = ''; } html_txt += '
TitleLeft contextMatchRight Context
' + l_text + '' + m_text + '' + r_text + '
' + match_source + '' + l_text + '' + m_text + '' + r_text + '
'; - console.log(html_txt); queryResultsElement.innerHTML = html_txt; } }); - function getResultInfos(matchObject) { - infos = ''; - for (var key in matchObject) { - var token = matchObject[key]; - for (var key in token) { - infos += token[key]['word'] + ' '; + function getResultInfos(matchObject, info_key, slice) { + var infos = []; + for (let [infoKey, infoValue] of Object.entries(matchObject)) { + var token = infoValue; + for (let key in token) { + infos.push(token[key][info_key]); } - var infos = infos; } - return infos + if (slice) { + console.log(infos); + var infos = infos[slice]; + return infos; + } else { + return infos; + } } {% endblock %}