| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import { enhanceFormsOfWords } from '../api/yjbdc/enhanceFormsOfWords.js';
- // 测试文章
- const testArticle = [
- "She cries when she is sad.",
- "The leaves fall from the trees in autumn.",
- "He went to the store yesterday.",
- "The knives are in the drawer.",
- "I am trying to understand this concept."
- ];
- // 创建测试JSON对象
- const testJsonObj = {
- ArticleEnglish: testArticle
- };
- // 打印测试文章
- console.log("[DEBUG] 文章中的句子:");
- testArticle.forEach((sentence, index) => {
- console.log(`[DEBUG] ${index + 1}: ${sentence}`);
- });
- // 提取文章中的所有单词
- const allWords = testArticle.join(" ")
- .toLowerCase()
- .replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, " ")
- .replace(/\s+/g, " ")
- .trim()
- .split(" ");
- console.log("[DEBUG] 文章中的所有单词:");
- console.log(`[DEBUG] ${allWords.join(" ")}`);
- // 测试checkSpecialWordForms函数
- console.log("测试checkSpecialWordForms函数...");
- // 从enhanceFormsOfWords.js中提取checkSpecialWordForms函数
- // 由于它是私有函数,我们需要重新实现它进行测试
- function checkSpecialWordForms(word, base) {
- // 处理以y结尾的单词变为复数时,y变为ies的情况 (如cry -> cries)
- if (base.endsWith('y') && word === base.slice(0, -1) + 'ies') {
- return true;
- }
-
- // 处理以fe结尾的单词变为复数时,fe变为ves的情况 (如knife -> knives)
- if (base.endsWith('fe') && word === base.slice(0, -2) + 'ves') {
- return true;
- }
-
- // 处理以f结尾的单词变为复数时,f变为ves的情况 (如leaf -> leaves)
- if (base.endsWith('f') && word === base.slice(0, -1) + 'ves') {
- return true;
- }
-
- // 处理不规则动词过去式 (如go -> went)
- const irregularVerbs = {
- 'go': ['went', 'gone', 'going', 'goes'],
- 'try': ['tried', 'trying', 'tries']
- };
-
- // 检查是否是不规则动词的变形
- if (irregularVerbs[base] && irregularVerbs[base].includes(word)) {
- return true;
- }
-
- return false;
- }
- // 测试用例
- const testCases = [
- { base: "cry", form: "cries", expected: true },
- { base: "leaf", form: "leaves", expected: true },
- { base: "knife", form: "knives", expected: true },
- { base: "go", form: "went", expected: true },
- { base: "try", form: "trying", expected: true },
- { base: "cat", form: "cats", expected: false } // 这个应该返回false,因为我们的简化版本不处理常规的复数形式
- ];
- // 运行测试
- testCases.forEach(test => {
- console.log("----------------------------------------");
- console.log(`测试: ${test.base} -> ${test.form}`);
- const result = checkSpecialWordForms(test.form, test.base);
- console.log(`结果: ${result}`);
- console.log(`预期: ${test.expected}`);
- if (result === test.expected) {
- console.log("✅ 通过");
- } else {
- console.log("❌ 失败");
- }
- });
- // 测试enhanceFormsOfWords函数
- console.log("----------------------------------------");
- console.log("开始测试enhanceFormsOfWords函数...");
- // 测试用例
- const enhanceTestCases = [
- { input: "cry", expected: ["cry", "cries"] },
- { input: "leaf", expected: ["leaf", "leaves"] },
- { input: "go", expected: ["go", "went"] },
- { input: "knife", expected: ["knife", "knives"] },
- { input: "try", expected: ["try", "trying"] }
- ];
- // 运行测试
- enhanceTestCases.forEach(test => {
- console.log("----------------------------------------");
- console.log(`[DEBUG] 处理测试输入: "${test.input}"`);
-
- const result = enhanceFormsOfWords({ ArticleEnglish: testArticle }, test.input);
- const formsFound = result.FormsOfWords || [];
-
- console.log(`测试输入: "${test.input}"`);
- console.log(`识别到的单词形式: [${formsFound.map(w => `"${w}"`).join("")}]`);
-
- // 检查是否所有预期的单词变形都被识别
- const allExpectedFound = test.expected.every(word => formsFound.includes(word));
-
- if (allExpectedFound) {
- console.log("✅ 测试通过: 所有预期的单词变形都被识别");
- } else {
- console.log("❌ 测试失败: 有预期的单词变形未被识别");
- const missing = test.expected.filter(word => !formsFound.includes(word));
- console.log(` 缺少: ${missing.join(", ")}`);
- }
- });
- // 测试多个单词输入
- console.log("----------------------------------------");
- const multiWordInput = "cryleafgo";
- const multiWordResult = enhanceFormsOfWords({ ArticleEnglish: testArticle }, multiWordInput);
- const multiWordForms = multiWordResult.FormsOfWords || [];
- console.log(`测试多个单词输入: "${multiWordInput}"`);
- console.log(`识别到的单词形式: [${multiWordForms.map(w => `"${w}"`).join("")}]`);
- // 预期的单词变形
- const expectedMultiWordForms = ["cry", "leaf", "go", "cries", "leaves", "went"];
- const allMultiWordExpectedFound = expectedMultiWordForms.every(word => multiWordForms.includes(word));
- if (allMultiWordExpectedFound) {
- console.log("✅ 多单词测试通过: 所有预期的单词变形都被识别");
- } else {
- console.log("❌ 多单词测试失败: 有预期的单词变形未被识别");
- const missing = expectedMultiWordForms.filter(word => !multiWordForms.includes(word));
- console.log(` 缺少: ${missing.join(", ")}`);
- }
|