Spaces:
Running
Running
File size: 1,360 Bytes
8b713eb e53a648 8b713eb | 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 | /** Node-only helper that loads shipped binaries into a SearchEngine for tests and benchmarks. */
import { gunzipSync } from 'node:zlib';
import { readFileSync } from 'node:fs';
import { SearchEngine } from '../src/lib/search/engine.ts';
export interface LoadedDataset {
engine: SearchEngine;
}
function gz(dir: string, name: string): Uint8Array {
return gunzipSync(readFileSync(`${dir}/${name}`));
}
function int8(dir: string, name: string): Int8Array {
const bytes = readFileSync(`${dir}/${name}`);
return new Int8Array(bytes.buffer, bytes.byteOffset, bytes.length);
}
function lines(bytes: Uint8Array): string[] {
return new TextDecoder().decode(bytes).split('\n');
}
export function loadDataset(dir: string): LoadedDataset {
const manifest = JSON.parse(readFileSync(`${dir}/manifest.json`, 'utf8')) as { dims: number };
const engine = new SearchEngine({
dims: manifest.dims,
subjects: lines(gz(dir, 'subjects.txt.z')),
termsA: lines(gz(dir, 'terms-a.txt.z')),
vecA: int8(dir, 'vec-a.bin'),
freqA: gz(dir, 'freq-a.bin.z'),
vecS: int8(dir, 'vec-s.bin')
});
engine.setGeneralVocabulary(lines(gz(dir, 'terms-b.txt.z')), int8(dir, 'vec-b.bin'));
const starts = gz(dir, 'catalog-subjects.bin.z');
engine.setCatalog(new Uint32Array(starts.slice().buffer));
engine.setCaptions(gz(dir, 'catalog-captions.bin.z'));
return { engine };
}
|