From cedd47f4beb0e79038b8013bae33da5fe77b8248 Mon Sep 17 00:00:00 2001 From: Hocky Yudhiono Date: Thu, 25 Jul 2024 21:53:57 +0800 Subject: [PATCH] Fix canto handler --- main/handler/chinese.ts | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/main/handler/chinese.ts b/main/handler/chinese.ts index dc8479c..11202c4 100644 --- a/main/handler/chinese.ts +++ b/main/handler/chinese.ts @@ -4,7 +4,7 @@ import path from "path"; import fs from "fs"; import {pinyin} from "pinyin-pro"; import ToJyutping from "to-jyutping"; -import { loadDict, cut } from '@node-rs/jieba' +import {loadDict, cut} from '@node-rs/jieba' interface JyutpingResult { @@ -107,31 +107,33 @@ class Chinese { } - static async getJyutpingForSentence(sentence: string): Promise { - const segments = cut(sentence); - const result: JyutpingResult[] = []; + static getJyutpingForSentence(sentence: string): Promise { + return Promise.resolve().then(() => { + const segments = cut(sentence); + const result: JyutpingResult[] = []; - for (const segment of segments) { - const jyutpingList = ToJyutping.getJyutpingList(segment); - const jyutping = jyutpingList.map(([, jp]) => jp).join(' '); - const separation = jyutpingList.map(([char, jp]) => ({ - main: char, - jyutping: jp - })); + for (const segment of segments) { + const jyutpingList = ToJyutping.getJyutpingList(segment); + const jyutping = jyutpingList.map(([, jp]) => jp).join(' '); + const separation = jyutpingList.map(([char, jp]) => ({ + main: char, + jyutping: jp + })); - result.push({ - origin: segment, - jyutping: jyutping, - separation: separation - }); - } + result.push({ + origin: segment, + jyutping: jyutping, + separation: separation + }); + } - return result; + return result; + }); } static registerCantoJieba() { ipcMain.handle('tokenizeUsingCantoneseJieba', async (event, sentence) => { - const res = this.getJyutpingForSentence(sentence); + const res = await this.getJyutpingForSentence(sentence); return res; });