fix(dictionary): 修复语义映射和错误日志

- 修复语义映射:强制将输入转换为查询语言的对应词
- 移除拼写自动纠正,避免错误纠正(如 franch→franchise)
- 修复 winston 日志 Error 对象序列化问题
This commit is contained in:
2026-03-09 18:14:14 +08:00
parent 6c811a77db
commit 719aef5a7f
5 changed files with 17 additions and 17 deletions

View File

@@ -41,7 +41,7 @@ export async function executeDictionaryLookup(
return finalResult;
} catch (error) {
log.error("Dictionary lookup failed", { error });
log.error("Dictionary lookup failed", { error: error instanceof Error ? error.message : String(error) });
const errorMessage = error instanceof Error ? error.message : "未知错误";
throw new LookUpError(errorMessage);
}

View File

@@ -17,23 +17,23 @@ export async function preprocessInput(
任务:
1. 判断输入是否有效(非空、是自然语言)
2. 识别输入语言和类型(单词/短语)
3. 如果输入语言 ≠ 查询语言,判断是否需要语义映射
4. 生成查询语言下的标准形式
2. 识别输入类型(单词/短语)
3. 将输入转换为查询语言的对应词(语义映射
4. 生成标准形式(必须是查询语言)
语义映射规则:
- 只有当输入是"明确、基础、可词典化的语义概念"(如常见动词、名词、形容词)时才映射
- 复杂句子、专业术语、无法确定语义的词汇不映射,直接用原文
标准形式规则:
- 修正拼写错误
- 还原为词典形式(英语:动词原形/名词单数;日语:辞书形;中文:标准简化字)
重要规则:
- standardForm 必须是查询语言的词汇
- 例如:查询语言=维吾尔语,输入="japanese" → standardForm="ياپونىيە"
- 例如:查询语言=中文,输入="japanese" → standardForm="日语"
- 例如:查询语言=English输入="日语" → standardForm="Japanese"
- 如果输入本身就是查询语言,则保持不变
- 只做词典形式还原,不纠正拼写
返回 JSON
{
"isValid": boolean,
"inputType": "word" | "phrase",
"standardForm": "标准形式",
"standardForm": "查询语言对应的标准形式",
"confidence": "high" | "medium" | "low",
"reason": "错误原因,成功时为空字符串"
}
@@ -81,7 +81,7 @@ export async function preprocessInput(
reason: typeof result.reason === "string" ? result.reason : "",
};
} catch (error) {
log.error("Preprocess failed", { error });
log.error("Preprocess failed", { error: error instanceof Error ? error.message : String(error) });
throw error;
}
}

View File

@@ -49,7 +49,7 @@ ${isWord ? `{"entries":[{"ipa":"音标","partOfSpeech":"词性","definition":"
return result;
} catch (error) {
log.error("Entries generation failed", { error });
log.error("Entries generation failed", { error: error instanceof Error ? error.message : String(error) });
throw error;
}
}

View File

@@ -21,7 +21,7 @@ export const actionLookUpDictionary = async (dto: ActionInputLookUpDictionary):
message: e.message
};
}
log.error("Dictionary lookup failed", { error: e });
log.error("Dictionary lookup failed", { error: e instanceof Error ? e.message : String(e) });
return {
success: false,
message: 'Unknown error occured.'

View File

@@ -42,7 +42,7 @@ export const serviceLookUp = async (dto: ServiceInputLookUp) => {
},
response.entries
).catch(error => {
log.error("Failed to save dictionary data", { error });
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
});
return response;
@@ -54,7 +54,7 @@ export const serviceLookUp = async (dto: ServiceInputLookUp) => {
definitionLang: definitionLang,
dictionaryItemId: lastLookUpResult.id
}).catch(error => {
log.error("Failed to save dictionary data", { error });
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
});
return {
standardForm: lastLookUpResult.standardForm,