snacklish
Version:
Translator from English to Snacklish
138 lines (137 loc) • 3.53 kB
JavaScript
function b() {
return Math.random();
}
function W() {
return (n, { type: o, pattern: r }) => 0;
}
function k() {
return (n, { type: o, pattern: r }) => 1;
}
function d() {
return (n, { type: o, pattern: r }) => r.split(",")[0].length / 5;
}
function R() {
return (n, { type: o, pattern: r }) => {
const c = r.split(",")[0];
switch (o) {
case "exact":
return c.length / 6;
default:
return c.length / 7;
}
};
}
function S(n) {
const o = n.split(/[\r\n]+/).map((s) => s.trim()).filter((s) => s.length > 0), r = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map();
for (const s of o) {
s.startsWith("-") && (s.includes("*") ? c.add(s.slice(1)) : r.add(s.slice(1)));
const [a, l] = s.split("->").map((e) => e.trim());
if (a && l) {
const e = l.split(",");
a.includes("*") ? t.set(a, e) : u.set(a, e);
}
}
return { stopWords: r, stopPatterns: c, exactSubs: u, patternSubs: t };
}
function h(n) {
return new RegExp(`^${n}$`.replace(/\*/g, "(.+)"));
}
function y(n, o) {
const r = [], c = [];
for (const e of n.split(","))
e.startsWith("-") ? c.push(e.slice(1)) : r.push(e);
const u = r.map((e) => h(e)), t = c.map((e) => h(e)), s = u[0];
let a = 1;
const l = o.replace(/\*/g, () => `$${a++}`);
return (e) => {
const p = u.every((i) => i.test(e)) && !t.some((i) => i.test(e));
return {
match: p,
output: p ? e.replace(s, l) : e
};
};
}
function m(n, o = b) {
const r = Math.floor(o() * n.length);
return n[r];
}
function x(n, {
getRandom: o = b,
getProbability: r = d()
} = {}) {
const c = [], u = [];
for (const t of n.stopPatterns) {
const s = h(t), a = (l) => s.test(l);
c.push(a);
}
for (const [t, s] of n.patternSubs.entries()) {
const a = s.map((e) => y(t, e)), l = (e) => {
const p = m(a, o)(e);
if (p.match) {
const i = r(e, { type: "pattern", pattern: t });
return o() < i ? p : {
match: !1,
output: e
};
} else
return p;
};
u.push(l);
}
return (t) => {
if (!t)
return t;
const s = t.toLowerCase(), a = t === t.toUpperCase(), l = t === s, e = t[0] === t[0].toUpperCase() && !a && !l, p = (i) => i === s ? t : a ? i.toUpperCase() : e ? i[0].toUpperCase() + i.slice(1) : i;
if (n.stopWords.has(s))
return t;
for (const i of c)
if (i(s))
return t;
if (n.exactSubs.has(s)) {
const i = n.exactSubs.get(s), f = r(t, {
type: "exact",
pattern: s
});
return o() < f ? p(m(i, o)) : t;
}
if (t.length <= 2)
return t;
for (const i of u) {
const f = i(s);
if (f.match)
return p(f.output);
}
return t;
};
}
async function P() {
return (await import("./snacklish-CkByQU7N.js")).default;
}
async function F() {
const n = await P();
return S(n);
}
function C(n) {
return n.split(/(n['’]t|\W+)/);
}
async function T({
getRandom: n,
getProbability: o
} = {}) {
const r = await F(), c = x(r, { getRandom: n, getProbability: o });
return (u) => C(u).map(c).join("");
}
export {
T as createTranslator,
b as defaultRandom,
k as getExtremeProbabilityFunction,
R as getKindaProbabilityFunction,
d as getNormalProbabilityFunction,
W as getZeroProbabilityFunction,
P as loadRuleString,
F as loadRules,
S as parseRules,
m as randomChoice,
x as rulesToFunction,
C as tokenize
};