From 904b34de8f7748b7954d88784369b9cae6fa92fb Mon Sep 17 00:00:00 2001 From: polwex Date: Mon, 2 Jun 2025 23:05:36 +0700 Subject: all me here should merge --- src/lib/calls/nlp.ts | 112 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) (limited to 'src/lib/calls') diff --git a/src/lib/calls/nlp.ts b/src/lib/calls/nlp.ts index 24e7cf3..3cff415 100644 --- a/src/lib/calls/nlp.ts +++ b/src/lib/calls/nlp.ts @@ -1,13 +1,35 @@ import { SyllableRes } from "../types/cards"; -type AnalyzeRes = { +export type ThaiNLPRes = { word: string; + normalized: string; syllables: string[]; + syllablesIpa: string[]; ipa: string; pos: string; }; +export type SorSylRes = { + word: string; + ipa: string; + clean_ipa: string; + syls: SorSyl[]; +}; +export type SorSyl = { + stressed: boolean; + long: boolean; + spelling: string; + ipa: string; + nucleus: string; + onset: string; + medial: string; + coda: string; + rhyme: string; + tone: string; + start_idx: number; + end_idx: number; +}; -export async function thaiData(word: string): Promise { +export async function thaiData(word: string): Promise { const [head, tail] = await Promise.all([ analyzeTHWord(word), segmentateThai(word), @@ -15,7 +37,7 @@ export async function thaiData(word: string): Promise { return [head, ...tail]; } -export async function analyzeTHWord(word: string): Promise { +export async function analyzeTHWord(word: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, @@ -26,7 +48,7 @@ export async function analyzeTHWord(word: string): Promise { const jj = await r1.json(); return jj; } -export async function segmentateThai(sentence: string): Promise { +export async function segmentateThai(sentence: string): Promise { const opts = { method: "POST", headers: { "Content-type": "application/json" }, @@ -37,6 +59,70 @@ export async function segmentateThai(sentence: string): Promise { 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 = { @@ -52,6 +138,24 @@ export async function deconstructSyllable(ipa: string): Promise { 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 = { -- cgit v1.2.3