summaryrefslogtreecommitdiff
path: root/src/lib/calls/thainlp.ts
blob: 662e984450237fccf228843a6bd745190e060a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { SyllableRes } from "../types/cards";

export type ThaiNLPRes = {
  word: string;
  normalized: string;
  realSyls: string[];
  syllables: string[];
  syllablesIpa: string[];
  ipa: string;
  pos: string;
};

export async function thaiData(word: string): Promise<ThaiNLPRes[]> {
  const [head, tail] = await Promise.all([
    analyzeTHWord(word),
    segmentateThai(word),
  ]);
  return [head, ...tail];
}

export async function analyzeTHWord(word: string): Promise<ThaiNLPRes> {
  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<ThaiNLPRes[]> {
  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<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;
}