Files
church-translate/app/_test-names-e2e.mjs
T

44 lines
2.2 KiB
JavaScript
Raw Normal View History

import { pipeline, env } from '@huggingface/transformers';
import { createRequire } from 'module';
import os from 'os'; import path from 'path'; import fs from 'fs'; import vm from 'vm';
const require = createRequire(import.meta.url);
const Liturgy = require('./liturgy.js'), Guard = require('./mt-guard.js');
const Preach = require('./preach.js'), B = require('./bible-lexicon.js');
const Spa = require('./spa-variants.js');
const src = fs.readFileSync('engine.js', 'utf8');
const sb = { console }; vm.createContext(sb);
vm.runInContext(src.slice(src.indexOf('const SCRIPT_NO_SPACES'), src.indexOf('function applyFixes')), sb);
env.allowLocalModels = false;
env.cacheDir = path.join(os.homedir(), 'Library/Application Support/Church Translate Models');
const pipe = await pipeline('translation', 'Xenova/nllb-200-distilled-600M', { dtype: 'fp32' });
/* The pipeline exactly as engine.js now runs it. */
async function translate(sentence, lang) {
const set = Liturgy.lookup(sentence, lang);
if (set) return set + ' [liturgy]';
const named = B.wholeUtterance(sentence, lang);
if (named) return named + ' [name lookup]';
const forMt = Preach.forTranslation(sentence);
const sd = Preach.stripShortPeriod(forMt);
const o = await pipe(sd.text, { src_lang: 'eng_Latn', tgt_lang: lang });
let raw = (Array.isArray(o) ? o[0] : o).translation_text.trim();
raw = Guard.clean(sd.text, raw).text;
raw = Preach.restorePeriod(raw, sd.hadPeriod);
if (lang === 'spa_Latn') raw = Spa.toLatinAmerican(Spa.toChurchRegister(raw));
const bl = B.forLanguage(lang);
if (bl) raw = sb.applyTable(raw, Object.keys(bl).map((f) => ({ from: f, to: bl[f] })));
const fx = B.fixesFor(lang);
if (fx) raw = sb.applyTable(raw, Object.keys(fx).map((f) => ({ from: f, to: fx[f] })));
return raw;
}
const CASES = ['Peter.','John.','Matthew.','Jesus.','Mary.','Noah.','Moses.','Habakkuk.',
'Simon Peter.','Donald Trump.','Amen.','Peter denied him three times.',
'His name was John.','Open up to the book of John.'];
for (const lang of ['spa_Latn','fra_Latn','por_Latn','zho_Hans']) {
console.log('\n=== ' + lang + ' ===');
for (const t of CASES) console.log(' ' + t.padEnd(32) + await translate(t, lang));
}