summaryrefslogtreecommitdiff
path: root/packages/prosody-ui/src/thai
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-11-23 01:12:53 +0700
committerpolwex <polwex@sortug.com>2025-11-23 01:12:53 +0700
commitcb1b56f5a0eddbf77446f415f2beda57c8305f85 (patch)
treed333ca5c143063af8ee1b2f9e2d1d25f8ef2007c /packages/prosody-ui/src/thai
wut
Diffstat (limited to 'packages/prosody-ui/src/thai')
-rw-r--r--packages/prosody-ui/src/thai/ThaiText.tsx49
-rw-r--r--packages/prosody-ui/src/thai/logic/thainlp.ts90
2 files changed, 139 insertions, 0 deletions
diff --git a/packages/prosody-ui/src/thai/ThaiText.tsx b/packages/prosody-ui/src/thai/ThaiText.tsx
new file mode 100644
index 0000000..fc1e1e6
--- /dev/null
+++ b/packages/prosody-ui/src/thai/ThaiText.tsx
@@ -0,0 +1,49 @@
+import React, { useCallback, useEffect, useState } from "react";
+import "../assets/fonts/Thai/style.css";
+import { segmentateThai } from "./logic/thainlp";
+import type { AnalyzeRes } from "../logic/types";
+import { ColoredText } from "../components/Sentence";
+import Word from "../components/Word";
+
+export default function ThaiText({
+ text,
+ openWord,
+}: {
+ text: string;
+ openWord: (s: string) => void;
+}) {
+ useEffect(() => {
+ pythonseg();
+ }, [text]);
+
+ const [data, setData] = useState<Record<string, AnalyzeRes>>({});
+ const [modal, setModal] = useState<any>();
+ const pythonseg = useCallback(async () => {
+ const s2 = await segmentateThai(text.trim());
+ if ("ok" in s2) {
+ const ob = s2.ok.reduce((acc, item) => {
+ acc[item.word] = item;
+ return acc;
+ }, {} as any);
+ setData(ob);
+ console.log(s2, "s2");
+ } else console.error(s2.error);
+ }, [text]);
+
+ // function openWord(e: React.MouseEvent<any>) {
+ // const s = e.currentTarget.innerText;
+ // const d = data[s];
+ // setModal(d);
+ // // setModal(<WordModal data={d} lang={lang} />);
+ // }
+ return (
+ <div className="thaitext">
+ <ColoredText lang="tha" frags={Object.keys(data)} fn={openWord} />
+ {modal && <Word data={modal} lang={"tha"} />}
+ </div>
+ );
+}
+
+function ThaiWord() {
+ return <div />;
+}
diff --git a/packages/prosody-ui/src/thai/logic/thainlp.ts b/packages/prosody-ui/src/thai/logic/thainlp.ts
new file mode 100644
index 0000000..031bf4c
--- /dev/null
+++ b/packages/prosody-ui/src/thai/logic/thainlp.ts
@@ -0,0 +1,90 @@
+import type { AsyncRes } from "sortug";
+import type { AnalyzeRes } from "../../logic/types";
+
+const ENDPOINT = "http://192.168.1.110:8001";
+async function call(path: string, body: any) {
+ try {
+ const opts = {
+ method: "POST",
+ headers: { "Content-type": "application/json" },
+ body: JSON.stringify(body),
+ };
+ const r1 = await fetch(ENDPOINT + path, opts);
+ // const r2 = await fetch(`http://192.168.1.110:8000/analyze`, opts);
+ const jj = await r1.json();
+ return { ok: jj };
+ } catch (e) {
+ return { error: `${e}` };
+ }
+}
+export async function analyzeTHWord(word: string): AsyncRes<AnalyzeRes> {
+ return await call("/analyze", { word });
+}
+export async function segmentateThai(sentence: string): AsyncRes<AnalyzeRes[]> {
+ return await call("/segmentate", { word: sentence });
+}
+
+export const POSMAP: Record<string, string> = {
+ ADJ: "Adjective",
+ ADP: "Adposition",
+ ADV: "Adverb",
+ AUX: "Auxiliary",
+ CCONJ: "Coordinating conjunction",
+ DET: "Determiner",
+ INTJ: "Interjection",
+ NOUN: "Noun",
+ NUM: "Numeral",
+ PART: "Particle",
+ PRON: "Pronoun",
+ PROPN: "Proper noun",
+ PUNCT: "Punctuation",
+ SCONJ: "Subordinating conjunction",
+ VERB: "Verb",
+ NPRP: "Proper noun",
+ NCNM: "Cardinal number",
+ NONM: "Ordinal number",
+ NLBL: "Label noun",
+ NCMN: "Common noun",
+ NTTL: "Title noun",
+ PPRS: "Personal pronoun",
+ PDMN: "Demonstrative pronoun",
+ PNTR: "Interrogative pronoun",
+ PREL: "Relative pronoun",
+ VACT: "Active verb",
+ VSTA: "Stative verb",
+ VATT: "Attributive verb",
+ XVBM: "Pre-verb auxiliary, before negator “ไม่”",
+ XVAM: "Pre-verb auxiliary, after negator “ไม่”",
+ XVMM: "Pre-verb, before or after negator “ไม่”",
+ XVBB: "Pre-verb auxiliary, in imperative mood",
+ XVAE: "Post-verb auxiliary",
+ DDAN: "classifier in between",
+ DDAC: "in between",
+ DDBQ: "classifier or preceding quantitative expression",
+ DDAQ: "following quantitative expression",
+ DIAC: "classifier in between",
+ DIBQ: "classifier or preceding quantitative expression",
+ DIAQ: "following quantitative expression",
+ DCNM: "Determiner, cardinal number expression",
+ DONM: "Determiner, ordinal number expression",
+ ADVN: "Adverb with normal form",
+ ADVI: "Adverb with iterative form",
+ ADVP: "Adverb with prefixed form",
+ ADVS: "Sentential adverb",
+ CNIT: "Unit classifier",
+ CLTV: "Collective classifier",
+ CMTR: "Measurement classifier",
+ CFQC: "Frequency classifier",
+ CVBL: "Verbal classifier",
+ JCRG: "Coordinating conjunction",
+ JCMP: "Comparative conjunction",
+ JSBR: "Subordinating conjunction",
+ RPRE: "Preposition",
+ INT: "Interjection",
+ FIXN: "Nominal prefix",
+ FIXV: "Adverbial prefix",
+ EAFF: "Ending for affirmative sentence",
+ EITT: "Ending for interrogative sentence",
+ NEG: "Negator",
+ PUNC: "Punctuation",
+};