diff options
author | polwex <polwex@sortug.com> | 2025-06-02 23:05:36 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-06-02 23:05:36 +0700 |
commit | 904b34de8f7748b7954d88784369b9cae6fa92fb (patch) | |
tree | 53bb5cb3377ae40d8bfa44087a0c712edd6c9d02 /src/lib/calls/nlp.ts | |
parent | a03c92dc82ad527d7da6bbaa3c43000e2e5f0e69 (diff) |
all me here should merge
Diffstat (limited to 'src/lib/calls/nlp.ts')
-rw-r--r-- | src/lib/calls/nlp.ts | 112 |
1 files changed, 108 insertions, 4 deletions
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<AnalyzeRes[]> { +export async function thaiData(word: string): Promise<ThaiNLPRes[]> { const [head, tail] = await Promise.all([ analyzeTHWord(word), segmentateThai(word), @@ -15,7 +37,7 @@ export async function thaiData(word: string): Promise<AnalyzeRes[]> { return [head, ...tail]; } -export async function analyzeTHWord(word: string): Promise<AnalyzeRes> { +export async function analyzeTHWord(word: string): Promise<ThaiNLPRes> { const opts = { method: "POST", headers: { "Content-type": "application/json" }, @@ -26,7 +48,7 @@ export async function analyzeTHWord(word: string): Promise<AnalyzeRes> { const jj = await r1.json(); return jj; } -export async function segmentateThai(sentence: string): Promise<AnalyzeRes[]> { +export async function segmentateThai(sentence: string): Promise<ThaiNLPRes[]> { const opts = { method: "POST", headers: { "Content-type": "application/json" }, @@ -37,6 +59,70 @@ export async function segmentateThai(sentence: string): Promise<AnalyzeRes[]> { const jj = await r2.json(); return jj; } +export async function getThaiFreq(word: string): Promise<number> { + 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<string[]> { + 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<string[]> { + 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<string[]> { + 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<string[]> { + 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<SyllableRes> { const opts = { @@ -52,6 +138,24 @@ export async function deconstructSyllable(ipa: string): Promise<SyllableRes> { const jj = await r2.json(); return jj; } +export async function sorSyl( + word: string, + lang_code: string, + ipa: string, +): Promise<SorSylRes> { + 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 = { |