import { SyllableRes } from "../types/cards"; import { randomFromArray } from "../utils"; export type ThaiNLPRes = { word: string; normalized: string; realSyls: string[]; syllables: string[]; syllablesIpa: string[]; ipa: string; pos: string; }; export type SorSylRes = { word: string; ipa: string; clean_ipa: string; syls: SorBSyl[]; }; export type SorBSyl = { ipa: SorSyl; spelling: SorSyl; }; export type SorSyl = { all: string; nucleus: string; onset: string; medial: string; coda: string; rhyme: string; tone: string; long: boolean; stressed: boolean; start_idx: number; end_idx: number; }; 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; } export async function getThaiFreq(word: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/freq`, opts); const jj = await r2.json(); return jj; } export async function getThaiNext(word: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/next`, opts); const jj = await r2.json(); return jj; } export async function getThaiPrev(word: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/prev`, opts); const jj = await r2.json(); return jj; } export async function getThaiNext_bi( word1: string, word2: string, ): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word1, word2 }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/next_bi`, opts); const jj = await r2.json(); return jj; } export async function getThaiPrev_bi( word1: string, word2: string, ): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify({ word1, word2 }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8001" + `/prev_bi`, opts); const jj = await r2.json(); return jj; } export async function deconstructSyllable(ipa: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json", "X-API-KEY": Bun.env.SORTUG_NLP_API_KEY!, }, body: JSON.stringify({ string: ipa }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8102" + `/lingpy`, opts); const jj = await r2.json(); return jj; } export async function sorSyl( word: string, lang_code: string, ipa: string, ): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json", "X-API-KEY": Bun.env.SORTUG_NLP_API_KEY!, }, body: JSON.stringify({ string: word, lang: lang_code, ipa }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8104" + `/syls`, opts); const jj = await r2.json(); return jj; } export async function findLemma(word: string, lang: string) { const opts = { method: "POST", headers: { "Content-type": "application/json", "X-API-KEY": Bun.env.SORTUG_NLP_API_KEY!, }, body: JSON.stringify({ string: word, lang }), }; // const r1 = await fetch(`http://localhost:8000/segmentate`, opts); const r2 = await fetch("http://localhost:8102" + `/spacy`, opts); const jj = await r2.json(); return jj; }