`
);
if (headerModifier) header = headerModifier(header);
const thead = dom("thead", ``);
thead.appendChild(header);
table.appendChild(thead);
now = performance.now();
let num = 0;
let limit = isMobile() ? 500 : 2000;
hits.every(hit => {
let itemDom = itemToDOM(hit);
if (itemDomModifier)
itemDom = itemDomModifier(hit, itemDom, hits, setQuery);
table.appendChild(itemDom);
num++;
return num < limit;
});
console.log(
"Building DOM took: " + (performance.now() - now) / 1000.0 + " secs"
);
numResults.innerHTML =
"Resultate: " +
hits.length +
(num < hits.length ? ", " + num + " angezeigt" : "");
lastHits = hits;
};
let timeoutId;
searchInput.addEventListener("input", (event) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
const query = searchInput.value.trim();
if (query == 0) {
minPrice.value = 0;
maxPrice.value = 100;
}
if (query?.charAt(0) == "!") {
parentElement
.querySelectorAll(".filters")
.forEach((f) => (f.style.display = "none"));
} else {
parentElement
.querySelectorAll(".filters")
.forEach((f) => (f.style.display = "block"));
}
setQuery();
search(searchInput.value);
}, 50);
});
budgetBrands.addEventListener("change", () => search(searchInput.value));
bio.addEventListener("change", () => search(searchInput.value));
allCheckbox.addEventListener("change", () => storeCheckboxes.forEach(store => store.checked = allCheckbox.checked));
storeCheckboxes.map((store) =>
store.addEventListener("change", () => search(searchInput.value))
);
sort.addEventListener("change", () => search(searchInput.value));
minPrice.addEventListener("change", () => search(searchInput.value));
maxPrice.addEventListener("change", () => search(searchInput.value));
exact.addEventListener("change", () => search(searchInput.value));
return () => search(searchInput.value);
}
function showChart(canvasDom, items, chartType) {
if (items.length == 0) {
canvasDom.style.display = "none";
return;
} else {
canvasDom.style.display = "block";
}
const allDates = items.flatMap((product) =>
product.priceHistory.map((item) => item.date)
);
const uniqueDates = [...new Set(allDates)];
uniqueDates.sort();
const datasets = items.map((product) => {
let price = null;
const prices = uniqueDates.map((date) => {
const priceObj = product.priceHistory.find(
(item) => item.date === date
);
if (!price && priceObj) price = priceObj.price;
return priceObj ? priceObj.price : null;
});
for (let i = 0; i < prices.length; i++) {
if (prices[i] == null) {
prices[i] = price;
} else {
price = prices[i];
}
}
return {
label: (product.store ? product.store + " " : "") + product.name,
data: prices,
};
});
const ctx = canvasDom.getContext("2d");
let scrollTop = -1;
if (canvasDom.lastChart) {
scrollTop = document.documentElement.scrollTop;
canvasDom.lastChart.destroy();
}
canvasDom.lastChart = new Chart(ctx, {
type: chartType ? chartType : "line",
data: {
labels: uniqueDates,
datasets: datasets,
},
options: {
responsive: true,
aspectRation: 16 / 9,
scales: {
y: {
title: {
display: true,
text: "EURO",
},
},
},
},
});
if (scrollTop != -1) document.documentElement.scrollTop = scrollTop;
}
function getOldestDate(items) {
let oldestDate = "9999-01-01";
for (item of items) {
if (oldestDate > item.dateOldest) oldestDate = item.dateOldest;
}
return oldestDate;
}
function showCharts(
canvasDom,
items,
sum,
sumStores,
todayOnly,
startDate,
endDate
) {
let itemsToShow = [];
if (sum && items.length > 0) {
itemsToShow.push({
name: "Preissumme Warenkorb",
priceHistory: calculateOverallPriceChanges(
items,
todayOnly,
startDate,
endDate
),
});
}
if (sumStores && items.length > 0) {
STORE_KEYS.forEach((store) => {
const storeItems = items.filter((item) => item.store === store);
if (storeItems.length > 0) {
itemsToShow.push({
name: "Preissumme " + store,
priceHistory: calculateOverallPriceChanges(
storeItems,
todayOnly,
startDate,
endDate
),
});
}
});
}
items.forEach((item) => {
if (item.chart) {
itemsToShow.push({
name: item.store + " " + item.name,
priceHistory: todayOnly
? [{ date: currentDate(), price: item.price }]
: item.priceHistory.filter(
(price) =>
price.date >= startDate && price.date <= endDate
),
});
}
});
showChart(canvasDom, itemsToShow, todayOnly ? "bar" : "line");
}
function calculateOverallPriceChanges(items, todayOnly, startDate, endDate) {
if (items.length == 0) return { dates: [], changes: [] };
if (todayOnly) {
let sum = 0;
for (item of items) sum += item.price;
return [{ date: currentDate(), price: sum }];
}
const allDates = items.flatMap((product) =>
product.priceHistory.map((item) => item.date)
);
let uniqueDates = [...new Set(allDates)];
uniqueDates.sort();
uniqueDates = uniqueDates.filter(
(date) => date >= startDate && date <= endDate
);
const allPrices = items.map((product) => {
let price = null;
const prices = uniqueDates.map((date) => {
const priceObj = product.priceHistory.find(
(item) => item.date === date
);
if (!price && priceObj) price = priceObj.price;
return priceObj ? priceObj.price : null;
});
for (let i = 0; i < prices.length; i++) {
if (!prices[i]) {
prices[i] = price;
} else {
price = prices[i];
}
}
return prices;
});
const priceChanges = [];
for (let i = 0; i < uniqueDates.length; i++) {
let price = 0;
for (let j = 0; j < allPrices.length; j++) {
price += allPrices[j][i];
}
priceChanges.push({ date: uniqueDates[i], price });
}
return priceChanges;
}
function downloadFile(filename, content) {
const blob = new Blob([content], { type: "text/plain" });
const element = document.createElement("a");
element.href = URL.createObjectURL(blob);
element.download = filename;
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
URL.revokeObjectURL(element.href);
}
/* by Joder Illi, Snowball mailing list */
function stem(word) {
/*
Put u and y between vowels into upper case
*/
word = word.replace(/([aeiouyäöü])u([aeiouyäöü])/g, "$1U$2");
word = word.replace(/([aeiouyäöü])y([aeiouyäöü])/g, "$1Y$2");
/*
and then do the following mappings,
(a) replace ß with ss,
(a) replace ae with ä, Not doing these,
have trouble with diphtongs
(a) replace oe with ö, Not doing these,
have trouble with diphtongs
(a) replace ue with ü unless preceded by q. Not doing these,
have trouble with diphtongs
So in quelle, ue is not mapped to ü because it follows q, and in
feuer it is not mapped because the first part of the rule changes it to
feUer, so the u is not found.
*/
word = word.replace(/ß/g, "ss");
//word = word.replace(/ae/g, 'ä');
//word = word.replace(/oe/g, 'ö');
//word = word.replace(/([^q])ue/g, '$1ü');
/*
R1 and R2 are first set up in the standard way (see the note on R1
and R2), but then R1 is adjusted so that the region before it contains at
least 3 letters.
R1 is the region after the first non-vowel following a vowel, or is
the null region at the end of the word if there is no such non-vowel.
R2 is the region after the first non-vowel following a vowel in R1,
or is the null region at the end of the word if there is no such non-vowel.
*/
var r1Index = word.search(/[aeiouyäöü][^aeiouyäöü]/);
var r1 = "";
if (r1Index != -1) {
r1Index += 2;
r1 = word.substring(r1Index);
}
var r2Index = -1;
var r2 = "";
if (r1Index != -1) {
var r2Index = r1.search(/[aeiouyäöü][^aeiouyäöü]/);
if (r2Index != -1) {
r2Index += 2;
r2 = r1.substring(r2Index);
r2Index += r1Index;
} else {
r2 = "";
}
}
if (r1Index != -1 && r1Index < 3) {
r1Index = 3;
r1 = word.substring(r1Index);
}
/*
Define a valid s-ending as one of b, d, f, g, h, k, l, m, n, r or t.
Define a valid st-ending as the same list, excluding letter r.
*/
/*
Do each of steps 1, 2 and 3.
*/
/*
Step 1:
Search for the longest among the following suffixes,
(a) em ern er
(b) e en es
(c) s (preceded by a valid s-ending)
*/
var a1Index = word.search(/(em|ern|er)$/g);
var b1Index = word.search(/(e|en|es)$/g);
var c1Index = word.search(/([bdfghklmnrt]s)$/g);
if (c1Index != -1) {
c1Index++;
}
var index1 = 10000;
var optionUsed1 = "";
if (a1Index != -1 && a1Index < index1) {
optionUsed1 = "a";
index1 = a1Index;
}
if (b1Index != -1 && b1Index < index1) {
optionUsed1 = "b";
index1 = b1Index;
}
if (c1Index != -1 && c1Index < index1) {
optionUsed1 = "c";
index1 = c1Index;
}
/*
and delete if in R1. (Of course the letter of the valid s-ending is
not necessarily in R1.) If an ending of group (b) is deleted, and the ending
is preceded by niss, delete the final s.
(For example, äckern -> äck, ackers -> acker, armes -> arm,
bedürfnissen -> bedürfnis)
*/
if (index1 != 10000 && r1Index != -1) {
if (index1 >= r1Index) {
word = word.substring(0, index1);
if (optionUsed1 == "b") {
if (word.search(/niss$/) != -1) {
word = word.substring(0, word.length - 1);
}
}
}
}
/*
Step 2:
Search for the longest among the following suffixes,
(a) en er est
(b) st (preceded by a valid st-ending, itself preceded by at least 3
letters)
*/
var a2Index = word.search(/(en|er|est)$/g);
var b2Index = word.search(/(.{3}[bdfghklmnt]st)$/g);
if (b2Index != -1) {
b2Index += 4;
}
var index2 = 10000;
var optionUsed2 = "";
if (a2Index != -1 && a2Index < index2) {
optionUsed2 = "a";
index2 = a2Index;
}
if (b2Index != -1 && b2Index < index2) {
optionUsed2 = "b";
index2 = b2Index;
}
/*
and delete if in R1.
(For example, derbsten -> derbst by step 1, and derbst -> derb by
step 2, since b is a valid st-ending, and is preceded by just 3 letters)
*/
if (index2 != 10000 && r1Index != -1) {
if (index2 >= r1Index) {
word = word.substring(0, index2);
}
}
/*
Step 3: d-suffixes (*)
Search for the longest among the following suffixes, and perform the
action indicated.
end ung
delete if in R2
if preceded by ig, delete if in R2 and not preceded by e
ig ik isch
delete if in R2 and not preceded by e
lich heit
delete if in R2
if preceded by er or en, delete if in R1
keit
delete if in R2
if preceded by lich or ig, delete if in R2
*/
var a3Index = word.search(/(end|ung)$/g);
var b3Index = word.search(/[^e](ig|ik|isch)$/g);
var c3Index = word.search(/(lich|heit)$/g);
var d3Index = word.search(/(keit)$/g);
if (b3Index != -1) {
b3Index++;
}
var index3 = 10000;
var optionUsed3 = "";
if (a3Index != -1 && a3Index < index3) {
optionUsed3 = "a";
index3 = a3Index;
}
if (b3Index != -1 && b3Index < index3) {
optionUsed3 = "b";
index3 = b3Index;
}
if (c3Index != -1 && c3Index < index3) {
optionUsed3 = "c";
index3 = c3Index;
}
if (d3Index != -1 && d3Index < index3) {
optionUsed3 = "d";
index3 = d3Index;
}
if (index3 != 10000 && r2Index != -1) {
if (index3 >= r2Index) {
word = word.substring(0, index3);
var optionIndex = -1;
var optionSubsrt = "";
if (optionUsed3 == "a") {
optionIndex = word.search(/[^e](ig)$/);
if (optionIndex != -1) {
optionIndex++;
if (optionIndex >= r2Index) {
word = word.substring(0, optionIndex);
}
}
} else if (optionUsed3 == "c") {
optionIndex = word.search(/(er|en)$/);
if (optionIndex != -1) {
if (optionIndex >= r1Index) {
word = word.substring(0, optionIndex);
}
}
} else if (optionUsed3 == "d") {
optionIndex = word.search(/(lich|ig)$/);
if (optionIndex != -1) {
if (optionIndex >= r2Index) {
word = word.substring(0, optionIndex);
}
}
}
}
}
/*
Finally,
turn U and Y back into lower case, and remove the umlaut accent from
a, o and u.
*/
word = word.replace(/U/g, "u");
word = word.replace(/Y/g, "y");
word = word.replace(/ä/g, "a");
word = word.replace(/ö/g, "o");
word = word.replace(/ü/g, "u");
return word;
}
function vector(tokens) {
const vector = {};
/*for (const token of tokens) {
vector[token] = (vector[token] || 0) + 1;
}*/
for (token of tokens) {
if (token.length > 3) {
for (let i = 0; i < token.length - 3; i++) {
let trigram = token.substring(i, i + 3);
vector[trigram] = (vector[trigram] || 0) + 1;
}
} else {
vector[token] = (vector[token] || 0) + 1;
}
}
normalizeVector(vector);
return vector;
}
function dotProduct(vector1, vector2) {
let product = 0;
for (const key in vector1) {
if (vector2.hasOwnProperty(key)) {
product += vector1[key] * vector2[key];
}
}
return product;
}
function addVector(vector1, vector2) {
for (const key in vector2) {
vector1[key] = (vector1[key] || 0) + vector2[key];
}
}
function scaleVector(vector, scalar) {
for (const key in vector) {
vector[key] *= scalar;
}
}
function normalizeVector(vector) {
const len = magnitude(vector);
for (const key in vector) {
vector[key] /= len;
}
}
function magnitude(vector) {
let sumOfSquares = 0;
for (const key in vector) {
sumOfSquares += vector[key] ** 2;
}
return Math.sqrt(sumOfSquares);
}
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
function similaritySortItems(items) {
if (items.length == 0) return items;
sortedItems = [items.shift()];
let refItem = sortedItems[0];
while (items.length > 0) {
let maxSimilarity = -1;
let similarItem = null;
let similarItemIdx = -1;
items.forEach((item, idx) => {
let similarity = dotProduct(refItem.vector, item.vector);
if (similarity > maxSimilarity) {
maxSimilarity = similarity;
similarItem = item;
similarItemIdx = idx;
}
});
sortedItems.push(similarItem);
items.splice(similarItemIdx, 1);
refItem = similarItem;
}
return sortedItems;
}
function vectorizeItems(items) {
items.forEach((item) => {
let name = item.name
.toLowerCase()
.replace(/[^\w\s]|_/g, "")
.replace("-", " ");
item.tokens = name.split(/\s+/).map((token) => stem(token));
if (item.quantity) item.tokens.push("" + item.quantity);
if (item.unit) item.tokens.push(item.unit);
item.vector = vector(item.tokens);
});
}
function cluster(items, maxTime) {
if (!maxTime) maxTime = 0.25;
// Tokenize, stem, and vectorize item names
vectorizeItems(items);
// Split by store and sort by number of items in descending order
const itemsPerStore = [];
for (const store of STORE_KEYS) {
const storeItems = items.filter((item) => item.store === store);
if (storeItems.length > 0) itemsPerStore.push(storeItems);
}
itemsPerStore.sort((a, b) => b.length - a.length);
itemsPerStore.forEach((items) =>
console.log(items[0].store + ", " + items.length)
);
// Take the store with the most items, then try to find the best match
// from each of the other stores
const baseStore = itemsPerStore.shift();
itemsPerStore.push(baseStore);
const otherItems = itemsPerStore.flat();
let clusters = [];
for (item of baseStore) {
clusters.push({ centroid: deepCopy(item.vector), item, items: [] });
}
const now = performance.now();
let maxIterations = 100;
while (maxIterations-- > 0) {
for (item of otherItems) {
let maxSimilarity = -1;
let nearestCluster = null;
for (cluster of clusters) {
const similarity = dotProduct(cluster.centroid, item.vector);
if (similarity > maxSimilarity) {
maxSimilarity = similarity;
nearestCluster = cluster;
item.similarity = similarity;
}
}
nearestCluster.items.push(item);
}
const newClusters = [];
for (cluster of clusters) {
const newCluster = { centroid: {}, item: cluster.item, items: [] };
for (item of cluster.items) {
addVector(newCluster.centroid, item.vector);
}
if (cluster.items.length > 0) {
scaleVector(newCluster.centroid, 1 / cluster.items.length);
normalizeVector(newCluster.centroid);
}
newClusters.push(newCluster);
}
const time = (performance.now() - now) / 1000;
console.log(maxIterations + ", time " + time);
if (
JSON.stringify(clusters) == JSON.stringify(newClusters) ||
maxIterations == 1 ||
time > maxTime
) {
break;
}
clusters = newClusters;
}
const finalClusters = [];
for (cluster of clusters) {
cluster.items = similaritySortItems(cluster.items);
finalClusters.push(cluster);
}
return finalClusters;
}
function flattenClusters(clusters) {
const items = [];
for (cluster of clusters) {
for (item of cluster.items) {
items.push(item);
}
}
return items;
}
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
}
try {
exports.vector = vector;
exports.dotProduct = dotProduct;
exports.addVector = addVector;
exports.scaleVector = scaleVector;
exports.normalizeVector = normalizeVector;
exports.stem = stem;
exports.cluster = cluster;
exports.flattenClusters = flattenClusters;
exports.vectorizeItems = vectorizeItems;
exports.similaritySortItems = similaritySortItems;
} catch (e) {
// hax
}