Last active
March 9, 2016 15:14
-
-
Save nodaguti/c735d7762d2b7995699c to your computer and use it in GitHub Desktop.
center-grammar-print-answers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* http://karino2.livejournal.com/378477.html | |
* にて公開されているセンター試験英語第2問のデータから,正答を埋めた完成文を出力する. | |
* Usage: | |
* i) スクリプトと同じ階層に center-grammar.csv として CSV データを配置する. | |
* ii) (必要ならば) 本スクリプトを Babel で変換する. | |
* iii) 実行する.結果は標準出力で出力される. | |
*/ | |
import fs from 'fs'; | |
import path from 'path'; | |
import csv from 'csv'; | |
const csvPath = path.join(__dirname, 'center-grammar.csv'); | |
new Promise((resolve, reject) => { | |
const input = fs.createReadStream(csvPath); | |
const parser = csv.parse(); | |
const transformer = csv.transform((record) => { | |
const [ | |
year, | |
, | |
, | |
question, | |
choicesStr, | |
answersStr, | |
type, | |
] = record; | |
const [ | |
choices, | |
answers, | |
] = [ | |
choicesStr.split(','), | |
answersStr.split(',').map((ansStr) => Number(ansStr)), | |
]; | |
if (Number(type) > 2) return null; | |
const [wareki, conductType] = year.split('-').map((item) => Number(item)); | |
const seireki = Number(wareki) + 1988; | |
const conductLabel = Number(conductType) === 1 ? '本試' : '追試'; | |
let blankIndex = 0; | |
const sentence = question.replace(/#@\d+/g, () => { | |
const ansIndex = answers[blankIndex] - 1; | |
blankIndex++; | |
return choices[ansIndex].replace(/(?:^"|"$)/g, ''); | |
}); | |
return `${sentence} (センター${conductLabel}, ${seireki})`; | |
}); | |
const questions = []; | |
input.pipe(parser) | |
.pipe(transformer) | |
.on('data', (row) => questions.push(row)) | |
.on('end', () => resolve(questions)) | |
.on('error', (err) => reject(err)); | |
}).then((questions) => { | |
questions.forEach((question) => console.log(question)); | |
}).catch((err) => console.error(err.stack)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment