Page MenuHomePhabricator
Paste P8610

Script to load template mapping json to sqlite database
ActivePublic

Authored by santhosh on Jun 13 2019, 11:22 AM.
const fs = require('fs'),
ArgumentParser = require('argparse').ArgumentParser,
sqlite = require('sqlite'); // https://github.com/kriasoft/node-sqlite
async function createTemplate(db, from, to, templateName) {
const mapping = await db.get(`SELECT rowid FROM templates
WHERE source_lang = ? AND target_lang = ? AND template =?`,
from, to, templateName);
if (mapping && mapping.rowid) {
return mapping.rowid
}
const result = await db.run(`INSERT OR IGNORE INTO templates
(source_lang, target_lang, template) VALUES(?,?,?)`,
from, to, templateName);
return result.lastID;
}
async function main(databaseFile, mapping, from, to) {
const db = await sqlite.open(databaseFile, { Promise });
await db.run(`CREATE TABLE IF NOT EXISTS templates (
source_lang TEXT NOT NULL,
target_lang TEXT NOT NULL,
template TEXT NOT NULL,
UNIQUE(source_lang, target_lang, template)
)`
);
await db.run(`CREATE TABLE IF NOT EXISTS mapping (
template_mapping_id INTEGER NOT NULL,
source_param TEXT NOT NULL,
target_param TEXT NOT NULL,
score REAL NOT NULL,
UNIQUE(template_mapping_id, source_param, target_param)
)`);
for (const templateName in mapping) {
let mappingId, mappingData = mapping[templateName];
mappingId = await createTemplate(db, from, to, templateName);
console.log(`${mappingId} ${from} ${to} ${templateName}`);
for (let index in mappingData) {
let paramMapping = mappingData[index];
if (!mappingId || !paramMapping[from] || !paramMapping[to]) {
continue;
}
await db.run(`INSERT OR IGNORE INTO mapping
(template_mapping_id, source_param, target_param, score)
VALUES(?,?,?,?)`,
mappingId, paramMapping[from], paramMapping[to], paramMapping.d)
console.log(`${paramMapping[from]} -> ${paramMapping[to]} [${paramMapping.d}]`);
}
}
await db.close()
};
const argparser = new ArgumentParser({
addHelp: true,
description: 'Prepare template mapping database'
});
argparser.addArgument(
['-d', '--database'],
{
help: 'template mapping database file',
defaultValue: 'templatemapping.db'
}
);
argparser.addArgument(
['-i', '--input'],
{
help: 'JSON file with mapping.',
required: true
}
);
argparser.addArgument(
['--from'],
{
help: 'Source language',
required: true
}
);
argparser.addArgument(
['--to'],
{
help: 'Target language',
required: true
}
);
const args = argparser.parseArgs();
const databaseFile = args.database;
const input = args.input;
if (!fs.existsSync(input)) {
throw Error(`File ${input} does not exist`);
}
const mapping = JSON.parse(fs.readFileSync(input));
main(databaseFile, mapping, args.from,args.to)