Created
March 12, 2019 10:13
-
-
Save erickedji/5d0fe77ca4f00a32ef88a5716b82d62c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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