Skip to content

Commit

Permalink
#23 优先使用直接匹配的短语释义, 添加自定义短语释义
Browse files Browse the repository at this point in the history
  • Loading branch information
testacount1 committed Mar 6, 2019
1 parent 38a4da1 commit 6851d86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
24 changes: 21 additions & 3 deletions src/查词.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as 释义处理 from './翻译/处理'
import * as 自定义词典 from './翻译/自定义词典'
import * as 模型 from './翻译/数据类型'
import * as 词典常量 from './翻译/词典相关常量'
import * as 词典 from './加载词典'

const 词形_原型变换形式 = "原型变换形式"
Expand Down Expand Up @@ -31,6 +32,7 @@ export function 取释义(选中文本: string): 模型.字段释义 {
}
}
if (处理后词 in 自定义词典.不翻译) {
// TODO: 使用"单词条"数据结构
所有词条.push({
: 处理后词,
释义: 处理后词});
Expand All @@ -43,6 +45,8 @@ export function 取释义(选中文本: string): 模型.字段释义 {
处理后词 = 释义处理.取原型(处理后词, 提取词形(词典.词形变化数据[处理后词]));
}
let 所有词形 = 提取词形(词典.词形变化数据[处理后词]);

// TODO: 使用"单词条"数据结构
所有词条.push({
: 处理后词,
释义: 词典.词典数据[处理后词],
Expand All @@ -51,9 +55,14 @@ export function 取释义(选中文本: string): 模型.字段释义 {
}
let 释义 = 选中文本;
if (所有词条.length > 1) {
let 首选释义 = 释义处理.选取释义(所有词条, 所有词);
for (let 序号 = 0; 序号 < 所有词.length; 序号++) {
释义 = 释义.replace(所有词[序号], 自定义词典.常用命名[所有词条[序号].] || 首选释义[序号])
let 短语释义 = 按短语查询(所有词条);
if (短语释义) {
释义 = 短语释义;
} else {
let 首选释义 = 释义处理.选取释义(所有词条, 所有词);
for (let 序号 = 0; 序号 < 所有词.length; 序号++) {
释义 = 释义.replace(所有词[序号], 自定义词典.常用命名[所有词条[序号].] || 首选释义[序号])
}
}
} else if (所有词条.length == 1) {
// TODO: 简化词条 (以适应状态栏宽度)
Expand Down Expand Up @@ -90,4 +99,13 @@ function 提取词形(原字符串: string): 模型.词形变化[] {
);
}
return 变化;
}

function 按短语查询(所有词条): string {
let 所有词 = [];
for (let 词条 of 所有词条) {
所有词.push(词条.);
}
let 短语 = 所有词.join(" ");
return 自定义词典.常用短语[短语] || 释义处理.首选(词典.词典数据[短语], 词典常量.词性_计算机);
}
4 changes: 4 additions & 0 deletions src/翻译/自定义词典.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const 常用命名 = {
"remove": "删除"
};

export const 常用短语 = {
"get started": "启动"
}

export const 不翻译 = {
"to": false,
"of": false,
Expand Down
26 changes: 20 additions & 6 deletions test/查词.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ suite("查词测试", () => {
检查释义("execPath", "执行路径");

检查释义("string_decoder", "字符串_译码器");
检查释义("ACCOUNT_NUMBER", "帐户_数字");
检查释义("_ACCOUNT_NUMBER", "_帐户_数字");
检查释义("account_number_0", "帐户_数字_0");
检查释义("account_number0", "帐户_数字0");
检查释义("NUMBER_ACCOUNT", "数字_帐户");
检查释义("_NUMBER_ACCOUNT", "_数字_帐户");
检查释义("number_account_0", "数字_帐户_0");
检查释义("number_account0", "数字_帐户0");

// 对于仅有一个单词且有其他字符的命名, 忽略其他字符, 按照单词查询释义
// 检查释义("account0", "帐户0");
Expand All @@ -107,21 +107,35 @@ suite("查词测试", () => {
检查释义("fsPath", "fs路径");

// 取现在分词原型
检查释义("gettingStarted", "获取出发");
检查释义("gettingGoods", "获取货物")

// 词性搭配
// 形容词+名词
检查释义("BasicCalculator", "基本的计算器");
// 副词+及物动词
// 检查释义("deepEqual", "深入地等于")
// 同时有以上两种组合
检查释义("firstSteps", "第一的步骤");
检查释义("lastSteps", "最后的步骤");

// 忽略常见单词
检查释义("is_decoder", "为_译码器");

// 获取常用命名
检查释义("eventListener", "事件监听器");

// 使用自定义短语释义
检查释义("gettingStarted", "启动");

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

// 优先查询短语
检查释义("lastName", "姓");
检查释义("firstName", "名");
检查释义("ACCOUNT_NUMBER", "帐号");
检查释义("firstSteps", "首要步骤");

});

function 检查释义(原词语, 释义) {
Expand Down

0 comments on commit 6851d86

Please sign in to comment.