wn-ts-node
Version:
Wordnet interface library - TypeScript port
70 lines (69 loc) • 1.81 kB
JavaScript
async function u(c, i) {
const s = c.relations.filter((e) => e.type === "hypernym"), a = [];
for (const e of s) {
const t = await i.synset(e.target);
t && a.push(t);
}
return a;
}
async function w(c, i, s) {
if (c.id === i.id) return [];
const a = await p(c, s), e = await p(i, s);
let t = null;
for (const o of a)
for (const h of e)
for (let r = 0; r < o.length; r++) {
const n = o[r];
if (!n) continue;
const f = h.findIndex((l) => l.id === n.id);
if (f !== -1) {
const l = o.slice(0, r), y = h.slice(0, f).reverse(), m = [...l, n, ...y];
(t === null || m.length < t.length) && (t = m);
}
}
return t || [];
}
async function d(c, i) {
const s = [{ node: c, depth: 0 }];
let a = 0;
const e = /* @__PURE__ */ new Set();
for (; s.length > 0; ) {
const { node: t, depth: o } = s.pop();
if (e.has(t.id)) continue;
e.add(t.id);
const h = await u(t, i);
if (h.length === 0)
o > a && (a = o);
else
for (const r of h)
s.push({ node: r, depth: o + 1 });
}
return a;
}
async function g(c, i, s) {
const a = await p(c, s), e = await p(i, s), t = new Set(a.flat().map((n) => n.id)), o = e.flat().filter((n) => t.has(n.id));
if (o.length === 0)
return [];
const h = await Promise.all(
o.map(async (n) => ({ synset: n, depth: await d(n, s) }))
), r = Math.max(...h.map((n) => n.depth));
return h.filter((n) => n.depth === r).map((n) => n.synset);
}
async function p(c, i) {
const s = [];
async function a(e, t) {
const o = await u(e, i);
if (o.length === 0)
s.push([...t, e]);
else
for (const h of o)
await a(h, [...t, e]);
}
return await a(c, []), s;
}
export {
u as h,
g as l,
d as m,
w as s
};