Skip to content

Commit

Permalink
#24 重构, 准备支持更复杂的重组处理
Browse files Browse the repository at this point in the history
  • Loading branch information
testacount1 committed Mar 7, 2019
1 parent 6851d86 commit ff4f812
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/查词.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export function 取释义(选中文本: string): 模型.字段释义 {
if (短语释义) {
释义 = 短语释义;
} else {
let 首选释义 = 释义处理.选取释义(所有词条, 所有词);
for (let 序号 = 0; 序号 < 所有词.length; 序号++) {
释义 = 释义.replace(所有词[序号], 自定义词典.常用命名[所有词条[序号].] || 首选释义[序号])
}
释义 = 逐词翻译(选中文本, 所有词条, 所有词);
}
} else if (所有词条.length == 1) {
// TODO: 简化词条 (以适应状态栏宽度)
Expand All @@ -75,6 +72,29 @@ export function 取释义(选中文本: string): 模型.字段释义 {
};
}

function 逐词翻译(选中文本, 所有词条, 所有词): string {
let 释义 = 选中文本;
let 首选释义 = 释义处理.选取释义(所有词条, 所有词);
let 各释义 = []
for (let 序号 = 0; 序号 < 所有词.length; 序号++) {
let 下一词 = 所有词[序号];
let 位置 = 释义.indexOf(下一词);

if (位置 == 0) {
各释义.push("");
} else {
各释义.push(释义.substring(0, 位置));
}
各释义.push(自定义词典.常用命名[所有词条[序号].] || 首选释义[序号]);
释义 = 释义.substring(位置 + 下一词.length);
}
if (释义 !== "") {
各释义.push(释义);
}
释义 = 各释义.join("");
return 释义;
}

// 词形部分数据格式描述: https://github.com/skywind3000/ECDICT#%E8%AF%8D%E5%BD%A2%E5%8F%98%E5%8C%96
function 提取词形(原字符串: string): 模型.词形变化[] {
let 变化 = [];
Expand Down
4 changes: 2 additions & 2 deletions test/查词.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ suite("查词测试", () => {
检查释义("gettingStarted", "启动");

// 将...替换为后续内容
// 检查释义("beforeRedesign", "在...之前重新设计");
// 检查释义("afterRedesignMobile", "在...之后重新设计移动的");
检查释义("beforeRedesign", "在...之前重新设计");
检查释义("afterRedesignMobile", "在...之后重新设计移动的");

// 优先查询短语
检查释义("lastName", "姓");
Expand Down

0 comments on commit ff4f812

Please sign in to comment.