test-enhance-words.js 5.2 KB

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