// import db from "../../lib/db"; import { z } from "zod"; import { NLP } from "sortug-ai"; const schema = z.object({ app: z.enum(["stanza", "spacy"]), text: z.string().min(3, "minimum 3 characters"), lang: z .custom((val) => { const check = NLP.ISO.BCP47.parse(val); if (!check.language) return false; const twochars = Object.values(NLP.ISO.iso6393To1); return twochars.includes(check.language); }) .optional(), }); export const POST = async (request: Request): Promise => { const bod = await request.json(); const { app, text, lang } = await schema.parseAsync(bod); try { const res = app === "stanza" ? NLP.Stanza.segmenter(text, lang) : NLP.Spacy.run(text, lang); const r = await res; return Response.json(r, { status: 200 }); } catch (error) { return Response.json({ message: "Failure" }, { status: 500 }); } }; type AnalyzeRes = { word: string; syllables: string[]; ipa: string; pos: string; }; export async function thaiData(word: string): Promise { const [head, tail] = await Promise.all([ analyzeTHWord(word), segmentateThai(word), ]); return [head, ...tail]; } export async function analyzeTHWord(word: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word }), }; const r1 = await fetch("http://localhost:8001" + "/analyze", opts); // const r2 = await fetch(`http://192.168.1.110:8000/analyze`, opts); const jj = await r1.json(); return jj; } export async function segmentateThai(sentence: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word: sentence }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/segmentate`, opts); const jj = await r2.json(); return jj; }