import type { AsyncRes, Result } from "sortug"; import { detectLang } from "./iso"; const ENDPOINT = "http://localhost:8102"; export async function run(text: string, langg?: string): AsyncRes { try { const lang = langg ? langg : detectLang(text); const body = JSON.stringify({ string: text, lang }); const opts = { headers: { "Content-type": "application/json", "X-API-KEY": Bun.env.SORTUG_NLP_API_KEY!, }, method: "POST", body, }; const res = await fetch(ENDPOINT + "/spacy", opts); const j = await res.json(); console.log("spacy", j); return { ok: j }; } catch (e) { return { error: `${e}` }; } } export type SpacyResBig = { doc: { text: string; ents: any[]; sents: Array<{ start: number; end: number }>; tokens: Token[]; }; segs: Sentence[]; }; export type SpacyRes = { input: string; segments: Sentence[]; }; export type Sentence = { text: string; start: number; end: number; root: Token; subj: Token; arcs: Arc[]; words: Word[]; }; export type Arc = { start: number; end: number; label: string; // deprel label dir: string; }; export type Token = { id: number; head: number; start: number; end: number; dep: string; lemma: string; morph: string; pos: string; tag: string; text: string; }; export interface Word extends Token { ancestors: number[]; children: []; n_lefts: number; n_rights: number; left_edge: number; right_edge: number; morph_map: Record; } export function isChild(w: Word, topId: number): boolean { return w.id === topId || w.ancestors.includes(topId); }