I'm having a huge difficulty in creating a program to check the number of occurrences of a document based on rules set by me. With the help of regex, I check some fields, and if a particular field exists , I can count the number of occurrences of it, or I create a deeper scan. It's a little confusing, and I do not know exactly how to explain.
I 'm checking text files, but to reduce the complexity , I will use arrays.
I have the following array:
let strings = [
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME2 ID: 10'
];
And this is the desire output:
{
'NAME' : { '12': 3 },
'NAME2': { '10': 1 }
}
To achieve this, I need to do some checks, so I came up with the following 'MAP':
let patterns = [
{
'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
'modifier': ''
},
{
'pattern' : 'ID:\\s*?(\\d{2})\\s*',
'modifier' : ''
}
];
I 'm having a hard time creating the pseudo- code, I know it's something that can be done recursively, but I'm stuck . The biggest problem is because of nested, I can have several levels of nested, not necessarily two.
In the last hours I created the following code:
'use strict';
let patterns = [
{
'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
'modifier': ''
},
{
'pattern' : 'ID:\\s*?(\\d{2})\\s*',
'modifier' : ''
}
];
let strings = [
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME ID: 12',
'COMPANY: NAME2 ID: 10'
];
var _data = {};
for (let string of strings) {
var root = _data;
for (let i = 0, length = patterns.length; i < length; i++) {
let item = patterns[i];
let regex = new RegExp(item.pattern, item.modifier);
let result = regex.exec(string);
if (i < patterns.length -1) {
root = root[result[1]] = {};
} else {
root = root[result[1]] = 1;
}
}
}
document.body.innerHTML = JSON.stringify({_data});
Now i'm trying to get the last part, count the number of occurrences, which is being a pain in the ass. Maybe recursion or generator could resolve this.
UPDATE -
It's important understand that should work with 3, 4, 5, objects. Example:
let patterns = [
{
'pattern': 'COMPANY:\\s*?([\\w]+)\\s',
'modifier': ''
},
{
'pattern' : 'ID:\\s*?(\\d{2})\\s*',
'modifier' : ''
},
{
'pattern' : 'SOMETHING:\\s*?(\\d+)\\s*',
'modifier' : ''
}
];
let strings = [
'COMPANY: NAME ID: 12 SOMETHING: 1010',
'COMPANY: NAME ID: 12 SOMETHING: 1010',
'COMPANY: NAME ID: 12 SOMETHING: 1010',
'COMPANY: NAME2 ID: 10 SOMETHING: 1010'
];
Output should be:
{
'NAME': {
'12': {
'1010': 3
}
},
'NAME2': {
'10': {
'1010': 1
}
}
}