From 572fdf3a00feedd85bc5b1f9b7ef7b5f301f5b6a Mon Sep 17 00:00:00 2001 From: Inga Kirschnick Date: Tue, 11 Jul 2023 13:40:20 +0200 Subject: [PATCH] Small updates custom stopword list --- .../js/CorpusAnalysis/CorpusAnalysisApp.js | 62 +++++++------------ app/templates/corpora/analysis.html.j2 | 4 +- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js index a30af989..7f0462d5 100644 --- a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js +++ b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js @@ -33,10 +33,8 @@ class CorpusAnalysisApp { .then((response) => { response.json() .then((json) => { - for (let [key, value] of Object.entries(json)) { - this.data.originalStopwords[key] = value; - } - this.data.stopwords = json; + this.data.originalStopwords = structuredClone(json); + this.data.stopwords = structuredClone(json); resolve(this.data.stopwords); }) .catch((error) => { @@ -92,11 +90,7 @@ class CorpusAnalysisApp { let frequenciesStopwordSettingModal = document.querySelector('#frequencies-stopwords-setting-modal'); let frequenciesStopwordSettingModalButton = document.querySelector('#frequencies-stopwords-setting-modal-button'); frequenciesStopwordSettingModalButton.addEventListener('click', () => { - this.data.stopwordCache = {}; - const stopwordsCopy = Object.assign({}, this.data.stopwords); - for (let [key, value] of Object.entries(stopwordsCopy)) { - this.data.stopwordCache[key] = value; - } + this.data.stopwordCache = structuredClone(this.data.stopwords); this.renderStopwordSettingsModal(this.data.stopwords); M.Modal.init(frequenciesStopwordSettingModal, {dismissible: false}); }); @@ -107,8 +101,7 @@ class CorpusAnalysisApp { if (action === 'submit') { this.renderFrequenciesGraphic(); } else if (action === 'cancel') { - this.data.stopwords = this.data.stopwordCache; - this.renderFrequenciesGraphic(); + this.data.stopwords = structuredClone(this.data.stopwordCache); } }); } @@ -271,13 +264,7 @@ class CorpusAnalysisApp { if (this.data.stopwords === undefined) { stopwords = await this.getStopwords(); } - let stopwordList = []; - Object.values(stopwords).forEach(stopwordItems => { - stopwordItems.forEach(stopword => { - stopwordList.push(stopword); - }); - }); - + let stopwordList = Object.values(stopwords).flat(); let graphData = []; let filteredData = Object.entries(corpusData.corpus.freqs[category]) .sort((a, b) => b[1] - a[1]) @@ -296,7 +283,7 @@ class CorpusAnalysisApp { } } else { for (let item of filteredData) { - let size = texts.map(text => text[1].freqs[category][item[0]] || 0); + let size = texts.map(text => text[1].freqs[category][item[0]] || 0); let data = { x: texts.map(text => `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`), y: texts.map(text => corpusData.values.p_attrs[category][item[0]]), @@ -324,18 +311,16 @@ class CorpusAnalysisApp { stopwordLanguageChipList.innerHTML = ''; userStopwordListContainer.innerHTML = ''; + stopwordInputField.value = ''; // Render stopword language selection. Set english as default language. Filter out user_stopwords. - for (let language of Object.keys(stopwords)) { - if (language !== 'user_stopwords') { - if (language === 'english') { - let optionElement = Utils.HTMLToElement(``); - stopwordLanguageSelection.appendChild(optionElement); - } else { - let optionElement = Utils.HTMLToElement(``); + if (stopwordLanguageSelection.children.length === 0) { + Object.keys(stopwords).forEach(language => { + if (language !== 'user_stopwords') { + let optionElement = Utils.HTMLToElement(``); stopwordLanguageSelection.appendChild(optionElement); } - } + }); } // Render user stopwords over input field. @@ -344,14 +329,15 @@ class CorpusAnalysisApp { let chipElement = Utils.HTMLToElement(`
${word}close
`); chipElement.addEventListener('click', (event) => { let removedListItem = event.target.closest('.chip').firstChild.textContent; - this.data.stopwords['user_stopwords'] = this.data.stopwords['user_stopwords'].filter(item => item !== removedListItem); + this.data.stopwords['user_stopwords'] = structuredClone(this.data.stopwords['user_stopwords'].filter(item => item !== removedListItem)); }); userStopwordListContainer.appendChild(chipElement); } } // Render english stopwords as default ... - this.renderStopwordLanguageChipList('english', stopwords['english']); + let selectedLanguage = document.querySelector('#stopword-language-selection').value; + this.renderStopwordLanguageChipList(selectedLanguage, stopwords[selectedLanguage]); // ... or render selected language stopwords. stopwordLanguageSelection.addEventListener('change', (event) => { @@ -369,7 +355,7 @@ class CorpusAnalysisApp { // Eventlistener for resetting all stopwords of a language to the original stopwords. resetLanguageStopwordListEntriesButton.addEventListener('click', () => { let selectedLanguage = stopwordLanguageSelection.value; - this.data.stopwords[selectedLanguage] = this.data.originalStopwords[selectedLanguage]; + this.data.stopwords[selectedLanguage] = structuredClone(this.data.originalStopwords[selectedLanguage]); this.renderStopwordLanguageChipList(selectedLanguage, this.data.stopwords[selectedLanguage]); }); @@ -379,13 +365,11 @@ class CorpusAnalysisApp { { placeholder: 'Add stopwords', onChipAdd: (event) => { - let userStopwords = []; for (let word of event[0].M_Chips.chipsData) { if (!this.data.stopwords['user_stopwords'].includes(word.tag.toLowerCase())) { - userStopwords.push(word.tag.toLowerCase()); + this.data.stopwords['user_stopwords'].push(word.tag.toLowerCase()); } } - this.data.stopwords['user_stopwords'] = this.data.stopwords['user_stopwords'].concat(userStopwords); } } ); @@ -394,18 +378,14 @@ class CorpusAnalysisApp { } buttonRendering() { - let stopwordLanguageSelection = document.querySelector('#stopword-language-selection'); let deleteLanguageStopwordListEntriesButton = document.querySelector('#delete-language-stopword-list-entries-button'); let resetLanguageStopwordListEntriesButton = document.querySelector('#reset-language-stopword-list-entries-button'); - - let selectedLanguage = stopwordLanguageSelection.value; + let selectedLanguage = document.querySelector('#stopword-language-selection').value; let stopwordLength = this.data.stopwords[selectedLanguage].length; let originalStopwordListLength = this.data.originalStopwords[selectedLanguage].length; - resetLanguageStopwordListEntriesButton.classList.toggle('blue', stopwordLength !== originalStopwordListLength); - deleteLanguageStopwordListEntriesButton.classList.toggle('red', stopwordLength > 0); - resetLanguageStopwordListEntriesButton.style.cursor = stopwordLength !== originalStopwordListLength ? 'pointer' : 'default'; - deleteLanguageStopwordListEntriesButton.style.cursor = stopwordLength > 0 ? 'pointer' : 'default'; + deleteLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === 0); + resetLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === originalStopwordListLength); } renderStopwordLanguageChipList(language, stopwords) { @@ -415,7 +395,7 @@ class CorpusAnalysisApp { let chipElement = Utils.HTMLToElement(`
${word}close
`); chipElement.addEventListener('click', (event) => { let removedListItem = event.target.closest('.chip').firstChild.textContent; - this.data.stopwords[language] = this.data.stopwords[language].filter(item => item !== removedListItem); + this.data.stopwords[language] = structuredClone(this.data.stopwords[language].filter(item => item !== removedListItem)); this.buttonRendering(); }); stopwordLanguageChipList.appendChild(chipElement); diff --git a/app/templates/corpora/analysis.html.j2 b/app/templates/corpora/analysis.html.j2 index 2bdb35f2..715361a4 100644 --- a/app/templates/corpora/analysis.html.j2 +++ b/app/templates/corpora/analysis.html.j2 @@ -186,8 +186,8 @@
-
Delete all belowdelete
-
Reset stopword listrefresh
+
Delete all belowdelete
+
Reset stopword listrefresh