Handle new data structure

This commit is contained in:
Patrick Jentsch 2019-11-27 09:36:27 +01:00
parent 9f42b9b99d
commit d63b5ec266

View File

@ -103,78 +103,60 @@
<script>
var loadingModal;
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener("DOMContentLoaded", function() {
loadingModal = M.Modal.init(document.getElementById("loading-modal"),
{"dismissible": false});
loadingModal.open();
socket.emit("request_corpus_analysis", {{ corpus_id }});
});
socket.emit('request_corpus_analysis', {{ corpus_id }});
socket.on('request_corpus_analysis', function(msg) {
if (msg === '[201]: Created') {loadingModal.close();}
socket.on("request_corpus_analysis", function(msg) {
if (msg === "[201]: Created") {loadingModal.close();}
});
var queryFormElement = document.getElementById("query-form");
var queryFormSubmitElement = document.getElementById("query-form-submit");
var queryResultsElement = document.getElementById("query-results");
queryFormSubmitElement.addEventListener('click', function(event) {
queryFormSubmitElement.addEventListener("click", function(event) {
event.preventDefault();
let formData = new FormData(queryFormElement);
let queryData = {'context': formData.get('context'),
'hits_per_page': formData.get('hits_per_page'),
'query': formData.get('query')};
let queryData = {"context": formData.get("context"),
"hits_per_page": formData.get("hits_per_page"),
"query": formData.get("query")};
socket.emit('corpus_analysis', queryData);
M.toast({html: 'Query has been sent!'});
});
socket.on('corpus_analysis', function(matches) {
if (matches === null) {
M.toast({html: 'Query has no results!'});
} else {
console.log(matches);
let htmlString = "";
for (let match of Object.values(matches)) {
niceMatch = matchInfo(match);
htmlString += `<tr><td>${niceMatch["text_title"]}</td><td>${niceMatch["left"]}</td><td>${niceMatch["match"]}</td><td>${niceMatch["right"]}</td></tr>`;
socket.on('corpus_analysis', function(result) {
console.log(result['matches']);
var htmlString;
var token;
for (let match of result['matches']) {
htmlString = `<tr class="match">`;
htmlString += `<td class="left-context">`;
for (cpos of match['lc']) {
token = result['cpos_lookup'][cpos];
htmlString += `<span data-cpos="${cpos}">${token['word']}</span>`;
}
queryResultsElement.innerHTML = htmlString;
htmlString += `</td>`;
htmlString += `<td class="hit">`;
for (cpos of match["hit"]) {
token = result['cpos_lookup'][cpos];
htmlString += `<span data-cpos="${cpos}">${token['word']}</span>`;
}
htmlString += `</td>`;
htmlString += `<td class="right-context">`;
for (cpos of match['rc']) {
token = result['cpos_lookup'][cpos];
htmlString += `<span data-cpos="${cpos}">${token['word']}</span>`;
}
htmlString += `</td>`;
htmlString += `</tr>`;
queryResultsElement.insertAdjacentHTML("beforeend", htmlString);
}
});
function matchInfo(match) {
var niceMatch = {"left": "", "match": "", "right": ""};
var tmp = Object.values(Object.values(match["match_cpos"])[0])[0];
niceMatch["text_author"] = tmp["text_author"][1];
niceMatch["text_title"] = tmp["text_title"][1];
niceMatch["publishing_year"] = tmp["text_publishing_year"][1];
for (let token of Object.values(match['left_context_cpos'])) {
tmp = Object.values(token)[0];
if (tmp["simple_pos"] != "PUNCT") {niceMatch["left"] += " ";}
niceMatch["left"] += '<span class="token">'
+ '<span class="word">' + tmp["word"] + '</span>'
+ '<span class="pos">' + tmp["pos"] + '</span>'
+ '<span class="lemma">' + tmp["lemma"] + '</span>'
+ '</span>';
}
for (let token of Object.values(match['match_cpos'])) {
tmp = Object.values(token)[0];
if (tmp["simple_pos"] != "PUNCT") {niceMatch["match"] += " ";}
niceMatch["match"] += '<span class="token" data-cpos="">'
+ '<span class="word">' + tmp["word"] + '</span>'
+ '<span class="pos">' + tmp["pos"] + '</span>'
+ '<span class="lemma">' + tmp["lemma"] + '</span>'
+ '</span>';
}
for (let token of Object.values(match['right_context_cpos'])) {
tmp = Object.values(token)[0];
if (tmp["simple_pos"] != "PUNCT") {niceMatch["right"] += " ";}
niceMatch["right"] += '<span class="token">'
+ '<span class="word">' + tmp["word"] + '</span>'
+ '<span class="pos">' + tmp["pos"] + '</span>'
+ '<span class="lemma">' + tmp["lemma"] + '</span>'
+ '</span>';
}
return niceMatch
}
</script>
{% endblock %}