This repository was archived by the owner on Sep 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
51 lines (46 loc) · 1.36 KB
/
parser.js
File metadata and controls
51 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function intiRec(str){
textToBeParsed = str;
parsed = textToBeParsed.replace(/[^a-z A-Z0-9.$]/g, '');
parsedArray = parsed.split(' ').map(word => word.toLowerCase());
console.log('parsed array', parsedArray);
[prices, priceIndices] = recognizePrices(parsedArray);
console.log('prices: ', prices);
console.log('price indices: ', priceIndices);
document.getElementById("ocr_results")
.innerText = parsed;
}
function recognizePrices(arr){
let fin = [];
let finInd = [];
for (let i in arr){
let val = arr[i];
if (val.indexOf('.') !== -1){
let testArr = val.split('.');
if (testArr.length === 2 && testArr[1].length === 2 && Number(testArr[0])){
fin.push(Number(val));
finInd.push(Number(i));
}
}
}
return [fin, finInd];
}
function isPrice(price){
let splitP = price.split('.');
return splitP.length === 2 && splitP[1].length === 2 && Number(splitP[0]) && price.indexOf('.') !== -1;
}
// function recognizeItems(arr){
// }
// input : {9: [1, 2, 3], 6: [1,3, 2], 3: [3], 4: [2,1 ,3]}
// output : { '1': 6.333333333333333, '2': 6.333333333333333, '3': 9.333333333333332 }
// function divideTotal(items){
// let divided = {};
// for (let i of Object.keys(items)){
// for (let j of items[i]){
// if (!divided[j]){
// divided[j] = 0;
// }
// divided[j]+= Number(i)/parseFloat(items[i].length);
// }
// }
// return divided;
// };