import { readFile, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { parseArgs } from 'node:util' import { sideOutcome, tally } from './search-oracle.mjs' const { values: options } = parseArgs({ options: { questions: { type: 'string' }, out: { type: 'string' }, side: { type: 'string', multiple: true }, }, }) if (options.questions === undefined || options.out === undefined || options.side === undefined) { throw new Error('Pass --questions, --out, and at least one --side label=path.json.') } const questions = (await readFile(options.questions, 'utf8')) .split('\n') .filter(line => line.trim().length > 0) .map(line => JSON.parse(line)) const sides = [] for (const entry of options.side) { const cut = entry.indexOf('=') if (cut <= 0) throw new Error(`Write each side as label=path.json, not ${entry}.`) const label = entry.slice(0, cut) const answers = JSON.parse(await readFile(entry.slice(cut + 1), 'utf8')) if (answers.length !== questions.length) throw new Error(`${label} answered ${answers.length} of ${questions.length}.`) answers.forEach((answer, index) => { if (answer.question !== questions[index]?.question) throw new Error(`${label} answers question ${index + 1} out of order.`) }) sides.push({ label, outcomes: answers.map((answer, index) => sideOutcome(questions[index], answer.results, answer.searchable)), }) } const folders = [...new Set(questions.map(question => question.file.split('/')[0]))].sort() const summary = sides.map(side => ({ label: side.label, all: tally(side.outcomes), byFolder: Object.fromEntries( folders.map(folder => [ folder, tally(side.outcomes.filter((_, index) => questions[index]?.file.startsWith(`${folder}/`))), ]), ), })) const row = (label, cells) => `| ${label} | ${cells.join(' | ')} |` const header = [ row( '', summary.map(side => side.label), ), row( '---', summary.map(() => '---'), ), ] const lines = [ '# Search against search, 10 results each', '', ...header, row( 'Answer first', summary.map(side => `${side.all.withinTop1} of ${side.all.questions}`), ), row( 'Answer in the top 3', summary.map(side => `${side.all.withinTop3} of ${side.all.questions}`), ), row( 'Answer in the top 5', summary.map(side => `${side.all.withinTop5} of ${side.all.questions}`), ), row( 'Answer in the top 10', summary.map(side => `${side.all.withinTop10} of ${side.all.questions}`), ), row( 'Answer missing from the top 10', summary.map(side => `${side.all.missed} of ${side.all.questions}`), ), row( 'Answer in a file the search could not read', summary.map(side => `${side.all.fileNotSearchable}`), ), row( 'Characters returned per search, mean', summary.map(side => side.all.meanCharacters.toLocaleString('en-US')), ), '', '## Answer first, by folder', '', ...header, ...folders.map(folder => row( folder, summary.map(side => `${side.byFolder[folder].withinTop1} of ${side.byFolder[folder].questions}`), ), ), '', '## Rank of the answer for each question', '', row( 'Question', summary.map(side => side.label), ), row( '---', summary.map(() => '---'), ), ...questions.map((question, index) => row( question.question.replaceAll('|', '/'), sides.map(side => { const outcome = side.outcomes[index] if (outcome === undefined || !outcome.searchable) return 'cannot read file' return outcome.rank === null ? 'missing' : String(outcome.rank) }), ), ), '', ] const details = questions.map((question, index) => ({ question: question.question, file: question.file, ...Object.fromEntries(sides.map(side => [side.label, side.outcomes[index]])), })) await writeFile(join(options.out, 'summary.json'), `${JSON.stringify(summary, null, 2)}\n`, { mode: 0o600 }) await writeFile(join(options.out, 'results.json'), `${JSON.stringify(details, null, 2)}\n`, { mode: 0o600 }) await writeFile(join(options.out, 'summary.md'), lines.join('\n'), { mode: 0o600 }) process.stdout.write(`${lines.join('\n')}\n`)