Skip to content

Commit

Permalink
requote: option to write erred example to the same output file (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehrad0711 committed Oct 27, 2021
1 parent f729095 commit b68f2e2
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tool/requote.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export function initArgparse(subparsers) {
});
parser.add_argument('-o', '--output', {
required: true,
type: fs.createWriteStream
type: String
});
parser.add_argument('--contextual', {
action: 'store_true',
Expand Down Expand Up @@ -475,7 +475,7 @@ export function initArgparse(subparsers) {
default: 'en-US'
});
parser.add_argument('--output-errors', {
type: fs.createWriteStream,
type: String,
help: 'If provided, examples which fail to be requoted are written in this file as well as being printed to stdout '
});
}
Expand All @@ -485,17 +485,18 @@ export async function execute(args) {

let outputErrors = null;
const output = new DatasetStringifier();
promises.push(StreamUtils.waitFinish(output.pipe(args.output)));
if (args.output_errors) {
promises.push(StreamUtils.waitFinish(output.pipe(fs.createWriteStream(args.output))));
if (args.output_errors && args.output !== args.output_errors) {
outputErrors = new DatasetStringifier();
promises.push(StreamUtils.waitFinish(outputErrors.pipe(args.output_errors)));
promises.push(StreamUtils.waitFinish(outputErrors.pipe(fs.createWriteStream(args.output_errors))));
}

readAllLines(args.input_file)
.pipe(new DatasetParser({ contextual: args.contextual, preserveId: true, parseMultiplePrograms: true }))
.pipe(new Stream.Transform({
objectMode: true,
transform(ex, encoding, callback) {
ex.is_ok = true;
try {
let requoted_programs = [];
let requoted_sentences = [];
Expand All @@ -508,18 +509,19 @@ export async function execute(args) {
}
ex.preprocessed = requoted_sentences[0];
ex.target_code = requoted_programs;
ex.is_ok = true;

this.push(ex);
callback();
} catch(e) {
console.error('**************');
console.error('Failed to requote');
console.error(ex.id);
console.error(String(e));
console.error(ex.preprocessed);
console.error(ex.target_code);
console.error('**************');
ex.is_ok = false;
if (outputErrors)
// send to output_errors
ex.is_ok = false;
if (args.skip_errors) {
this.push(ex);
callback();
Expand Down

0 comments on commit b68f2e2

Please sign in to comment.