Skip to content

Commit

Permalink
fix: 继续优化答题问题
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 4, 2022
1 parent 4e43a25 commit 2dfd14d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
14 changes: 7 additions & 7 deletions packages/core/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ export function domSearchAll<E extends RawElements>(
*
*/
export function answerSimilar(answers: string[], options: string[]): Rating[] {
/**
* 删除题目选项中开头的冗余字符串
*/
const removeRedundant = (str: string) => {
return str.replace(/[A-Z]{1}[^A-Za-z0-9\u4e00-\u9fa5]+([A-Za-z0-9\u4e00-\u9fa5]+)/, "$1");
};
answers = answers.map(removeRedundant);
options = options.map(removeRedundant);

Expand All @@ -174,9 +168,15 @@ export function answerSimilar(answers: string[], options: string[]): Rating[] {
}

/**
* 创建日志面板
* 删除题目选项中开头的冗余字符串
*/
export function removeRedundant(str: string) {
return str.trim().replace(/[A-Z]{1}[^A-Za-z0-9\u4e00-\u9fa5]+([A-Za-z0-9\u4e00-\u9fa5]+)/, "$1");
}

/**
* 创建日志面板
*/
export function createTerminalPanel(): ScriptPanelChild {
return {
name: "日志",
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/core/worker/question.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { answerSimilar, clearString } from "../utils";
import { answerSimilar, clearString, removeRedundant } from "../utils";
import { QuestionResolver, WorkContext } from "./interface";

/** 默认答案题目处理器 */
Expand Down Expand Up @@ -50,23 +50,23 @@ export function defaultQuestionResolve<E>(

let count = 0;
for (const answers of results.map((res) => res.answers.map((ans) => ans.answer))) {
const ratings = answerSimilar(
answers,
options.map((el) => el.innerText)
).sort((a, b) => b.rating - a.rating);

targetAnswers[count] = [];
targetOptions[count] = [];

// 判断选项是否完全存在于答案里面
options.forEach((el, i) => {
if (answers.some((answer) => answer.includes(el.innerText))) {
if (answers.some((answer) => answer.includes(removeRedundant(el.innerText)))) {
targetAnswers[count][i] = el.innerText;
targetOptions[count][i] = el;
}
});

if (targetAnswers[count].length === 0) {
const ratings = answerSimilar(
answers,
options.map((el) => el.innerText)
).sort((a, b) => b.rating - a.rating);

// 匹配相似率
if (ratings.some((rating) => rating.rating > 0.6)) {
options.forEach((el, i) => {
Expand Down Expand Up @@ -100,7 +100,10 @@ export function defaultQuestionResolve<E>(
targetOptions[index] = targetOptions[index].filter((ans) => ans !== undefined);

targetOptions[index].forEach((_, i) => {
handler("multiple", targetAnswers[index][i], targetOptions[index][i], ctx);
/** 延长点击时间,避免一次点击全部 */
setTimeout(() => {
handler("multiple", targetAnswers[index][i], targetOptions[index][i], ctx);
}, 200 * i);
});

return { finish: true, targetOptions, targetAnswers };
Expand Down

0 comments on commit 2dfd14d

Please sign in to comment.