fix(dictionary): 修复语义映射和错误日志
- 修复语义映射:强制将输入转换为查询语言的对应词 - 移除拼写自动纠正,避免错误纠正(如 franch→franchise) - 修复 winston 日志 Error 对象序列化问题
This commit is contained in:
@@ -41,7 +41,7 @@ export async function executeDictionaryLookup(
|
|||||||
return finalResult;
|
return finalResult;
|
||||||
|
|
||||||
} catch (error) {
|
} 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 : "未知错误";
|
const errorMessage = error instanceof Error ? error.message : "未知错误";
|
||||||
throw new LookUpError(errorMessage);
|
throw new LookUpError(errorMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,23 +17,23 @@ export async function preprocessInput(
|
|||||||
|
|
||||||
任务:
|
任务:
|
||||||
1. 判断输入是否有效(非空、是自然语言)
|
1. 判断输入是否有效(非空、是自然语言)
|
||||||
2. 识别输入语言和类型(单词/短语)
|
2. 识别输入类型(单词/短语)
|
||||||
3. 如果输入语言 ≠ 查询语言,判断是否需要语义映射
|
3. 将输入转换为查询语言的对应词(语义映射)
|
||||||
4. 生成查询语言下的标准形式
|
4. 生成标准形式(必须是查询语言)
|
||||||
|
|
||||||
语义映射规则:
|
重要规则:
|
||||||
- 只有当输入是"明确、基础、可词典化的语义概念"(如常见动词、名词、形容词)时才映射
|
- standardForm 必须是查询语言的词汇
|
||||||
- 复杂句子、专业术语、无法确定语义的词汇不映射,直接用原文
|
- 例如:查询语言=维吾尔语,输入="japanese" → standardForm="ياپونىيە"
|
||||||
|
- 例如:查询语言=中文,输入="japanese" → standardForm="日语"
|
||||||
标准形式规则:
|
- 例如:查询语言=English,输入="日语" → standardForm="Japanese"
|
||||||
- 修正拼写错误
|
- 如果输入本身就是查询语言,则保持不变
|
||||||
- 还原为词典形式(英语:动词原形/名词单数;日语:辞书形;中文:标准简化字)
|
- 只做词典形式还原,不纠正拼写
|
||||||
|
|
||||||
返回 JSON:
|
返回 JSON:
|
||||||
{
|
{
|
||||||
"isValid": boolean,
|
"isValid": boolean,
|
||||||
"inputType": "word" | "phrase",
|
"inputType": "word" | "phrase",
|
||||||
"standardForm": "标准形式",
|
"standardForm": "查询语言对应的标准形式",
|
||||||
"confidence": "high" | "medium" | "low",
|
"confidence": "high" | "medium" | "low",
|
||||||
"reason": "错误原因,成功时为空字符串"
|
"reason": "错误原因,成功时为空字符串"
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ export async function preprocessInput(
|
|||||||
reason: typeof result.reason === "string" ? result.reason : "",
|
reason: typeof result.reason === "string" ? result.reason : "",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Preprocess failed", { error });
|
log.error("Preprocess failed", { error: error instanceof Error ? error.message : String(error) });
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ ${isWord ? `{"entries":[{"ipa":"音标","partOfSpeech":"词性","definition":"
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Entries generation failed", { error });
|
log.error("Entries generation failed", { error: error instanceof Error ? error.message : String(error) });
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export const actionLookUpDictionary = async (dto: ActionInputLookUpDictionary):
|
|||||||
message: e.message
|
message: e.message
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
log.error("Dictionary lookup failed", { error: e });
|
log.error("Dictionary lookup failed", { error: e instanceof Error ? e.message : String(e) });
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Unknown error occured.'
|
message: 'Unknown error occured.'
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export const serviceLookUp = async (dto: ServiceInputLookUp) => {
|
|||||||
},
|
},
|
||||||
response.entries
|
response.entries
|
||||||
).catch(error => {
|
).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;
|
return response;
|
||||||
@@ -54,7 +54,7 @@ export const serviceLookUp = async (dto: ServiceInputLookUp) => {
|
|||||||
definitionLang: definitionLang,
|
definitionLang: definitionLang,
|
||||||
dictionaryItemId: lastLookUpResult.id
|
dictionaryItemId: lastLookUpResult.id
|
||||||
}).catch(error => {
|
}).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 {
|
return {
|
||||||
standardForm: lastLookUpResult.standardForm,
|
standardForm: lastLookUpResult.standardForm,
|
||||||
|
|||||||
Reference in New Issue
Block a user