Skip to content
Snippets Groups Projects
Commit 4355fd0c authored by Millicent Billette's avatar Millicent Billette
Browse files

WiP: dictionary stuff (dictionary-tree)

parent 978f4bad
No related branches found
No related tags found
No related merge requests found
import * as tree from './dictionary-tree.mjs';
export function parse(dictionaryString) {
const allLines = dictionaryString.split('\n');
const parsedLines = parseAllLines(allLines);
return combineUnspecifiedLines2IdSecPwdLines(parsedLines);
return combineUnspecified2IdSecPwd(parsedLines);
}
function parseAllLines(lines) {
return lines;
}
function parseLine(theLine, allLines) {
return theLine;
}
function combineUnspecifiedLines2IdSecPwdLines(lines){
const unspecifiedLines = [];
function combineUnspecified2IdSecPwd(lines) {
const idSecPwdLines = [];
lines.forEach(line=>line.includes('@@@@') ? idSecPwdLines.push(line) : unspecifiedLines.push(line));
const unspecifiedLines = [];
lines.forEach(line => line.includes('@@@@') ? deepUnspecifiedExtractor(line, unspecifiedLines, idSecPwdLines) : unspecifiedLines.push(line));
if (unspecifiedLines.length) idSecPwdLines.push(`${mergeLines(unspecifiedLines)}@@@@${mergeLines(unspecifiedLines)}`)
return mergeLines(idSecPwdLines);
}
function deepUnspecifiedExtractor(line, unspecifiedLines, idSecPwdLines) {
const hybridTree = tree.buildTreeStruct(line);
/*
buildTree (à créer dans un module à part)
parcours en profondeur jusqu'à trouver des branches unspecified.
extraction de celles-ci pour les ajouter (avec leur chaine de parents) aux unspecifiedlines
ajout de l'arbre restant aux idSecPwdLines
*/
idSecPwdLines.push(line);
}
function mergeLines(lines) {
console.log('lines:',lines);
//console.log('lines:',lines);
if (!lines.length) return '';
lines = lines.filter(l => l.length);
if (lines.length === 1) return lines[0];
return `(${lines.join('|')})`;
}
/*
const json1 = '(a(b@@@@c|d)|(e|f)|g|h@@@@i)'
const json2 = {
'a':'a'{
'b@@@@c':'b@@@@c',
'd':'d'
},
{
'e':'e',
'f':'f'
},
'g':'g',
'h@@@@i':'h@@@@i'
}
*/
......@@ -16,5 +16,5 @@ h@@@@(i|j)
`), '(a@@@@(b|c)|h@@@@(i|j)|((d|e)|(f|g))@@@@((d|e)|(f|g)))');
});
test.skip('no @@@@ cases combined with @@@@ case on same line recombine to allways finish with @@@@ case', t => {
t.is(app.parse('(a|b|c@@@@d)'), '(c@@@@d|(a|b)@@@@(a|b))');
t.is(app.parse('(a(b@@@@c|d)|(e|f)|g|h@@@@i)'), '(g@@@@h|(a|b|c)@@@@(a|b|c))');
});
......@@ -2,10 +2,10 @@ export function build(dictionaryString) {
}
export function buildTreeStruct(monoLineString) {
console.log('srcTree', monoLineString);
//console.log('srcTree', monoLineString);
const stringAsArray = monoLineString.split('');
const rawTree = leftParser(stringAsArray);
console.log('rawTree', JSON.stringify(rawTree));
//console.log('rawTree', JSON.stringify(rawTree));
const outOfScope = stringAsArray.length;
if (outOfScope) throw `fail to build tree from : "${monoLineString}" parsed: ${JSON.stringify(rawTree)} unparsed/failed: ${stringAsArray.join('')}`;
//return rawTree;//trivialDedup(flattenTree(rawTree))
......@@ -19,23 +19,33 @@ function flushNoEmptyString(data){
if (data.str.length) data.tree[data.tree.length - 1].step.push(data.str);
data.str = '';
}
function appendToString(chr, data) {
data.str += chr;
}
function nextAlt(data) {
data.tree.push({step: []});
}
function goIntoNewAlt(data) {
data.tree[data.tree.length - 1].step.push(leftParser(data.input));
}
function returnTrue(){return true;}
function doTheses(){}
function returnTrue() {
return true;
}
function doTheses() {
}
const leftParserFunc = {
')': (chr, data) => returnTrue(flushNoEmptyString(data)),
'|': (chr, data) => doTheses(flushNoEmptyString(data), nextAlt(data)),
'(': (chr, data) => doTheses(flushNoEmptyString(data), goIntoNewAlt(data)),
'default': (chr, data) => appendToString(chr, data)
}
function leftParser(inputChrArray) {
const data = {
input: inputChrArray,
......@@ -52,6 +62,7 @@ function leftParser(inputChrArray) {
flushNoEmptyString(data);
return data.tree;
}
// end leftParser
function concatStrings(array) {
const data = {
......@@ -69,6 +80,7 @@ function concatStrings(array){
flushNoEmptyString(data)
return data.tree[data.tree.length - 1].step;
}
function flattenTree(tree) {
if (typeof tree === 'string') return tree;
if (Array.isArray(tree) && tree.length === 0) return '';
......@@ -101,6 +113,7 @@ function flattenTree(tree) {
function isStep(element) {
return typeof element === 'object' && typeof element.step !== 'undefined';
}
function trivialDedup(tree) {
if (typeof tree === 'string') return tree;
if (isStep(tree)) return {step: tree.step.map(subTree => trivialDedup(subTree))};
......@@ -111,3 +124,10 @@ function trivialDedup(tree) {
}
throw `unknown case : ${tree}`;
}
export function serialize(treeStruct) {
if (typeof treeStruct === 'string') return treeStruct;
if (Array.isArray(treeStruct)) return `(${treeStruct.map(serialize).join('|')})`;
if (isStep(treeStruct)) return treeStruct.step.map(serialize).join('');
throw new Error(`Error: how to serialize ${JSON.stringify(treeStruct)} RAW: ${treeStruct}`);
}
......@@ -13,16 +13,8 @@ test('(|b) empty choice is choice', t=>t.is(buildTreeStruct('(|b)'),expected([''
test('(b|b) trivial dedup', t => t.is(buildTreeStruct('(|b||b|)'), expected(['', 'b'])));
test('a(b|c) ordered part in step obj', t => t.is(buildTreeStruct('a(b|c)'), expected({step: ['a', ['b', 'c']]})));
test('a(b) flat merge when no alt', t => t.is(buildTreeStruct('a(b)'), expected('ab')));
test('build tree with (|) pattern', t => t.is(buildTreeStruct('(a(|b@@@@c|d|)|(e|f)|g|h@@@@i)'),
expected([
{step:['a',[
'',
'b@@@@c',
'd'
]]},
'e',
'f',
'g',
'h@@@@i'
])));
test('build complexe tree with (|) pattern', t => t.is(buildTreeStruct('(a(|b@@@@c|d|)|(e|f)|g|h@@@@i)'),
expected([{step: ['a', ['', 'b@@@@c', 'd']]}, 'e', 'f', 'g', 'h@@@@i'])
));
test('serialize tree to a(b|c)', t => t.is(app.serialize({step: ['a', ['b', 'c']]}), 'a(b|c)'));
test('serialize incorrect tree throw', t => t.throws(() => app.serialize({plop: ['a']})));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment