// Create a new db and fill it with the CISP info // TODO: remove this step and create a persistent DB object // Using lokiJS, because it is enough and fast var db = new loki('db.json'); // CISP database collection var cisp = db.addCollection('cisp2db'); cisp.ensureUniqueIndex("Description"); cisp.ensureUniqueIndex("Code"); // Legal words database collection var words = db.addCollection('vocabulaire2db'); words.ensureUniqueIndex("Word"); function extractWords (a) { console.log("Done extracting words"); // Array of obj -> Array of arrays var r = a.map(function (o) { return [ o.Code, o.Description ]; }); // Array of arrays -> Array of words r = r.reduce(function (pv,cv,i,a) { return cv.concat(pv); },[]); // Array of words -> Array of objs { 'Words': … } return r.map(function (w) { return { 'Word': w }; }); } // Download CSV file in the background $.ajax({ url: "./cisp2fr.csv", async: true, success: function (csvd) { var o = $.csv.toObjects(csvd); cisp.insert(o); words.insert(extractWords(o)); }, dataType: "text", complete: function () { main(); } }); function displayMany (a) { var out = a.map(function (o) { var d = cisp.by("Description", o.Word); var ds = d ? ""+d.Code+"" + ""+d.Description+"" + ""+d.Thème+"" + ""+d.Catégorie+"" : ""; var cs = ""; if (ds != "") { var c = cisp.by("Code", o.Word); cs = c ? ""+c.Code+"" + ""+c.Description+"" + ""+c.Thème+"" + ""+c.Catégorie+"" : ""; } return ds + cs; }).reduce(function (pv,cv,i,a) { return pv + cv; },""); var table = ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += out; table += "
CodeDescriptionThèmeCatégorie
"; return table; } // Main application function main() { console.log(cisp.data); console.log(words.data); $( "#question" ).keyup(function () { var str = $( "#question" ).val(); var ans = words.find({'Word': { '$contains': str}}); console.log("Words found: ",ans); console.log(displayMany(ans)); $( "#answer" ).empty().append(displayMany(ans)); }); }