2023-06-25 23:52:39 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
const analysis = require("./analysis");
|
|
|
|
const knn = require("./site/js/knn");
|
|
|
|
|
2023-06-26 02:46:22 +02:00
|
|
|
function similaritySort(items, filter, filterA, filterB) {
|
|
|
|
const filteredItems = items.filter(filter);
|
|
|
|
knn.vectorizeItems(filteredItems);
|
|
|
|
const itemsA = filteredItems.filter(filterA);
|
|
|
|
const itemsB = filteredItems.filter(filterB);
|
2023-06-25 23:52:39 +02:00
|
|
|
const sortedItems = [];
|
|
|
|
|
2023-06-26 02:46:22 +02:00
|
|
|
itemsB.forEach((item) => (item.sorted = false));
|
|
|
|
const total = itemsA.length;
|
|
|
|
while (itemsA.length > 0) {
|
|
|
|
const refItem = itemsA.shift();
|
|
|
|
const similar = knn.findMostSimilarItem(refItem, itemsB);
|
2023-06-25 23:52:39 +02:00
|
|
|
if (similar.item != null) {
|
|
|
|
sortedItems.push(refItem);
|
|
|
|
sortedItems.push(similar.item);
|
|
|
|
} else {
|
|
|
|
console.log("No similar item found for " + refItem.name);
|
|
|
|
}
|
|
|
|
if (sortedItems.length % 100 == 0) console.log(sortedItems.length / 2 + "/" + total);
|
|
|
|
}
|
2023-06-26 02:46:22 +02:00
|
|
|
return sortedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterSimilarItems(items) {
|
|
|
|
const filteredItems = [];
|
|
|
|
for (let i = 0; i < items.length; i += 2) {
|
|
|
|
const a = items[i];
|
|
|
|
const b = items[i + 1];
|
|
|
|
if (b.priceHistory.some((price) => price.price == a.price) && a.quantity == b.quantity) {
|
|
|
|
filteredItems.push(a);
|
|
|
|
filteredItems.push(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync("patterns")) fs.mkdirSync("patterns");
|
|
|
|
|
|
|
|
if (!fs.existsSync("patterns/sorted-billa-spar.json")) {
|
|
|
|
const items = analysis.readJSON("data/latest-canonical.json.br");
|
|
|
|
const sortedItems = similaritySort(
|
|
|
|
items,
|
|
|
|
(item) => {
|
|
|
|
if (!(item.store == "billa" || item.store == "spar")) return false;
|
|
|
|
return !["Clever", "S-BUDGET", "Ja! Natürlich", "SPAR", "BILLA"].some((str) => item.name.includes(str));
|
|
|
|
},
|
|
|
|
(item) => item.store === "billa",
|
|
|
|
(item) => item.store === "spar"
|
|
|
|
);
|
2023-06-25 23:52:39 +02:00
|
|
|
analysis.writeJSON("patterns/sorted-billa-spar.json", sortedItems);
|
|
|
|
}
|
|
|
|
|
2023-06-26 03:05:03 +02:00
|
|
|
if (!fs.existsSync("site/data/billa-spar-cart.json")) {
|
2023-06-26 02:46:22 +02:00
|
|
|
const sortedItems = analysis.readJSON("patterns/sorted-billa-spar.json");
|
|
|
|
const filteredItems = filterSimilarItems(sortedItems);
|
|
|
|
analysis.writeJSON("site/data/billa-spar-cart.json", {
|
|
|
|
name: "Markenprodukte Billa/Spar",
|
|
|
|
items: filteredItems.map((item) => {
|
|
|
|
return { store: item.store, id: item.id };
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2023-06-25 23:52:39 +02:00
|
|
|
|
2023-06-26 02:46:22 +02:00
|
|
|
if (!fs.existsSync("patterns/sorted-budget.json")) {
|
|
|
|
const items = analysis.readJSON("data/latest-canonical.json.br");
|
|
|
|
const sortedItems = similaritySort(
|
|
|
|
items,
|
|
|
|
(item) => {
|
|
|
|
if (!(item.store == "billa" || item.store == "spar")) return false;
|
|
|
|
return ["Clever", "S-BUDGET"].some((str) => item.name.includes(str));
|
|
|
|
},
|
|
|
|
(item) => item.store === "billa",
|
|
|
|
(item) => item.store === "spar"
|
|
|
|
);
|
|
|
|
analysis.writeJSON("patterns/sorted-budget.json", sortedItems);
|
|
|
|
}
|
|
|
|
|
2023-06-26 03:05:03 +02:00
|
|
|
if (!fs.existsSync("site/data/budget-cart.json")) {
|
2023-06-26 02:46:22 +02:00
|
|
|
const sortedItems = analysis.readJSON("patterns/sorted-budget.json");
|
|
|
|
const filteredItems = filterSimilarItems(sortedItems);
|
|
|
|
analysis.writeJSON("site/data/budget-cart.json", {
|
|
|
|
name: "Diskont-Marken Produkte Billa/Spar",
|
|
|
|
items: filteredItems.map((item) => {
|
|
|
|
return { store: item.store, id: item.id };
|
|
|
|
}),
|
|
|
|
});
|
2023-06-25 23:52:39 +02:00
|
|
|
}
|
2023-06-26 03:05:03 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync("patterns/sorted-bio.json")) {
|
|
|
|
const items = analysis.readJSON("data/latest-canonical.json.br");
|
|
|
|
const sortedItems = similaritySort(
|
|
|
|
items,
|
|
|
|
(item) => {
|
|
|
|
if (!(item.store == "billa" || item.store == "spar")) return false;
|
|
|
|
return ["Ja! Natürlich", "SPAR Natur*pur"].some((str) => item.name.includes(str));
|
|
|
|
},
|
|
|
|
(item) => item.store === "billa",
|
|
|
|
(item) => item.store === "spar"
|
|
|
|
);
|
|
|
|
analysis.writeJSON("patterns/sorted-bio.json", sortedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync("site/data/bio-cart.json")) {
|
|
|
|
const sortedItems = analysis.readJSON("patterns/sorted-bio.json");
|
|
|
|
const filteredItems = filterSimilarItems(sortedItems);
|
|
|
|
analysis.writeJSON("site/data/bio-cart.json", {
|
|
|
|
name: "Bio Eigenmarken Produkte Billa/Spar",
|
|
|
|
items: filteredItems.map((item) => {
|
|
|
|
return { store: item.store, id: item.id };
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2023-06-26 03:18:57 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync("patterns/sorted-midrange.json")) {
|
|
|
|
const items = analysis.readJSON("data/latest-canonical.json.br");
|
|
|
|
const sortedItems = similaritySort(
|
|
|
|
items,
|
|
|
|
(item) => {
|
|
|
|
if (!(item.store == "billa" || item.store == "spar")) return false;
|
|
|
|
return (
|
|
|
|
["BILLA", "SPAR"].some((str) => item.name.includes(str)) &&
|
|
|
|
!["Ja! Natürlich", "SPAR Natur*pur"].some((str) => item.name.includes(str))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(item) => item.store === "billa",
|
|
|
|
(item) => item.store === "spar"
|
|
|
|
);
|
|
|
|
analysis.writeJSON("patterns/sorted-midrange.json", sortedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync("site/data/midrange-cart.json")) {
|
|
|
|
const sortedItems = analysis.readJSON("patterns/sorted-midrange.json");
|
|
|
|
const filteredItems = filterSimilarItems(sortedItems);
|
|
|
|
analysis.writeJSON("site/data/midrange-cart.json", {
|
|
|
|
name: "Mittelpreisige Eigenmarken Produkte Billa/Spar",
|
|
|
|
items: filteredItems.map((item) => {
|
|
|
|
return { store: item.store, id: item.id };
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|