Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickedji/5d0fe77ca4f00a32ef88a5716b82d62c to your computer and use it in GitHub Desktop.
Save erickedji/5d0fe77ca4f00a32ef88a5716b82d62c to your computer and use it in GitHub Desktop.
https://selectra.info/telecom/guides/comparatifs/telephones-mobiles
function getPhoneData(tableDivEl) {
try {
let rawData = Array.from(tableDivEl.querySelectorAll("tr"))
.map(tr => ({
key: tr.querySelector("th").textContent,
value: parseInt(tr
.querySelector("td")
.textContent
.replace(/\s+/g, " ")
.replace(/.*: ?/, "")
, 10)
}))
let input = {};
for (let item of rawData) {
if (item.value)
input[item.key] = item.value;
}
let paragraph = tableDivEl
let price;
let euroSymbolFound;
do {
if (!euroSymbolFound)
euroSymbolFound = paragraph.querySelector("img[alt=euro]")
let priceEl = paragraph.querySelector("strong");
if (priceEl && euroSymbolFound) {
let priceText = priceEl.textContent
let priceDigits = priceText.replace(/[^0-9]/g, "")
price = parseInt(priceDigits, 10)
}
paragraph = paragraph.nextElementSibling
} while (!price && paragraph)
return { input, output: { price } }
} catch (e) {
return null
}
}
function normalize(data) {
let max = {};
for (key in data[0])
max[key] = Math.max(...data.map(x => x[key]))
for (item of data) {
for (key in item) {
item[key] /= max[key]
if (item[key] !== 0 && !item[key])
delete item[key]
}
}
}
var trainingData = Array
.from(document.querySelectorAll("div.table-responsive"))
.map(getPhoneData)
.filter(x => !!x)
normalize(trainingData.map(d => d.input))
normalize(trainingData.map(d => d.output))
console.log(JSON.stringify(trainingData))
console.log(trainingData.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment