TelegramPWA/src/util/languageDetection.ts
2023-02-28 18:47:26 +01:00

39 lines
1.2 KiB
TypeScript

import type { FastTextApi } from '../lib/fasttextweb/fasttext.worker';
import type { Connector } from './PostMessageConnector';
import { createConnector } from './PostMessageConnector';
import Deferred from './Deferred';
const WORKER_INIT_DELAY = 4000;
const DEFAULT_THRESHOLD = 0.2;
const DEFAULT_LABELS_COUNT = 5;
let worker: Connector<FastTextApi> | undefined;
const initializationDeferred = new Deferred();
setTimeout(initWorker, WORKER_INIT_DELAY);
function initWorker() {
if (!worker) {
worker = createConnector<FastTextApi>(
new Worker(new URL('../lib/fasttextweb/fasttext.worker.ts', import.meta.url)),
);
initializationDeferred.resolve();
}
}
export async function detectLanguage(text: string, threshold = DEFAULT_THRESHOLD) {
if (!worker) await initializationDeferred.promise;
const result = await worker!.request({ name: 'detectLanguage', args: [text, threshold] });
return result;
}
export async function detectLanguageProbability(
text: string, labelsCount = DEFAULT_LABELS_COUNT, threshold = DEFAULT_THRESHOLD,
) {
if (!worker) await initializationDeferred.promise;
const result = await worker!.request({ name: 'detectLanguageProbability', args: [text, labelsCount, threshold] });
return result;
}