summaryrefslogtreecommitdiff
path: root/src/lib/db/enseed.ts
blob: 39dec44fe4cc5b4f7a909426eb4d43141058c54e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Database from "bun:sqlite";
import {
  analyzeTHWord,
  deconstructSyllable,
  segmentateThai,
  type SorSyl,
  type ThaiNLPRes,
  sorSyl,
  getThaiFreq,
  SorBSyl,
} from "../calls/nlp";
import pdb from "./prosodydb";
import { cleanIpa } from "../utils";
import { handleFile } from "./utils";
import { Tone } from "../types/phonetics";
import { AsyncRes } from "../types";

const errors: string[] = [];
async function readDump(lang: string) {
  await pdb.init();
  pdb.addLanguage("th", "thai");
  let count = 0;
  const langdb = new Database(
    `/home/y/code/prosody/resources/wiktionary/${lang}.db`,
  );
  let langrows: any = langdb.query("SELECT data FROM langs");
  // langrows = langrows.slice(10);
  const freqMap = await getFrequency();
  for (const langrow of langrows) {
    count++;
    console.log(count);
    // if (count <= 10000) continue;
    if (count > 300) break;
    const j = JSON.parse(langrow.data);
    const word = j.word.trim();
    if (!word) continue;
    const split = word.split(" ");
    const res =
      split.length > 1
        ? await handleIdiom(lang, word)
        : await handleWord(lang, word, j, freqMap);
    if ("error" in res) {
      console.error(res.error);
      break;
    }
  }
  console.dir(errors);
}

async function handleWord(
  lang: string,
  word: string,
  j: any,
  freqMap: Map<string, number>,
): AsyncRes<string> {
  // TODO add categories but add a tag to see what classifying scheme we're using
  //
  const sounds = j.sounds || [];
  const hasIpa = sounds.find((s: any) => "ipa" in s);
  const hwikiRhyme = sounds.find((s: any) => "rhymes" in s);
  const wikiRhyme = hwikiRhyme ? hwikiRhyme.rhymes : null;
  if (!hasIpa) {
    // console.error("no ipa!!", word);
    // console.dir(j, { depth: null });
    return { error: "meh no ipa" };
  }
  const freq = freqMap.get(word) || null;
  // const wordId = pdb.addWord(word, lang, freq, null);
  // WIPE
  const wordId = 0;
  // console.log(analyzed);
  for (let snd of sounds)
    if ("ipa" in snd) {
      const res = await handleIpa(wordId, word, lang, j, snd, wikiRhyme);
      if ("error" in res) return res;
    }
  return { ok: "" };
}
async function handleIpa(
  wordId: number | bigint,
  word: string,
  lang: string,
  j: any,
  snd: any,
  wikiRhyme: string | null,
) {
  const tags = JSON.stringify(snd.tags) || null;
  const ipa = snd.ipa;
  const syls = await sorSyl(word, lang, ipa);
  // console.log(syls, "sorsyl");

  console.log(word);
  console.log(ipa);
  pdb.addPronunciation(wordId, ipa, syls.syls.length, tags, null);
  // set word rhyme
  const wordRhyme = syls.syls.reduce((acc: string, itemm: SorBSyl) => {
    const item = itemm.ipa;
    if (!item.stressed && !acc) return acc;
    if (item.stressed && !acc) return `${acc}${item.rhyme}`;
    else return `${acc}${item.all}`;
  }, "");
  if (wordRhyme) pdb.addWordRhyme(wordId, wordRhyme, j.lang_code, wikiRhyme);

  for (let i = 0; i < syls.syls.length; i++) {
    const syl = syls.syls[i]!;
    const res = await handleSyllable(syl, wordId, i);
    if ("error" in res) return res;
  }
  return { ok: "" };
}
async function handleSyllable(
  syl: SorBSyl,
  wordId: number | bigint,
  idx: number,
): AsyncRes<string> {
  try {
    pdb.addSyllable(
      wordId,
      idx + 1,
      syl.ipa.stressed,
      "th",
      syl.ipa.all,
      syl.ipa.long,
      syl.spelling.all,
      { spelling: syl.spelling.onset, ipa: syl.ipa.onset },
      { spelling: syl.spelling.medial, ipa: syl.ipa.medial },
      { spelling: syl.spelling.nucleus, ipa: syl.ipa.nucleus },
      { spelling: syl.spelling.coda, ipa: syl.ipa.coda },
      { spelling: syl.spelling.rhyme, ipa: syl.ipa.rhyme },
      { letters: "", numbers: 0, name: "" },
      null,
    );
    return { ok: "" };
  } catch (e) {
    // console.log("well fuck", syl);
    // console.error(e);
    return { error: `${e}` };
  }
}
async function handleIdiom(lang: string, idiom: string): AsyncRes<string> {
  try {
    pdb.addIdiom(idiom, lang);
    // TODO later set idiom_words once all words are populated
    // console.log();
    return { ok: "" };
  } catch (e) {
    return { error: `${e}` };
  }
}
// ช้า ๆ
// งก ๆ
// หงก ๆ

async function getFrequency() {
  const freqMap = new Map<number, string>();
  await handleFile(
    "/home/y/code/prosody/hanchu/datasets/unigram_freq.csv",
    (line, idx) => {
      const [spelling, frequency] = line.split(",");
      freqMap.set(Number(frequency!), spelling!);
    },
  );
  const orderedMap = new Map<string, number>();
  const keys = Array.from(freqMap.keys()).sort();
  for (let i = 0; i < keys.length; i++) {
    const val = freqMap.get(keys[i]!)!;
    orderedMap.set(val, i + 1);
  }
  return orderedMap;
}

readDump("en");