Small updates custom stopword list

This commit is contained in:
Inga Kirschnick 2023-07-11 13:40:20 +02:00
parent 22b43a689f
commit 572fdf3a00
2 changed files with 23 additions and 43 deletions

View File

@ -33,10 +33,8 @@ class CorpusAnalysisApp {
.then((response) => { .then((response) => {
response.json() response.json()
.then((json) => { .then((json) => {
for (let [key, value] of Object.entries(json)) { this.data.originalStopwords = structuredClone(json);
this.data.originalStopwords[key] = value; this.data.stopwords = structuredClone(json);
}
this.data.stopwords = json;
resolve(this.data.stopwords); resolve(this.data.stopwords);
}) })
.catch((error) => { .catch((error) => {
@ -92,11 +90,7 @@ class CorpusAnalysisApp {
let frequenciesStopwordSettingModal = document.querySelector('#frequencies-stopwords-setting-modal'); let frequenciesStopwordSettingModal = document.querySelector('#frequencies-stopwords-setting-modal');
let frequenciesStopwordSettingModalButton = document.querySelector('#frequencies-stopwords-setting-modal-button'); let frequenciesStopwordSettingModalButton = document.querySelector('#frequencies-stopwords-setting-modal-button');
frequenciesStopwordSettingModalButton.addEventListener('click', () => { frequenciesStopwordSettingModalButton.addEventListener('click', () => {
this.data.stopwordCache = {}; this.data.stopwordCache = structuredClone(this.data.stopwords);
const stopwordsCopy = Object.assign({}, this.data.stopwords);
for (let [key, value] of Object.entries(stopwordsCopy)) {
this.data.stopwordCache[key] = value;
}
this.renderStopwordSettingsModal(this.data.stopwords); this.renderStopwordSettingsModal(this.data.stopwords);
M.Modal.init(frequenciesStopwordSettingModal, {dismissible: false}); M.Modal.init(frequenciesStopwordSettingModal, {dismissible: false});
}); });
@ -107,8 +101,7 @@ class CorpusAnalysisApp {
if (action === 'submit') { if (action === 'submit') {
this.renderFrequenciesGraphic(); this.renderFrequenciesGraphic();
} else if (action === 'cancel') { } else if (action === 'cancel') {
this.data.stopwords = this.data.stopwordCache; this.data.stopwords = structuredClone(this.data.stopwordCache);
this.renderFrequenciesGraphic();
} }
}); });
} }
@ -271,13 +264,7 @@ class CorpusAnalysisApp {
if (this.data.stopwords === undefined) { if (this.data.stopwords === undefined) {
stopwords = await this.getStopwords(); stopwords = await this.getStopwords();
} }
let stopwordList = []; let stopwordList = Object.values(stopwords).flat();
Object.values(stopwords).forEach(stopwordItems => {
stopwordItems.forEach(stopword => {
stopwordList.push(stopword);
});
});
let graphData = []; let graphData = [];
let filteredData = Object.entries(corpusData.corpus.freqs[category]) let filteredData = Object.entries(corpusData.corpus.freqs[category])
.sort((a, b) => b[1] - a[1]) .sort((a, b) => b[1] - a[1])
@ -296,7 +283,7 @@ class CorpusAnalysisApp {
} }
} else { } else {
for (let item of filteredData) { 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 = { let data = {
x: texts.map(text => `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`), 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]]), y: texts.map(text => corpusData.values.p_attrs[category][item[0]]),
@ -324,18 +311,16 @@ class CorpusAnalysisApp {
stopwordLanguageChipList.innerHTML = ''; stopwordLanguageChipList.innerHTML = '';
userStopwordListContainer.innerHTML = ''; userStopwordListContainer.innerHTML = '';
stopwordInputField.value = '';
// Render stopword language selection. Set english as default language. Filter out user_stopwords. // Render stopword language selection. Set english as default language. Filter out user_stopwords.
for (let language of Object.keys(stopwords)) { if (stopwordLanguageSelection.children.length === 0) {
if (language !== 'user_stopwords') { Object.keys(stopwords).forEach(language => {
if (language === 'english') { if (language !== 'user_stopwords') {
let optionElement = Utils.HTMLToElement(`<option value="${language}" selected>${language}</option>`); let optionElement = Utils.HTMLToElement(`<option value="${language}" ${language === 'english' ? 'selected' : ''}>${language}</option>`);
stopwordLanguageSelection.appendChild(optionElement);
} else {
let optionElement = Utils.HTMLToElement(`<option value="${language}">${language}</option>`);
stopwordLanguageSelection.appendChild(optionElement); stopwordLanguageSelection.appendChild(optionElement);
} }
} });
} }
// Render user stopwords over input field. // Render user stopwords over input field.
@ -344,14 +329,15 @@ class CorpusAnalysisApp {
let chipElement = Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`); let chipElement = Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`);
chipElement.addEventListener('click', (event) => { chipElement.addEventListener('click', (event) => {
let removedListItem = event.target.closest('.chip').firstChild.textContent; 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); userStopwordListContainer.appendChild(chipElement);
} }
} }
// Render english stopwords as default ... // 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. // ... or render selected language stopwords.
stopwordLanguageSelection.addEventListener('change', (event) => { stopwordLanguageSelection.addEventListener('change', (event) => {
@ -369,7 +355,7 @@ class CorpusAnalysisApp {
// Eventlistener for resetting all stopwords of a language to the original stopwords. // Eventlistener for resetting all stopwords of a language to the original stopwords.
resetLanguageStopwordListEntriesButton.addEventListener('click', () => { resetLanguageStopwordListEntriesButton.addEventListener('click', () => {
let selectedLanguage = stopwordLanguageSelection.value; 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]); this.renderStopwordLanguageChipList(selectedLanguage, this.data.stopwords[selectedLanguage]);
}); });
@ -379,13 +365,11 @@ class CorpusAnalysisApp {
{ {
placeholder: 'Add stopwords', placeholder: 'Add stopwords',
onChipAdd: (event) => { onChipAdd: (event) => {
let userStopwords = [];
for (let word of event[0].M_Chips.chipsData) { for (let word of event[0].M_Chips.chipsData) {
if (!this.data.stopwords['user_stopwords'].includes(word.tag.toLowerCase())) { 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() { buttonRendering() {
let stopwordLanguageSelection = document.querySelector('#stopword-language-selection');
let deleteLanguageStopwordListEntriesButton = document.querySelector('#delete-language-stopword-list-entries-button'); let deleteLanguageStopwordListEntriesButton = document.querySelector('#delete-language-stopword-list-entries-button');
let resetLanguageStopwordListEntriesButton = document.querySelector('#reset-language-stopword-list-entries-button'); let resetLanguageStopwordListEntriesButton = document.querySelector('#reset-language-stopword-list-entries-button');
let selectedLanguage = document.querySelector('#stopword-language-selection').value;
let selectedLanguage = stopwordLanguageSelection.value;
let stopwordLength = this.data.stopwords[selectedLanguage].length; let stopwordLength = this.data.stopwords[selectedLanguage].length;
let originalStopwordListLength = this.data.originalStopwords[selectedLanguage].length; let originalStopwordListLength = this.data.originalStopwords[selectedLanguage].length;
resetLanguageStopwordListEntriesButton.classList.toggle('blue', stopwordLength !== originalStopwordListLength); deleteLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === 0);
deleteLanguageStopwordListEntriesButton.classList.toggle('red', stopwordLength > 0); resetLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === originalStopwordListLength);
resetLanguageStopwordListEntriesButton.style.cursor = stopwordLength !== originalStopwordListLength ? 'pointer' : 'default';
deleteLanguageStopwordListEntriesButton.style.cursor = stopwordLength > 0 ? 'pointer' : 'default';
} }
renderStopwordLanguageChipList(language, stopwords) { renderStopwordLanguageChipList(language, stopwords) {
@ -415,7 +395,7 @@ class CorpusAnalysisApp {
let chipElement = Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`); let chipElement = Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`);
chipElement.addEventListener('click', (event) => { chipElement.addEventListener('click', (event) => {
let removedListItem = event.target.closest('.chip').firstChild.textContent; 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(); this.buttonRendering();
}); });
stopwordLanguageChipList.appendChild(chipElement); stopwordLanguageChipList.appendChild(chipElement);

View File

@ -186,8 +186,8 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="chip white-text" id="delete-language-stopword-list-entries-button" style="cursor:pointer">Delete all below<i class="material-icons right" style="margin-top: 4px; margin-left: -1px;">delete</i></div> <div class="chip btn white-text red" id="delete-language-stopword-list-entries-button">Delete all below<i class="material-icons right">delete</i></div>
<div class="chip white-text" id="reset-language-stopword-list-entries-button" style="cursor:pointer">Reset stopword list<i class="material-icons right disable-on-click" style="margin-top: 4px; margin-left: -1px;">refresh</i></div> <div class="chip btn white-text blue" id="reset-language-stopword-list-entries-button">Reset stopword list<i class="material-icons right">refresh</i></div>
</div> </div>
<div id="stopword-language-chip-list"></div> <div id="stopword-language-chip-list"></div>
</div> </div>