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
  | 
import { readFileSync } from "fs";
import { request } from "undici";
const phonemize = (grapheme: string) =>
  request(
    `http://localhost:5000?grapheme=${encodeURIComponent(grapheme)}`
  ).then((x) => x.body.text());
let data: {
  word: string;
  phoneme: string;
  partials: string[];
}[] = JSON.parse(readFileSync("./data.json").toString("utf8"));
const cutWord = (sentence: string) => {
  let lastWord = sentence.split(" ").slice(-1)[0].replace(/(\?)/g, "");
  return phonemize(lastWord);
};
export const match = async (sentence: string) => {
  let scores: { complete: string; score: number }[] = [];
  let sentenceWord = await cutWord(sentence);
  console.debug("handling word phoneme = ", sentenceWord);
  for (const { phoneme, word, partials } of data) {
    let maxIter = Math.min(sentenceWord.length, phoneme.length);
    let overlap = 0;
    for (let i = maxIter - 1; i !== 0; i--) {
      let part1 = sentenceWord[sentenceWord.length - i - 1];
      let part2 = phoneme[i];
      if (part1 !== part2) {
        console.log("\t\tnot eq", part1, part2);
        break;
      }
      overlap++;
    }
    console.debug("\ttesting with word = ", overlap, word, phoneme);
    /*
    for (let i = 1; i < phoneme.length; i++) {
      // add n last characters from the phoneme
      let add = phoneme.slice(phoneme.length - i, phoneme.length);
      console.debug(
        "\t\ttesting match with = ",
        add,
        " add = ",
        sentenceWord + add
      );
      // we matched a phoneme
      if (phoneme == sentenceWord + add) {
        let score = 1 / (i / phoneme.length);
        // next, we need to find the completion of the word
        // this is relatively easy since we only need to 
        let phonemeIndex = partials.indexOf(add);
        if (phonemeIndex == -1) {
          // cannot find the comlpetion count.
          // default to index
          console.log("couldn't find corresponding cut", add);
          phonemeIndex = word.length + 1;
        }
        let complete = word.slice(word.length - phonemeIndex - 1, word.length);
        console.log("\t\tmatched with score = ", score, " complete = ", complete);
        // need to change to the cut-ed version.
        scores.push({ score, complete: `${complete} (${word})` });
      }
    }*/
  }
  return null;
  let resp = scores.sort((a, b) => b.score - a.score);
  return resp[Math.floor(Math.random() * resp.length)]?.complete;
};
match("quoi");
  |