web-ai-toolkit
Version:
AI powered features on the web made easy
59 lines (58 loc) • 1.69 kB
JavaScript
function b(i, l, f, c) {
if (i.length <= l)
return [i];
const s = [];
let e = 0;
for (; e < i.length; ) {
const t = e + l;
if (t >= i.length) {
const n = i.substring(e).trim();
n.length >= c || s.length === 0 ? s.push(n) : s.length > 0 && (s[s.length - 1] += " " + n);
break;
}
let o = t;
for (let n = t; n > e + Math.min(c, l * 0.5); n--)
if ((i[n] === "." || i[n] === "!" || i[n] === "?") && (n + 1 >= i.length || /\s/.test(i[n + 1]))) {
const a = i.substring(Math.max(0, n - 10), n);
if (!(i[n] === "." && /\b[A-Z][a-z]?$/.test(a.trim()))) {
o = n + 1;
break;
}
}
if (o === t) {
for (let n = t; n > e + Math.min(c, l * 0.5); n--)
if (i[n] === `
` && n + 1 < i.length && i[n + 1] === `
`) {
o = n + 2;
break;
}
}
if (o === t) {
for (let n = t; n > e + Math.min(c, l * 0.5); n--)
if (/\s/.test(i[n])) {
o = n + 1;
break;
}
}
const r = i.substring(e, o).trim();
r.length >= c || s.length === 0 ? s.push(r) : s.length > 0 && (s[s.length - 1] += " " + r), e = Math.max(o - f, e + 1);
}
return s.filter((t) => t.length > 0);
}
async function p(i, l, f, c, s, e, t, o) {
const r = b(i, l, f, c), h = [];
s?.(0, `Processing ${r.length} chunks...`);
for (let a = 0; a < r.length; a++) {
const u = r[a], m = await e(u);
h.push(t(m));
const g = (a + 1) / r.length * 0.9;
s?.(g, `Processed ${a + 1} of ${r.length} chunks`);
}
s?.(0.95, "Combining summaries...");
const n = h.join(" ");
return s?.(1, "Summarization complete!"), o(n);
}
export {
p
};