2023-11-13 11:59:36 +00:00
|
|
|
nopaque.corpus_analysis.StaticVisualizationExtension = class StaticVisualizationExtension {
|
2023-07-26 07:03:36 +00:00
|
|
|
name = 'Static Visualization (beta)';
|
2023-07-11 13:52:44 +00:00
|
|
|
|
|
|
|
constructor(app) {
|
|
|
|
this.app = app;
|
|
|
|
this.data = {
|
|
|
|
stopwords: undefined,
|
|
|
|
originalStopwords: {},
|
|
|
|
stopwordCache: {},
|
2023-07-21 11:14:29 +00:00
|
|
|
promises: {getStopwords: undefined},
|
|
|
|
tokenSet: new Set()
|
2023-07-11 13:52:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.app.registerExtension(this);
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:14:29 +00:00
|
|
|
init() {
|
2023-07-11 13:52:44 +00:00
|
|
|
// Init data
|
|
|
|
this.data.corpus = this.app.data.corpus;
|
|
|
|
this.renderGeneralCorpusInfo();
|
|
|
|
this.renderTextInfoList();
|
2023-07-18 14:01:31 +00:00
|
|
|
this.renderTextProportionsGraphic();
|
|
|
|
this.renderTokenList();
|
2023-07-21 11:14:29 +00:00
|
|
|
// this.renderFrequenciesGraphic();
|
2023-07-18 14:01:31 +00:00
|
|
|
|
2023-07-11 13:52:44 +00:00
|
|
|
// Add event listeners
|
|
|
|
let frequenciesStopwordSettingModal = document.querySelector('#frequencies-stopwords-setting-modal');
|
|
|
|
let frequenciesStopwordSettingModalButton = document.querySelector('#frequencies-stopwords-setting-modal-button');
|
|
|
|
frequenciesStopwordSettingModalButton.addEventListener('click', () => {
|
|
|
|
this.data.stopwordCache = structuredClone(this.data.stopwords);
|
|
|
|
this.renderStopwordSettingsModal(this.data.stopwords);
|
|
|
|
M.Modal.init(frequenciesStopwordSettingModal, {dismissible: false});
|
|
|
|
});
|
|
|
|
|
2023-07-18 14:01:31 +00:00
|
|
|
let textProportionsGraphModeButtons = document.querySelectorAll('.text-proportions-graph-mode-button');
|
|
|
|
textProportionsGraphModeButtons.forEach(graphModeButton => {
|
|
|
|
graphModeButton.addEventListener('click', (event) => {
|
|
|
|
textProportionsGraphModeButtons.forEach(btn => {
|
|
|
|
btn.classList.remove('disabled');
|
|
|
|
});
|
|
|
|
event.target.closest('.text-proportions-graph-mode-button').classList.add('disabled');
|
|
|
|
this.renderTextProportionsGraphic();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let frequenciesTokenCategoryDropdownElement = document.querySelector('[data-target="frequencies-token-category-dropdown"]');
|
|
|
|
let frequenciesTokenCategoryDropdownListElement = document.querySelector("#frequencies-token-category-dropdown");
|
|
|
|
frequenciesTokenCategoryDropdownListElement.addEventListener('click', (event) => {
|
|
|
|
frequenciesTokenCategoryDropdownElement.firstChild.textContent = event.target.innerHTML;
|
2023-07-21 11:14:29 +00:00
|
|
|
this.renderTokenList();
|
2023-07-18 14:01:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let frequenciesGraphModeButtons = document.querySelectorAll('.frequencies-graph-mode-button');
|
|
|
|
frequenciesGraphModeButtons.forEach(graphModeButton => {
|
|
|
|
graphModeButton.addEventListener('click', (event) => {
|
|
|
|
frequenciesGraphModeButtons.forEach(btn => {
|
|
|
|
btn.classList.remove('disabled');
|
|
|
|
});
|
|
|
|
event.target.closest('.frequencies-graph-mode-button').classList.add('disabled');
|
2023-07-21 11:14:29 +00:00
|
|
|
this.renderFrequenciesGraphic(this.data.tokenSet);
|
2023-07-18 14:01:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-11 13:52:44 +00:00
|
|
|
for (let actionButton of document.querySelectorAll('.frequencies-stopword-setting-modal-action-buttons')) {
|
|
|
|
actionButton.addEventListener('click', (event) => {
|
|
|
|
let action = event.target.closest('.frequencies-stopword-setting-modal-action-buttons').dataset.action;
|
|
|
|
if (action === 'submit') {
|
2023-07-21 11:14:29 +00:00
|
|
|
this.renderTokenList();
|
2023-07-11 13:52:44 +00:00
|
|
|
} else if (action === 'cancel') {
|
|
|
|
this.data.stopwords = structuredClone(this.data.stopwordCache);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getStopwords() {
|
|
|
|
this.data.promises.getStopwords = new Promise((resolve, reject) => {
|
2023-11-09 13:29:01 +00:00
|
|
|
nopaque.requests.corpora.entity.getStopwords()
|
2023-07-11 13:52:44 +00:00
|
|
|
.then((response) => {
|
|
|
|
response.json()
|
|
|
|
.then((json) => {
|
|
|
|
this.data.originalStopwords = structuredClone(json);
|
|
|
|
this.data.stopwords = structuredClone(json);
|
|
|
|
resolve(this.data.stopwords);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return this.data.promises.getStopwords;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderGeneralCorpusInfo() {
|
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
2023-09-08 09:12:43 +00:00
|
|
|
document.querySelector('.corpus-num-tokens').innerHTML = corpusData.corpus.bounds[1] - corpusData.corpus.bounds[0];
|
|
|
|
document.querySelector('.corpus-num-s').innerHTML = corpusData.s_attrs.s.lexicon.length;
|
2023-07-11 13:52:44 +00:00
|
|
|
document.querySelector('.corpus-num-unique-words').innerHTML = Object.entries(corpusData.corpus.freqs.word).length;
|
|
|
|
document.querySelector('.corpus-num-unique-lemmas').innerHTML = Object.entries(corpusData.corpus.freqs.lemma).length;
|
|
|
|
document.querySelector('.corpus-num-unique-pos').innerHTML = Object.entries(corpusData.corpus.freqs.pos).length;
|
|
|
|
document.querySelector('.corpus-num-unique-simple-pos').innerHTML = Object.entries(corpusData.corpus.freqs.simple_pos).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTextInfoList() {
|
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
|
|
|
let corpusTextInfoListElement = document.querySelector('.corpus-text-info-list');
|
2023-11-09 13:29:01 +00:00
|
|
|
let corpusTextInfoList = new nopaque.resource_lists.CorpusTextInfoList(corpusTextInfoListElement);
|
2023-07-11 13:52:44 +00:00
|
|
|
let texts = corpusData.s_attrs.text.lexicon;
|
|
|
|
let textData = [];
|
|
|
|
for (let i = 0; i < Object.entries(texts).length; i++) {
|
|
|
|
let resource = {
|
|
|
|
title: corpusData.values.s_attrs.text[i].title,
|
|
|
|
publishing_year: corpusData.values.s_attrs.text[i].publishing_year,
|
2023-09-08 09:12:43 +00:00
|
|
|
// num_sentences: corpusData.s_attrs.text.lexicon[i].counts.s,
|
|
|
|
num_tokens: corpusData.s_attrs.text.lexicon[i].bounds[1] - corpusData.s_attrs.text.lexicon[i].bounds[0],
|
|
|
|
num_sentences: corpusData.s_attrs.s.lexicon.filter((s) => {
|
|
|
|
return s.bounds[0] >= corpusData.s_attrs.text.lexicon[i].bounds[0] && s.bounds[1] <= corpusData.s_attrs.text.lexicon[i].bounds[1];
|
|
|
|
}).length,
|
2023-07-11 13:52:44 +00:00
|
|
|
num_unique_words: Object.entries(corpusData.s_attrs.text.lexicon[i].freqs.word).length,
|
|
|
|
num_unique_lemmas: Object.entries(corpusData.s_attrs.text.lexicon[i].freqs.lemma).length,
|
|
|
|
num_unique_pos: Object.entries(corpusData.s_attrs.text.lexicon[i].freqs.pos).length,
|
|
|
|
num_unique_simple_pos: Object.entries(corpusData.s_attrs.text.lexicon[i].freqs.simple_pos).length
|
|
|
|
};
|
|
|
|
|
|
|
|
textData.push(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
corpusTextInfoList.add(textData);
|
|
|
|
|
|
|
|
let textCountChipElement = document.querySelector('.text-count-chip');
|
2023-09-08 09:12:43 +00:00
|
|
|
textCountChipElement.innerHTML = `Text count: ${corpusData.s_attrs.text.lexicon.length}`;
|
2023-07-11 13:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTextProportionsGraphic() {
|
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
|
|
|
let textProportionsGraphicElement = document.querySelector('#text-proportions-graphic');
|
|
|
|
let texts = Object.entries(corpusData.s_attrs.text.lexicon);
|
2023-07-18 14:01:31 +00:00
|
|
|
let graphtype = document.querySelector('.text-proportions-graph-mode-button.disabled').dataset.graphType;
|
|
|
|
let textProportionsTitleElement = document.querySelector('#text-proportions-title-element');
|
|
|
|
|
|
|
|
if (graphtype === 'bar') {
|
|
|
|
textProportionsTitleElement.innerHTML = 'Bounds';
|
|
|
|
} else if (graphtype === 'pie') {
|
|
|
|
textProportionsTitleElement.innerHTML = 'Proportions';
|
|
|
|
}
|
|
|
|
|
|
|
|
let graphData = this.createTextProportionsGraphData(texts, graphtype);
|
2023-07-11 13:52:44 +00:00
|
|
|
let graphLayout = {
|
2023-07-18 14:01:31 +00:00
|
|
|
barmode: graphtype === 'bar' ? 'relative' : '',
|
|
|
|
type: graphtype,
|
|
|
|
showgrid: false,
|
|
|
|
height: 447,
|
2023-07-11 13:52:44 +00:00
|
|
|
margin: {
|
|
|
|
l: 10,
|
|
|
|
r: 10,
|
2023-07-18 14:01:31 +00:00
|
|
|
b: graphtype === 'bar' ? 80 : 10,
|
|
|
|
t: graphtype === 'bar' ? 80 : 10,
|
2023-07-11 13:52:44 +00:00
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
"orientation": "h",
|
|
|
|
font: {
|
|
|
|
size: 10
|
|
|
|
}
|
2023-07-18 14:01:31 +00:00
|
|
|
},
|
|
|
|
xaxis: {
|
|
|
|
rangemode: 'nonnegative',
|
|
|
|
autorange: true
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
autorange: true,
|
|
|
|
showticklabels: false
|
2023-07-11 13:52:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let config = {
|
|
|
|
responsive: true,
|
2023-07-18 14:01:31 +00:00
|
|
|
modeBarButtonsToRemove: ['zoom2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'],
|
2023-07-11 13:52:44 +00:00
|
|
|
displaylogo: false
|
|
|
|
};
|
|
|
|
|
|
|
|
Plotly.newPlot(textProportionsGraphicElement, graphData, graphLayout, config);
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:01:31 +00:00
|
|
|
createTextProportionsGraphData(texts, graphtype) {
|
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
|
|
|
let graphData = [];
|
|
|
|
switch (graphtype) {
|
|
|
|
case 'bar':
|
|
|
|
for (let text of texts) {
|
|
|
|
let textData = {
|
|
|
|
type: 'bar',
|
|
|
|
orientation: 'h',
|
|
|
|
x: [text[1].bounds[1] - text[1].bounds[0]],
|
|
|
|
y: [0.5],
|
|
|
|
text: [`${text[1].bounds[0]} - ${text[1].bounds[1]}`],
|
|
|
|
name: `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`,
|
|
|
|
hovertemplate: `${text[1].bounds[0]} - ${text[1].bounds[1]}`,
|
|
|
|
};
|
|
|
|
graphData.push(textData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
graphData = [
|
|
|
|
{
|
2023-09-08 09:12:43 +00:00
|
|
|
values: texts.map(text => text[1].bounds[1] - text[1].bounds[0]),
|
2023-07-18 14:01:31 +00:00
|
|
|
labels: texts.map(text => `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`),
|
|
|
|
type: graphtype
|
|
|
|
}
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return graphData;
|
|
|
|
}
|
|
|
|
|
|
|
|
async renderTokenList() {
|
|
|
|
let corpusTokenListElement = document.querySelector('.corpus-token-list');
|
2023-11-09 13:29:01 +00:00
|
|
|
let corpusTokenList = new nopaque.resource_lists.CorpusTokenList(corpusTokenListElement);
|
2023-07-21 11:14:29 +00:00
|
|
|
let filteredData = this.filterData();
|
2023-07-18 14:01:31 +00:00
|
|
|
let stopwords = this.data.stopwords;
|
|
|
|
if (this.data.stopwords === undefined) {
|
|
|
|
stopwords = await this.getStopwords();
|
|
|
|
}
|
|
|
|
stopwords = Object.values(stopwords).flat();
|
2023-07-21 11:14:29 +00:00
|
|
|
let mostFrequent = Object.entries(filteredData)
|
|
|
|
.sort((a, b) => b[1].count - a[1].count)
|
|
|
|
.filter(item => !stopwords.includes(item[0].toLowerCase()))
|
2023-07-18 14:01:31 +00:00
|
|
|
.slice(0, 4)
|
2023-07-21 11:14:29 +00:00
|
|
|
.map(item => item[0])
|
|
|
|
|
2023-07-18 14:01:31 +00:00
|
|
|
let tokenData = [];
|
2023-07-21 11:14:29 +00:00
|
|
|
Object.entries(filteredData).forEach(item => {
|
2023-07-18 14:01:31 +00:00
|
|
|
let resource = {
|
2023-07-21 11:14:29 +00:00
|
|
|
term: item[0],
|
|
|
|
count: item[1].count,
|
|
|
|
mostFrequent: mostFrequent.includes(item[0])
|
2023-07-18 14:01:31 +00:00
|
|
|
};
|
|
|
|
if (!Object.values(stopwords).includes(resource.term)) {
|
|
|
|
tokenData.push(resource);
|
|
|
|
}
|
2023-07-21 11:14:29 +00:00
|
|
|
});
|
2023-07-18 14:01:31 +00:00
|
|
|
corpusTokenList.add(tokenData);
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:14:29 +00:00
|
|
|
filterData() {
|
|
|
|
let frequenciesTokenCategoryDropdownElement = document.querySelector('[data-target="frequencies-token-category-dropdown"]');
|
|
|
|
let tokenCategory = frequenciesTokenCategoryDropdownElement.firstChild.textContent.toLowerCase();
|
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
|
|
|
let filteredData = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < Object.values(corpusData.corpus.freqs[tokenCategory]).length; i++) {
|
|
|
|
let term = corpusData.values.p_attrs[tokenCategory][i].toLowerCase();
|
|
|
|
let count = corpusData.corpus.freqs[tokenCategory][i];
|
|
|
|
|
|
|
|
if (filteredData[term]) {
|
|
|
|
filteredData[term].count += count;
|
|
|
|
filteredData[term].originalIds.push(i);
|
|
|
|
} else {
|
|
|
|
filteredData[term] = {
|
|
|
|
count: count,
|
|
|
|
originalIds: [i]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
renderFrequenciesGraphic(tokenSet) {
|
|
|
|
this.data.tokenSet = tokenSet;
|
2023-07-11 13:52:44 +00:00
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
|
|
|
let frequenciesTokenCategoryDropdownElement = document.querySelector('[data-target="frequencies-token-category-dropdown"]');
|
|
|
|
let frequenciesGraphicElement = document.querySelector('#frequencies-graphic');
|
|
|
|
let texts = Object.entries(corpusData.s_attrs.text.lexicon);
|
|
|
|
let graphtype = document.querySelector('.frequencies-graph-mode-button.disabled').dataset.graphType;
|
|
|
|
let tokenCategory = frequenciesTokenCategoryDropdownElement.firstChild.textContent.toLowerCase();
|
|
|
|
|
2023-07-21 11:14:29 +00:00
|
|
|
let graphData = this.createFrequenciesGraphData(tokenCategory, texts, graphtype, tokenSet);
|
2023-07-11 13:52:44 +00:00
|
|
|
let graphLayout = {
|
|
|
|
barmode: graphtype === 'bar' ? 'stack' : '',
|
|
|
|
yaxis: {
|
|
|
|
showticklabels: graphtype === 'markers' ? false : true
|
|
|
|
},
|
2023-07-21 11:14:29 +00:00
|
|
|
height: 627,
|
|
|
|
margin: {
|
2023-07-26 07:03:36 +00:00
|
|
|
l: 33
|
2023-07-21 11:14:29 +00:00
|
|
|
}
|
2023-07-11 13:52:44 +00:00
|
|
|
};
|
|
|
|
let config = {
|
|
|
|
responsive: true,
|
|
|
|
modeBarButtonsToRemove: ['zoom2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'],
|
|
|
|
displaylogo: false
|
|
|
|
};
|
|
|
|
Plotly.newPlot(frequenciesGraphicElement, graphData, graphLayout, config);
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:14:29 +00:00
|
|
|
createFrequenciesGraphData(tokenCategory, texts, graphtype, tokenSet) {
|
2023-07-18 14:01:31 +00:00
|
|
|
let corpusData = this.data.corpus.o.staticData;
|
2023-07-11 13:52:44 +00:00
|
|
|
let graphData = [];
|
2023-07-21 11:14:29 +00:00
|
|
|
let filteredData = this.filterData();
|
2023-07-18 14:01:31 +00:00
|
|
|
switch (graphtype) {
|
|
|
|
case 'markers':
|
2023-07-21 11:14:29 +00:00
|
|
|
for (let item of tokenSet) {
|
|
|
|
let textTitles = texts.map(text => `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`);
|
|
|
|
let tokenCountPerText = [];
|
|
|
|
for (let originalId of filteredData[item].originalIds) {
|
|
|
|
for (let i = 0; i < texts.length; i++) {
|
|
|
|
tokenCountPerText[i] = (tokenCountPerText[i] || 0) + (texts[i][1].freqs[tokenCategory][originalId] || 0);
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 14:01:31 +00:00
|
|
|
let data = {
|
2023-07-21 11:14:29 +00:00
|
|
|
x: textTitles,
|
|
|
|
y: texts.map(text => item),
|
|
|
|
name: item,
|
|
|
|
text: texts.map(text => `${item}<br>${tokenCountPerText || 0}`),
|
2023-07-18 14:01:31 +00:00
|
|
|
mode: 'markers',
|
|
|
|
marker: {
|
2023-07-21 11:14:29 +00:00
|
|
|
size: tokenCountPerText,
|
2023-07-18 14:01:31 +00:00
|
|
|
sizeref: 0.4
|
|
|
|
}
|
|
|
|
};
|
|
|
|
graphData.push(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2023-07-21 11:14:29 +00:00
|
|
|
for (let item of tokenSet) {
|
|
|
|
let textTitles = texts.map(text => `${corpusData.values.s_attrs.text[text[0]].title} (${corpusData.values.s_attrs.text[text[0]].publishing_year})`);
|
|
|
|
let tokenCountPerText = [];
|
|
|
|
for (let originalId of filteredData[item].originalIds) {
|
|
|
|
for (let i = 0; i < texts.length; i++) {
|
|
|
|
tokenCountPerText[i] = (tokenCountPerText[i] || 0) + (texts[i][1].freqs[tokenCategory][originalId] || 0);
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 14:01:31 +00:00
|
|
|
let data = {
|
2023-07-21 11:14:29 +00:00
|
|
|
x: textTitles,
|
|
|
|
y: tokenCountPerText,
|
|
|
|
name: item,
|
2023-07-18 14:01:31 +00:00
|
|
|
type: graphtype
|
|
|
|
};
|
|
|
|
graphData.push(data);
|
|
|
|
}
|
|
|
|
break;
|
2023-07-11 13:52:44 +00:00
|
|
|
}
|
|
|
|
return graphData;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderStopwordSettingsModal(stopwords) {
|
|
|
|
let stopwordInputField = document.querySelector('#stopword-input-field');
|
|
|
|
let userStopwordListContainer = document.querySelector('#user-stopword-list-container');
|
|
|
|
let stopwordLanguageSelection = document.querySelector('#stopword-language-selection');
|
|
|
|
let stopwordLanguageChipList = document.querySelector('#stopword-language-chip-list');
|
|
|
|
let deleteLanguageStopwordListEntriesButton = document.querySelector('#delete-language-stopword-list-entries-button');
|
|
|
|
let resetLanguageStopwordListEntriesButton = document.querySelector('#reset-language-stopword-list-entries-button');
|
|
|
|
|
|
|
|
stopwordLanguageChipList.innerHTML = '';
|
|
|
|
userStopwordListContainer.innerHTML = '';
|
|
|
|
stopwordInputField.value = '';
|
|
|
|
|
|
|
|
// Render stopword language selection. Set english as default language. Filter out user_stopwords.
|
|
|
|
if (stopwordLanguageSelection.children.length === 0) {
|
|
|
|
Object.keys(stopwords).forEach(language => {
|
|
|
|
if (language !== 'user_stopwords') {
|
2023-11-09 13:29:01 +00:00
|
|
|
let optionElement = nopaque.Utils.HTMLToElement(`<option value="${language}" ${language === 'english' ? 'selected' : ''}>${language}</option>`);
|
2023-07-11 13:52:44 +00:00
|
|
|
stopwordLanguageSelection.appendChild(optionElement);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render user stopwords over input field.
|
|
|
|
if (this.data.stopwords['user_stopwords'].length > 0) {
|
|
|
|
for (let word of this.data.stopwords['user_stopwords']) {
|
2023-11-09 13:29:01 +00:00
|
|
|
let chipElement = nopaque.Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`);
|
2023-07-11 13:52:44 +00:00
|
|
|
chipElement.addEventListener('click', (event) => {
|
|
|
|
let removedListItem = event.target.closest('.chip').firstChild.textContent;
|
|
|
|
this.data.stopwords['user_stopwords'] = structuredClone(this.data.stopwords['user_stopwords'].filter(item => item !== removedListItem));
|
|
|
|
});
|
|
|
|
userStopwordListContainer.appendChild(chipElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render english stopwords as default ...
|
|
|
|
let selectedLanguage = document.querySelector('#stopword-language-selection').value;
|
|
|
|
this.renderStopwordLanguageChipList(selectedLanguage, stopwords[selectedLanguage]);
|
|
|
|
|
|
|
|
// ... or render selected language stopwords.
|
|
|
|
stopwordLanguageSelection.addEventListener('change', (event) => {
|
|
|
|
this.renderStopwordLanguageChipList(event.target.value, stopwords[event.target.value]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Eventlistener for deleting all stopwords of a language.
|
|
|
|
deleteLanguageStopwordListEntriesButton.addEventListener('click', (event) => {
|
|
|
|
let selectedLanguage = stopwordLanguageSelection.value;
|
|
|
|
this.data.stopwords[selectedLanguage] = [];
|
|
|
|
stopwordLanguageChipList.innerHTML = '';
|
|
|
|
this.buttonRendering();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Eventlistener for resetting all stopwords of a language to the original stopwords.
|
|
|
|
resetLanguageStopwordListEntriesButton.addEventListener('click', () => {
|
|
|
|
let selectedLanguage = stopwordLanguageSelection.value;
|
|
|
|
this.data.stopwords[selectedLanguage] = structuredClone(this.data.originalStopwords[selectedLanguage]);
|
|
|
|
this.renderStopwordLanguageChipList(selectedLanguage, this.data.stopwords[selectedLanguage]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize Materialize components.
|
|
|
|
M.Chips.init(
|
|
|
|
stopwordInputField,
|
|
|
|
{
|
|
|
|
placeholder: 'Add stopwords',
|
|
|
|
onChipAdd: (event) => {
|
|
|
|
for (let word of event[0].M_Chips.chipsData) {
|
|
|
|
if (!this.data.stopwords['user_stopwords'].includes(word.tag.toLowerCase())) {
|
|
|
|
this.data.stopwords['user_stopwords'].push(word.tag.toLowerCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
M.FormSelect.init(stopwordLanguageSelection);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonRendering() {
|
|
|
|
let deleteLanguageStopwordListEntriesButton = document.querySelector('#delete-language-stopword-list-entries-button');
|
|
|
|
let resetLanguageStopwordListEntriesButton = document.querySelector('#reset-language-stopword-list-entries-button');
|
|
|
|
let selectedLanguage = document.querySelector('#stopword-language-selection').value;
|
|
|
|
let stopwordLength = this.data.stopwords[selectedLanguage].length;
|
|
|
|
let originalStopwordListLength = this.data.originalStopwords[selectedLanguage].length;
|
|
|
|
|
|
|
|
deleteLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === 0);
|
|
|
|
resetLanguageStopwordListEntriesButton.classList.toggle('disabled', stopwordLength === originalStopwordListLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderStopwordLanguageChipList(language, stopwords) {
|
|
|
|
let stopwordLanguageChipList = document.querySelector('#stopword-language-chip-list');
|
|
|
|
stopwordLanguageChipList.innerHTML = '';
|
|
|
|
for (let word of stopwords) {
|
2023-11-09 13:29:01 +00:00
|
|
|
let chipElement = nopaque.Utils.HTMLToElement(`<div class="chip">${word}<i class="close material-icons">close</i></div>`);
|
2023-07-11 13:52:44 +00:00
|
|
|
chipElement.addEventListener('click', (event) => {
|
|
|
|
let removedListItem = event.target.closest('.chip').firstChild.textContent;
|
|
|
|
this.data.stopwords[language] = structuredClone(this.data.stopwords[language].filter(item => item !== removedListItem));
|
|
|
|
this.buttonRendering();
|
|
|
|
});
|
|
|
|
stopwordLanguageChipList.appendChild(chipElement);
|
|
|
|
}
|
|
|
|
this.buttonRendering();
|
|
|
|
}
|
|
|
|
}
|