Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #11

Merged
merged 26 commits into from
Jul 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Separate out readAndProcessLines
MarvNC committed Jul 12, 2024
commit 8944b1f2419f82c86526f3aef2b050ff7cab95c7
50 changes: 1 addition & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { file } from 'bun';
import { Dictionary } from 'yomichan-dict-builder';

import { getVersion } from './util/getVersion';

import * as cliProgress from 'cli-progress';
import { downloadDumps } from './util/downloadDumps';
import { readArgs } from './util/readArgs';
import { processLine } from './yomitan/processLine';
import { readAndProcessLines } from './util/readAndProcessLines';

const outputZipName = (lang: string, date: string, version: string) =>
`${lang} Wikipedia [${date}] (v${version}).zip`;
@@ -49,49 +47,3 @@ const OUT_DIRECTORY = './out';
})().catch((e) => {
console.error(e);
});

async function readAndProcessLines(
filePath: string,
dict: Dictionary,
lang: string,
dev: boolean
) {
const fileHandle = file(filePath);
const fileReader = fileHandle.stream();
const lineReader = fileReader.getReader();
let processedLines = 0;
let buffer = '';
const progressBar = new cliProgress.SingleBar({
format: `{value} lines | {file}`,
});

progressBar.start(0, 0, {
file: filePath,
});

while (true) {
const { done, value } = await lineReader.read();
if (done) break;

buffer += new TextDecoder().decode(value);
let lineEnd;

while ((lineEnd = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, lineEnd);

processLine(line.trim(), dict, lang);

buffer = buffer.slice(lineEnd + 1);
processedLines++;

progressBar.update(processedLines);

if (processedLines >= 1000 && dev) {
return processedLines;
}
}
}

progressBar.stop();
return processedLines;
}
50 changes: 50 additions & 0 deletions src/util/readAndProcessLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { file } from 'bun';
import { Dictionary } from 'yomichan-dict-builder';
import * as cliProgress from 'cli-progress';
import { processLine } from '../yomitan/processLine';

export async function readAndProcessLines(
filePath: string,
dict: Dictionary,
lang: string,
dev: boolean
) {
const fileHandle = file(filePath);
const fileReader = fileHandle.stream();
const lineReader = fileReader.getReader();
let processedLines = 0;
let buffer = '';
const progressBar = new cliProgress.SingleBar({
format: `{value} lines | {file}`,
});

progressBar.start(0, 0, {
file: filePath,
});

while (true) {
const { done, value } = await lineReader.read();
if (done) break;

buffer += new TextDecoder().decode(value);
let lineEnd;

while ((lineEnd = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, lineEnd);

processLine(line.trim(), dict, lang);

buffer = buffer.slice(lineEnd + 1);
processedLines++;

progressBar.update(processedLines);

if (processedLines >= 1000 && dev) {
return processedLines;
}
}
}

progressBar.stop();
return processedLines;
}