Przeglądaj źródła

改动拼音错误的bug

chengjie 3 tygodni temu
rodzic
commit
31f053a635
3 zmienionych plików z 99 dodań i 6 usunięć
  1. 2 1
      .vscode/settings.json
  2. 61 4
      src/api/miaoguo/miaoguoController.js
  3. 36 1
      src/model/hanzi.js

+ 2 - 1
.vscode/settings.json

@@ -53,7 +53,8 @@
53 53
         "statusBar.debuggingBackground": "#1857a4",
54 54
         "statusBar.debuggingForeground": "#e7e7e7",
55 55
         "activityBar.activeBorder": "#e7e7e7",
56
-        "activityBarTop.activeBorder": "#e7e7e7"
56
+        "activityBarTop.activeBorder": "#e7e7e7",
57
+        "statusBar.inactiveBackground": "#1857a4"
57 58
     },
58 59
     "peacock.color": "#1857a4"
59 60
 }

+ 61 - 4
src/api/miaoguo/miaoguoController.js

@@ -5266,10 +5266,13 @@ export async function GetMiaoguoTestExamine(ctx) {
5266 5266
                         result = await hanzi.GetHanziUnitWords(param);
5267 5267
                     }
5268 5268
 
5269
+                    //用 MiaoguoLiteracy 中的标准拼音纠正多音字与轻声(如 勒紧 的“勒”读 lēi,石榴 的“榴”读 liu)
5270
+                    await fixWordsPinyinByLiteracy(result);
5271
+
5269 5272
                     for (const item of result) {
5270 5273
                         if (item.Pinyin) {
5271 5274
                             item.Pinyin = item.Pinyin.split(",")[0];
5272
-                            item.CombineWords2 = stringUtils.ReplaceAllString(item.CombineWords, item.Name, item.Pinyin);
5275
+                            item.CombineWords2 = stringUtils.ReplaceAllString(item.CombineWords, item.Name, item.PinyinInWord || item.Pinyin);
5273 5276
                             if (item.CombineWords.length == 2 && item.CombineWords[0] == item.CombineWords[1]) {
5274 5277
                                 switch (item.CombineWords) {
5275 5278
                                     case "妈妈":
@@ -6111,6 +6114,59 @@ function getEF(EF, Q) {
6111 6114
     return result;
6112 6115
 }
6113 6116
 
6117
+//用 MiaoguoLiteracy 中的标准拼音,纠正汉字在词语里的读音(多音字、轻声)
6118
+//如:勒紧 的“勒”应为 lēi,石榴 的“榴”应为 liu
6119
+async function fixWordsPinyinByLiteracy(words) {
6120
+    if (!words || words.length === 0)
6121
+        return;
6122
+
6123
+    const wordList = [];
6124
+    for (const item of words) {
6125
+        if (!item.CombineWords)
6126
+            continue;
6127
+        for (const word of String(item.CombineWords).split(",")) {
6128
+            if (word && wordList.indexOf(word) < 0)
6129
+                wordList.push(word);
6130
+        }
6131
+    }
6132
+    if (wordList.length === 0)
6133
+        return;
6134
+
6135
+    const pinyinMap = await hanzi.GetMiaoguoLiteracyPinyin({ Words: wordList.join("','") });
6136
+    for (const item of words) {
6137
+        if (!item.CombineWords || !item.Pinyin || !item.Name)
6138
+            continue;
6139
+        for (const word of String(item.CombineWords).split(",")) {
6140
+            const arrPinyin = pinyinMap[word];
6141
+            if (!arrPinyin || arrPinyin.length !== word.length)
6142
+                continue;
6143
+            //完全没有声调的脏数据不采信(如 毛泽东 被记为 “mao ze dong”)
6144
+            if (!/[āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ]/.test(arrPinyin.join("")))
6145
+                continue;
6146
+            const index = word.indexOf(item.Name);
6147
+            if (index < 0 || word.lastIndexOf(item.Name) !== index)
6148
+                continue;
6149
+            const pinyinInWord = arrPinyin[index];
6150
+            if (!pinyinInWord || pinyinInWord == item.Pinyin)
6151
+                continue;
6152
+            //轻声只会出现在词尾,非词尾却没有声调的数据不采信(如 吾国 被记为 “wu guó”)
6153
+            if (index !== word.length - 1 && !/[āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ]/.test(pinyinInWord))
6154
+                continue;
6155
+            item.PinyinInWord = pinyinInWord;
6156
+            //多音字:词语中的读音本来就是该字的读音之一,则字本身的读音也采用词语中的读音
6157
+            const arrReading = String(item.Pinyin).split(",");
6158
+            if (arrReading.length > 1 && arrReading.indexOf(pinyinInWord) > 0)
6159
+                item.Pinyin = pinyinInWord;
6160
+            break;
6161
+        }
6162
+    }
6163
+}
6164
+
6165
+function getPinyinNormalText(pinyin) {
6166
+    const result = getPinyinNormal(pinyin);
6167
+    return result ? result : pinyin;
6168
+}
6169
+
6114 6170
 function getWordJSON(param, words) {
6115 6171
     const taskList = [];
6116 6172
     if (words[0].Pinyin) {
@@ -6125,9 +6181,10 @@ function getWordJSON(param, words) {
6125 6181
                 ],
6126 6182
             };
6127 6183
             taskInfo.Word = word.Name;
6128
-            const pinyinNormal = "(" + getPinyinNormal(word.Pinyin) + ")";
6129
-            const combineWords = stringUtils.ReplaceAllString(word.CombineWords, word.Name, word.Name + pinyinNormal);
6130
-            taskInfo.ReadString = word.Name + pinyinNormal + "," + combineWords + "的" + word.Name + pinyinNormal;
6184
+            const pinyinNormal = "(" + getPinyinNormalText(word.Pinyin) + ")";
6185
+            const pinyinNormalInWord = "(" + getPinyinNormalText(word.PinyinInWord || word.Pinyin) + ")";
6186
+            const combineWords = stringUtils.ReplaceAllString(word.CombineWords, word.Name, word.Name + pinyinNormalInWord);
6187
+            taskInfo.ReadString = word.Name + pinyinNormal + "," + combineWords + "的" + word.Name + pinyinNormalInWord;
6131 6188
 
6132 6189
             if (param.TestType == "read") {
6133 6190
                 taskInfo.Content[0].Content = ["怎么念"];

+ 36 - 1
src/model/hanzi.js

@@ -491,11 +491,46 @@ class Hanzi {
491 491
                         order by Word,SearchType desc;`;
492 492
             return await query(sql);
493 493
         } catch (error) {
494
-            console.error('获取妙果识字词语失败:', error);
494
+            console.error('获取秒过识字词语失败:', error);
495 495
             throw error;
496 496
         }
497 497
     }
498 498
 
499
+    /**
500
+     * 获取秒过识字词语的标准拼音(按字拆分,用于纠正多音字与轻声)
501
+     * @param {Object} obj 查询参数(Words:'词语1','词语2')
502
+     * @returns {Promise<Object>} { 词语: [拼音1,拼音2...] }
503
+     */
504
+    static async GetMiaoguoLiteracyPinyin(obj) {
505
+        try {
506
+            const sql = `select Word,SearchType,JSONString 
507
+                        from MiaoguoLiteracy 
508
+                        where Word in ('${obj.Words}') 
509
+                        and (SearchType is null or (SearchType<>'shici' and SearchType<>'eng')) 
510
+                        order by Word,SearchType desc;`;
511
+            const list = await query(sql);
512
+            const result = {};
513
+            for (const item of list) {
514
+                if (result[item.Word] || !item.JSONString)
515
+                    continue;
516
+                let json = null;
517
+                try {
518
+                    json = JSON.parse(item.JSONString);
519
+                } catch (error) {
520
+                    continue;
521
+                }
522
+                const pinyin = json && json.CHN && json.CHN.PinYin && json.CHN.PinYin[0] ? json.CHN.PinYin[0].pinyin : "";
523
+                if (!pinyin)
524
+                    continue;
525
+                result[item.Word] = String(pinyin).trim().split(/\s+/);
526
+            }
527
+            return result;
528
+        } catch (error) {
529
+            console.error('获取秒过识字拼音失败:', error);
530
+            return {};
531
+        }
532
+    }
533
+
499 534
     /**
500 535
      * 获取没有图片的汉字列表
501 536
      * @returns {Promise<Array>} 汉字列表