Add basic style for expert view

This commit is contained in:
Patrick Jentsch 2019-11-27 15:56:39 +01:00
parent 54f3036d89
commit 49311f58f0
2 changed files with 61 additions and 16 deletions

View File

@ -75,10 +75,11 @@ def corpus_analysis(message):
cpos_list = list(set(cpos_list)) cpos_list = list(set(cpos_list))
pos_list = client.cl_cpos2str('{}.pos'.format(corpus), cpos_list) pos_list = client.cl_cpos2str('{}.pos'.format(corpus), cpos_list)
simple_pos_list = client.cl_cpos2str('{}.simple_pos'.format(corpus), cpos_list) simple_pos_list = client.cl_cpos2str('{}.simple_pos'.format(corpus), cpos_list)
text_id_list = client.cl_cpos2struc('{}.text_title'.format(corpus), cpos_list) s_id_list = client.cl_cpos2struc('{}.s'.format(corpus), cpos_list)
text_id_list = client.cl_cpos2struc('{}.text'.format(corpus), cpos_list)
word_list = client.cl_cpos2str('{}.word'.format(corpus), cpos_list) word_list = client.cl_cpos2str('{}.word'.format(corpus), cpos_list)
for cpos, pos, simple_pos, text_id, word in zip(cpos_list, pos_list, simple_pos_list, text_id_list, word_list): for cpos, pos, simple_pos, s_id, text_id, word in zip(cpos_list, pos_list, simple_pos_list, s_id_list, text_id_list, word_list):
data['cpos_lookup'][cpos] = {'pos': pos, 'simple_pos': simple_pos, 'text_id': text_id, 'word': word} data['cpos_lookup'][cpos] = {'pos': pos, 'simple_pos': simple_pos, 's_id': s_id, 'text_id': text_id, 'word': word}
text_author_list = client.cl_struc2str('{}.text_author'.format(corpus), text_id_list) text_author_list = client.cl_struc2str('{}.text_author'.format(corpus), text_id_list)
text_publishing_year_list = client.cl_struc2str('{}.text_publishing_year'.format(corpus), text_id_list) text_publishing_year_list = client.cl_struc2str('{}.text_publishing_year'.format(corpus), text_id_list)
text_title_list = client.cl_struc2str('{}.text_title'.format(corpus), text_id_list) text_title_list = client.cl_struc2str('{}.text_title'.format(corpus), text_id_list)

View File

@ -1,6 +1,21 @@
{% extends "full_width.html.j2" %} {% extends "full_width.html.j2" %}
{% block page_content %} {% block page_content %}
<style>
.token-box {
padding: 5px;
margin: 2px 0;
border-radius: 5px;
background-color: #009688;
display: inline-block;
color: white;
}
.token-box:hover {
background-color: #00bfa5;
cursor: pointer;
}
</style>
<div class="col s12 m3 sticky"> <div class="col s12 m3 sticky">
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
@ -36,6 +51,13 @@
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div class="switch">
<label>
Expert Mode
<input type="checkbox" id="expert-mode-switch">
<span class="lever"></span>
</label>
</div>
</form> </form>
</div> </div>
</div> </div>
@ -69,10 +91,10 @@
<table class="highlight"> <table class="highlight">
<thead> <thead>
<tr> <tr>
<th>Title</th> <th style="width: 5%">Title</th>
<th>Left context</th> <th style="width: 25%">Left context</th>
<th>Match</th> <th style="width: 45%">Match</th>
<th>Right Context</th> <th style="width: 25%">Right Context</th>
</tr> </tr>
</thead> </thead>
<tbody id="query-results"></tbody> <tbody id="query-results"></tbody>
@ -103,7 +125,6 @@
<script> <script>
var loadingModal; var loadingModal;
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
loadingModal = M.Modal.init(document.getElementById("loading-modal"), loadingModal = M.Modal.init(document.getElementById("loading-modal"),
{"dismissible": false}); {"dismissible": false});
@ -115,10 +136,23 @@
if (msg === "[201]: Created") {loadingModal.close();} if (msg === "[201]: Created") {loadingModal.close();}
}); });
var expertModeSwitchElement = document.getElementById("expert-mode-switch");
var tokenElements = [];
expertModeSwitchElement.addEventListener('change', function(event) {
if (event.target.checked) {
tokenElements.forEach(function(tokenElement) {
tokenElement.classList.add("token-box");
});
} else {
tokenElements.forEach(function(tokenElement) {
tokenElement.classList.remove("token-box");
});
}
})
var queryFormElement = document.getElementById("query-form"); var queryFormElement = document.getElementById("query-form");
var queryFormSubmitElement = document.getElementById("query-form-submit"); var queryFormSubmitElement = document.getElementById("query-form-submit");
var queryResultsElement = document.getElementById("query-results"); var queryResultsElement = document.getElementById("query-results");
queryFormSubmitElement.addEventListener("click", function(event) { queryFormSubmitElement.addEventListener("click", function(event) {
event.preventDefault(); event.preventDefault();
let formData = new FormData(queryFormElement); let formData = new FormData(queryFormElement);
@ -132,6 +166,7 @@
socket.on("corpus_analysis", function(result) { socket.on("corpus_analysis", function(result) {
console.log(result); console.log(result);
var htmlString = ""; var htmlString = "";
var textTitles;
var token; var token;
if (result['matches'].length === 0) { if (result['matches'].length === 0) {
@ -140,25 +175,32 @@
for (let match of result['matches']) { for (let match of result['matches']) {
htmlString += `<tr class="match">`; htmlString += `<tr class="match">`;
htmlString += `<td class="title"></td>` htmlString += `<td class="title">`;
textTitles = new Set();
for (cpos of match["hit"]) {
token = result["cpos_lookup"][cpos];
textTitles.add(result["text_loopup"][token["text_id"]]["title"]);
}
htmlString += [...textTitles].join(",");
htmlString += `</td>`;
htmlString += `<td class="left-context">`; htmlString += `<td class="left-context">`;
for (cpos of match["lc"]) { for (cpos of match["lc"]) {
token = result["cpos_lookup"][cpos]; token = result["cpos_lookup"][cpos];
htmlString += token["simple_pos"] != "PUNCT" ? " " : ""; htmlString += " ";
htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`; htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`;
} }
htmlString += `</td>`; htmlString += `</td>`;
htmlString += `<td class="hit">`; htmlString += `<td class="hit">`;
for (cpos of match["hit"]) { for (cpos of match["hit"]) {
token = result["cpos_lookup"][cpos]; token = result["cpos_lookup"][cpos];
htmlString += token["simple_pos"] != "PUNCT" ? " " : ""; htmlString += " ";
htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`; htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`;
} }
htmlString += `</td>`; htmlString += `</td>`;
htmlString += `<td class="right-context">`; htmlString += `<td class="right-context">`;
for (cpos of match["rc"]) { for (cpos of match["rc"]) {
token = result["cpos_lookup"][cpos]; token = result["cpos_lookup"][cpos];
htmlString += token["simple_pos"] != "PUNCT" ? " " : ""; htmlString += " ";
htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`; htmlString += `<span class="token" data-cpos="${cpos}">${token["word"]}</span>`;
} }
htmlString += `</td>`; htmlString += `</td>`;
@ -167,10 +209,12 @@
queryResultsElement.innerHTML = htmlString; queryResultsElement.innerHTML = htmlString;
queryResultsElement.querySelectorAll(".token").forEach(function(tokenElement) { tokenElements = queryResultsElement.querySelectorAll(".token");
tokenElements.forEach(function(tokenElement) {
tokenElement.addEventListener("click", function(event) { tokenElement.addEventListener("click", function(event) {
let token = result["cpos_lookup"][tokenElement.dataset.cpos]; if (!expertModeSwitchElement.checked) {return;}
let text = result["text_loopup"][token["text_id"]]; var token = result["cpos_lookup"][tokenElement.dataset.cpos];
var text = result["text_loopup"][token["text_id"]];
alert(`${token["word"]} // ${token["pos"]} // ${token["simple_pos"]} // ${text["title"]}`); alert(`${token["word"]} // ${token["pos"]} // ${token["simple_pos"]} // ${text["title"]}`);
}); });
}); });