-
Notifications
You must be signed in to change notification settings - Fork 35
/
extract-contexts.js
84 lines (75 loc) · 2.69 KB
/
extract-contexts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2019 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details
"use strict";
const seedrandom = require('seedrandom');
const fs = require('fs');
const Tp = require('thingpedia');
const ThingTalk = require('thingtalk');
const { DatasetParser } = require('../lib/dataset-parsers');
const ContextExtractor = require('../lib/context-extractor');
const StreamUtils = require('../lib/stream-utils');
const { maybeCreateReadStream, readAllLines } = require('./lib/argutils');
module.exports = {
initArgparse(subparsers) {
const parser = subparsers.addParser('extract-contexts', {
addHelp: true,
description: "Extract normalized contexts from a non-contextual dataset."
});
parser.addArgument(['-o', '--output'], {
required: true,
type: fs.createWriteStream
});
parser.addArgument(['-l', '--locale'], {
required: false,
defaultValue: 'en-US',
help: `BGP 47 locale tag of the language to generate (defaults to 'en-US', English)`
});
parser.addArgument('--thingpedia', {
required: true,
help: 'Path to ThingTalk file containing class definitions.'
});
parser.addArgument('input_file', {
nargs: '+',
type: maybeCreateReadStream,
help: 'Input datasets to extract contexts from (in TSV format); use - for standard input'
});
parser.addArgument('--debug', {
nargs: 0,
action: 'storeTrue',
help: 'Enable debugging.',
defaultValue: true
});
parser.addArgument('--no-debug', {
nargs: 0,
action: 'storeFalse',
dest: 'debug',
help: 'Disable debugging.',
});
parser.addArgument('--random-seed', {
defaultValue: 'almond is awesome',
help: 'Random seed'
});
},
async execute(args) {
const rng = seedrandom.alea(args.random_seed);
const tpClient = new Tp.FileClient(args);
const schemas = new ThingTalk.SchemaRetriever(tpClient, null, !args.debug);
let allprograms = await readAllLines(args.input_file)
.pipe(new DatasetParser())
.pipe(new ContextExtractor(schemas, rng))
.read();
for (let prog of allprograms) {
args.output.write(prog);
args.output.write('\n');
}
args.output.end();
await StreamUtils.waitFinish(args.output);
}
};