Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F29506791
raw.txt
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
•
santhosh
Jun 13 2019, 11:22 AM
2019-06-13 11:22:59 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
raw.txt
View Options
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)
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
7625466
Default Alt Text
raw.txt (2 KB)
Attached To
Mode
P8610 Script to load template mapping json to sqlite database
Attached
Detach File
Event Timeline
Log In to Comment