chengjie 4 meses atrás
pai
commit
cf9afae6e5

+ 108 - 0
src/test/build.test2.js

@@ -0,0 +1,108 @@
1
+import commonModel from '../model/commonModel.js';
2
+import fs from 'fs';
3
+import { stringUtils } from '../util/stringClass.js';
4
+import aiController from '../api/yjbdc/aiController.js';
5
+
6
+async function runScript(){
7
+    try {
8
+        
9
+        const sql="select * from Words where BookID=110 and Soundmark is null order by ID;"
10
+        let list = await commonModel.RunSql(null,sql);
11
+        let count=list.length;
12
+
13
+        const start=0;
14
+        // 添加延时函数,确保每分钟只发送9次请求(约每6.67秒一次请求)
15
+        const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
16
+        const requestDelay = 300; // 7秒,确保每分钟最多9次请求
17
+        
18
+        for(let i=start;i<count;i++){
19
+            let item=list[i];
20
+
21
+            //获得单词
22
+            const word=item.Word;
23
+
24
+            let content={
25
+                "instruction": "提供单词"+word+"音标",
26
+                "output_format":{
27
+                    "Soundmark":"[音标]",
28
+                }
29
+            };
30
+
31
+            content=JSON.stringify(content);
32
+
33
+            const aiProvider="doubao-1-5-pro-32k-250115";
34
+
35
+            //生成例句
36
+            let result = await aiController.generateArticle(content, aiProvider);
37
+
38
+            result=JSON.parse(result);
39
+            //console.log(result); 
40
+
41
+            let soundmark=result.Soundmark;
42
+            //console.log(soundmark); 
43
+
44
+            soundmark=stringUtils.ReplaceAllString(soundmark,"'","ˈ");
45
+            soundmark=stringUtils.ReplaceAllString(soundmark,"\"","");
46
+            if (soundmark.substr(0,1)!="[")
47
+                soundmark="["+soundmark+"]";
48
+            
49
+            console.log(word +" "+soundmark); 
50
+            let sql2="update Words set Soundmark=? where ID="+item.ID+";";
51
+            await commonModel.RunSql(soundmark,sql2);
52
+            
53
+            console.log( i +"/"+ list.length);
54
+            
55
+            // 在每次请求后添加延时,除非是最后一个请求
56
+            // if (i < count - 1) {
57
+            //     console.log(`等待 ${requestDelay/1000} 秒后继续下一个请求...`);
58
+            //     await delay(requestDelay);
59
+            // }
60
+        }
61
+        
62
+        console.log("完成");
63
+    } catch (error) {
64
+        console.error('Error executing script:', error);
65
+    }
66
+}
67
+
68
+// 处理Promise并添加错误捕获
69
+runScript().catch(error => {
70
+    console.error('Error in runScript:', error);
71
+});
72
+
73
+////批量处理加资料的数据
74
+// async function runScript(){
75
+//     try {
76
+//         const sql="select ID from MiaoguoLiteracy order by ID;"
77
+//         let list = await commonModel.RunSql(null,sql);
78
+//         for(let i=0;i<list.length;i++){
79
+//             if (i % 1000 == 0)
80
+//                 console.log("i:"+i+" ID:"+list[i].ID
81
+//             let item =await commonModel.RunSql(null,"select * from MiaoguoLiteracy where ID="+list[i].ID+";"
82
+//             let json;
83
+//             try {
84
+//                 json = JSON.parse(item[0].JSONString
85
+//             } catch (parseError) {
86
+//                 console.error('JSON Parse Error at ID:', list[i].ID);
87
+//                 console.error('Error details:', parseError);
88
+//                 continue;
89
+//             }
90
+//             if (json && json.CHN && json.CHN.BiShunArr && json.CHN.BiShunArr.length>0){
91
+//                 console.log(i+" "+json.CHN.HanZi);
92
+                
93
+//                 delete json.CHN.BiShunArr;
94
+//                 let obj={};
95
+//                 obj.ID=list[i].ID;
96
+//                 obj.JSONString=JSON.stringify(json);
97
+
98
+//                 await commonModel.RunSql(obj,"update MiaoguoLiteracy set ? where ID="+list[i].ID+";"
99
+//             }
100
+//             //console.log(json);
101
+//             //console.log(item.length);
102
+//         }
103
+//         console.log("完成"
104
+//     } catch (error) {
105
+//         console.error('Error executing script:', error);
106
+//     }
107
+// }
108
+

+ 0 - 46
src/test/data copy.txt

@@ -1,46 +0,0 @@
1
-('同济创意',613,364,106,117,106);
2
-('上海中学',705.5,417.5,137,136,142);
3
-('久隆模范',653.5,381,123,121,130);
4
-('田家炳',640.5,370.5,121,126,122);
5
-('扬波中学',566,320,109,98,109);
6
-('新和中学',586,355,110,115,97);
7
-('风范中学',563.5,327,89,108,104);
8
-('上戏附中',620.5,370,116,119,115);
9
-('安生学校',514.5,276.5,102,114,103);
10
-('上音安师',531,299,76,105,102);
11
-('复旦复兴',680,407,133,130,130);
12
-('师大一附',670.5,397.5,128,131,129);
13
-('上财北郊',664.5,383.5,130,127,139);
14
-('上音北虹',627,371.5,123,121,123);
15
-('继光高级',567,338,87,121,91);
16
-('鲁迅中学',614.5,353.5,119,109,120);
17
-('五十二中',514.5,286.5,95,104,104);
18
-('上师虹口',641,375,129,116,124);
19
-('同济澄衷',594.5,355.5,108,113,109);
20
-('交大附中',703,415.5,142,134,141.5);
21
-('复旦附中',705,417,136,136,138);
22
-('燎原双语',569,333,100,102,105);
23
-('存志高级',659,387,128,119,129);
24
-('创艺高级',551.5,314.5,91,116,101);
25
-('华曜高级',637.5,375.5,122,109,118);
26
-('同洲模范',584.5,347,106,117,100);
27
-('宝山世外',662.5,386,125,125,132);
28
-('远东学校',524.5,306,95,113,92);
29
-('华盛怀少',560.5,330,96,107,98);
30
-('师大二附',704,417.5,144,128,142);
31
-('丰华高级',588.5,339,108,107,119);
32
-('金苹果',563.5,330.5,97,114,96);
33
-('东鼎外语',564.5,327.5,88,117,99);
34
-('尚德实验',623,365,120,116,115);
35
-('交大南洋',582,335.5,98,115,107);
36
-('永昌中学',542.5,317,112,105,97);
37
-('金山世外',625.5,356.5,106,124,126);
38
-('领科双语',634.5,385,120,127,115);
39
-('西外外语',547.5,308.5,99,114,105);
40
-('赫贤学校',606.5,357.5,103,116,110);
41
-('青浦世外',603.5,357.5,110,110,109);
42
-('宋庆龄',554,310,94,107,108);
43
-('协和双语',592.5,351,105,111,103);
44
-('博华高级',538.5,312,113,113,98);
45
-('城桥中学',544.5,314.5,105,119,96);
46
-('民一中学',504,291,83,104,81);

+ 0 - 51
src/test/stringClass.test.js

@@ -1,51 +0,0 @@
1
-'use strict';
2
-import { stringUtils } from '../util/stringClass.js';
3
-
4
-// 测试获取服务器IP
5
-console.log('Server IP:', stringUtils.GetServerIP());
6
-
7
-// 创建模拟的Koa上下文对象
8
-const mockCtx = {
9
-    request: {
10
-        headers: {
11
-            'x-forwarded-for': '192.168.1.1, 10.0.0.1',
12
-            'x-real-ip': '192.168.1.2'
13
-        },
14
-        ip: '192.168.1.3',
15
-        socket: {
16
-            remoteAddress: '192.168.1.4'
17
-        }
18
-    },
19
-    ip: '192.168.1.5'
20
-};
21
-
22
-// 测试不同场景的GetClientIP
23
-console.log('Client IP (with proxy headers):', stringUtils.GetClientIP(mockCtx));
24
-
25
-// 测试没有代理头的情况
26
-const mockCtxNoProxy = {
27
-    request: {
28
-        headers: {},
29
-        ip: '192.168.2.1',
30
-        socket: {
31
-            remoteAddress: '192.168.2.2'
32
-        }
33
-    },
34
-    ip: '192.168.2.3'
35
-};
36
-console.log('Client IP (no proxy headers):', stringUtils.GetClientIP(mockCtxNoProxy));
37
-
38
-// 测试IPv6格式
39
-const mockCtxIPv6 = {
40
-    request: {
41
-        headers: {
42
-            'x-forwarded-for': '::ffff:192.168.3.1'
43
-        },
44
-        ip: '::1',
45
-        socket: {
46
-            remoteAddress: '::ffff:192.168.3.2'
47
-        }
48
-    },
49
-    ip: '::ffff:192.168.3.3'
50
-};
51
-console.log('Client IP (IPv6 format):', stringUtils.GetClientIP(mockCtxIPv6));

+ 0 - 148
src/test/test-enhance-words.js

@@ -1,148 +0,0 @@
1
-import { enhanceFormsOfWords } from '../api/yjbdc/enhanceFormsOfWords.js';
2
-
3
-// 测试文章
4
-const testArticle = [
5
-    "She cries when she is sad.",
6
-    "The leaves fall from the trees in autumn.",
7
-    "He went to the store yesterday.",
8
-    "The knives are in the drawer.",
9
-    "I am trying to understand this concept."
10
-];
11
-
12
-// 创建测试JSON对象
13
-const testJsonObj = {
14
-    ArticleEnglish: testArticle
15
-};
16
-
17
-// 打印测试文章
18
-console.log("[DEBUG] 文章中的句子:");
19
-testArticle.forEach((sentence, index) => {
20
-    console.log(`[DEBUG]   ${index + 1}: ${sentence}`);
21
-});
22
-
23
-// 提取文章中的所有单词
24
-const allWords = testArticle.join(" ")
25
-    .toLowerCase()
26
-    .replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, " ")
27
-    .replace(/\s+/g, " ")
28
-    .trim()
29
-    .split(" ");
30
-
31
-console.log("[DEBUG] 文章中的所有单词:");
32
-console.log(`[DEBUG]   ${allWords.join(" ")}`);
33
-
34
-// 测试checkSpecialWordForms函数
35
-console.log("测试checkSpecialWordForms函数...");
36
-
37
-// 从enhanceFormsOfWords.js中提取checkSpecialWordForms函数
38
-// 由于它是私有函数,我们需要重新实现它进行测试
39
-function checkSpecialWordForms(word, base) {
40
-    // 处理以y结尾的单词变为复数时,y变为ies的情况 (如cry -> cries)
41
-    if (base.endsWith('y') && word === base.slice(0, -1) + 'ies') {
42
-        return true;
43
-    }
44
-    
45
-    // 处理以fe结尾的单词变为复数时,fe变为ves的情况 (如knife -> knives)
46
-    if (base.endsWith('fe') && word === base.slice(0, -2) + 'ves') {
47
-        return true;
48
-    }
49
-    
50
-    // 处理以f结尾的单词变为复数时,f变为ves的情况 (如leaf -> leaves)
51
-    if (base.endsWith('f') && word === base.slice(0, -1) + 'ves') {
52
-        return true;
53
-    }
54
-    
55
-    // 处理不规则动词过去式 (如go -> went)
56
-    const irregularVerbs = {
57
-        'go': ['went', 'gone', 'going', 'goes'],
58
-        'try': ['tried', 'trying', 'tries']
59
-    };
60
-    
61
-    // 检查是否是不规则动词的变形
62
-    if (irregularVerbs[base] && irregularVerbs[base].includes(word)) {
63
-        return true;
64
-    }
65
-    
66
-    return false;
67
-}
68
-
69
-// 测试用例
70
-const testCases = [
71
-    { base: "cry", form: "cries", expected: true },
72
-    { base: "leaf", form: "leaves", expected: true },
73
-    { base: "knife", form: "knives", expected: true },
74
-    { base: "go", form: "went", expected: true },
75
-    { base: "try", form: "trying", expected: true },
76
-    { base: "cat", form: "cats", expected: false } // 这个应该返回false,因为我们的简化版本不处理常规的复数形式
77
-];
78
-
79
-// 运行测试
80
-testCases.forEach(test => {
81
-    console.log("----------------------------------------");
82
-    console.log(`测试: ${test.base} -> ${test.form}`);
83
-    const result = checkSpecialWordForms(test.form, test.base);
84
-    console.log(`结果: ${result}`);
85
-    console.log(`预期: ${test.expected}`);
86
-    if (result === test.expected) {
87
-        console.log("✅ 通过");
88
-    } else {
89
-        console.log("❌ 失败");
90
-    }
91
-});
92
-
93
-// 测试enhanceFormsOfWords函数
94
-console.log("----------------------------------------");
95
-console.log("开始测试enhanceFormsOfWords函数...");
96
-
97
-// 测试用例
98
-const enhanceTestCases = [
99
-    { input: "cry", expected: ["cry", "cries"] },
100
-    { input: "leaf", expected: ["leaf", "leaves"] },
101
-    { input: "go", expected: ["go", "went"] },
102
-    { input: "knife", expected: ["knife", "knives"] },
103
-    { input: "try", expected: ["try", "trying"] }
104
-];
105
-
106
-// 运行测试
107
-enhanceTestCases.forEach(test => {
108
-    console.log("----------------------------------------");
109
-    console.log(`[DEBUG] 处理测试输入: "${test.input}"`);
110
-    
111
-    const result = enhanceFormsOfWords({ ArticleEnglish: testArticle }, test.input);
112
-    const formsFound = result.FormsOfWords || [];
113
-    
114
-    console.log(`测试输入: "${test.input}"`);
115
-    console.log(`识别到的单词形式: [${formsFound.map(w => `"${w}"`).join("")}]`);
116
-    
117
-    // 检查是否所有预期的单词变形都被识别
118
-    const allExpectedFound = test.expected.every(word => formsFound.includes(word));
119
-    
120
-    if (allExpectedFound) {
121
-        console.log("✅ 测试通过: 所有预期的单词变形都被识别");
122
-    } else {
123
-        console.log("❌ 测试失败: 有预期的单词变形未被识别");
124
-        const missing = test.expected.filter(word => !formsFound.includes(word));
125
-        console.log(`  缺少: ${missing.join(", ")}`);
126
-    }
127
-});
128
-
129
-// 测试多个单词输入
130
-console.log("----------------------------------------");
131
-const multiWordInput = "cryleafgo";
132
-const multiWordResult = enhanceFormsOfWords({ ArticleEnglish: testArticle }, multiWordInput);
133
-const multiWordForms = multiWordResult.FormsOfWords || [];
134
-
135
-console.log(`测试多个单词输入: "${multiWordInput}"`);
136
-console.log(`识别到的单词形式: [${multiWordForms.map(w => `"${w}"`).join("")}]`);
137
-
138
-// 预期的单词变形
139
-const expectedMultiWordForms = ["cry", "leaf", "go", "cries", "leaves", "went"];
140
-const allMultiWordExpectedFound = expectedMultiWordForms.every(word => multiWordForms.includes(word));
141
-
142
-if (allMultiWordExpectedFound) {
143
-    console.log("✅ 多单词测试通过: 所有预期的单词变形都被识别");
144
-} else {
145
-    console.log("❌ 多单词测试失败: 有预期的单词变形未被识别");
146
-    const missing = expectedMultiWordForms.filter(word => !multiWordForms.includes(word));
147
-    console.log(`  缺少: ${missing.join(", ")}`);
148
-}