feat: idiom 成语支持

This commit is contained in:
tackchen 2020-05-22 19:12:26 +08:00
parent 3fc2736af0
commit 1145a9e927
11 changed files with 224 additions and 36 deletions

View File

@ -2,6 +2,7 @@ import cnchar from '../src/main/index';
import '../src/plugin/order';
import '../src/plugin/trad';
import '../src/plugin/poly';
import '../src/plugin/idiom';
import cncharDraw from '../src/plugin/draw';
// import cnchar from '../npm/cnchar';

2
src/main/index.d.ts vendored
View File

@ -6,7 +6,7 @@ declare type strokeArg = 'letter' | 'shape' | 'count' | 'name' | 'detail' | 'arr
declare type orderToWordArg = 'match' | 'matchorder' | 'contain' | 'start' | 'array' | 'simple' | 'trad';
declare type spellToWordArg = 'poly' | 'alltone' | 'array' | 'simple' | 'trad';
declare type strokeToWordArg = 'array' | 'simple' | 'trad';
declare type pluginArg = 'order' | 'trad' | 'poly';
declare type pluginArg = 'order' | 'trad' | 'poly' | 'draw' | 'idiom' | 'xhy';
declare type orderName = '横折折撇' | '竖弯' | '横折' | '撇点' | '横斜钩' | '横' | '捺' | '横折钩' | '竖' | '竖钩' | '点' | '撇' | '撇折' | '竖折撇' | '横折折折钩' | '竖折折钩' | '提' | '弯钩' | '斜钩' | '横折折' | '横撇' | '横折提' | '横折折折' | '竖提' | '竖弯钩'
| '竖折折' | '横撇弯钩' | '卧钩' | '横折弯' | '横钩';

View File

@ -1,7 +1,9 @@
let version = require('./version');
let {spell, tones, stroke, arg, has, _throw, _wran, dealUpLowFirst, removeTone, sumStroke, isCnChar, checkArgs, initCnchar} = require('./tool');
let {spell, tones, stroke, arg, has, _throw, _wran,
dealUpLowFirst, removeTone, sumStroke, isCnChar,
checkArgs, initCnchar, transformTone} = require('./tool');
let dict = require('./dict');
let initSpellToWord = require('./spellToWord');
let {initSpellToWord} = require('./spellToWord');
let initStrokeToWord = require('./strokeToWord');
function _spell (...args) {
@ -42,7 +44,8 @@ let cnchar = {
},
plugins: [],
use,
_: {arg, has, _throw, tones, _wran, dealUpLowFirst, removeTone, sumStroke, isCnChar, checkArgs, dict: {}},
_: {arg, has, _throw, tones, _wran, dealUpLowFirst, removeTone,
sumStroke, isCnChar, checkArgs, transformTone, dict: {}},
type: {
spell: arg,
stroke: {

View File

@ -41,16 +41,7 @@ function spellToWord (...args) {
if (typeof spell !== 'string') {
throw new Error('spellToWord: 输入必须是字符串');
}
if (spell.indexOf('v') !== -1) {
spell = spell.replace('v', 'ü');
}
let tone = spell[spell.length - 1];
if (parseInt(tone).toString() === tone) {
spell = spell.substr(0, spell.length - 1);
tone = parseInt(tone);
} else {
tone = false;
}
let info = _.transformTone(spell, false);
args = args.splice(1);
_.checkArgs('spellToWord', args);
let argRes = {
@ -63,10 +54,6 @@ function spellToWord (...args) {
argRes.simple = argRes.trad = true;
}
let res = '';
let info = _.removeTone(spell, false);
if (tone !== false) {
info.tone = tone;
}
let str = dict[info.spell].substr(2);
for (let i = 0; i < str.length; i += 2) {
let word = str[i];
@ -98,4 +85,4 @@ function spellToWord (...args) {
}
return res;
}
module.exports = initSpellToWord;
module.exports = {initSpellToWord, spellInfo};

View File

@ -1,4 +1,4 @@
const {spellInfo} = require('./spellToWord');
const tones = 'āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ*ńňǹ'; // * 表示n的一声
const noTones = 'aoeiuün';
let defDict = require('./spell-default.json');
@ -289,6 +289,16 @@ function checkArgs (type, args, jumpNext) {
}
} else if (type === 'strokeToWord') {
} else if (type === 'spellToWord') {
} else if (type === 'idiom') {
if (has(args, 'spell')) {
check(['stroke', 'char']);
} else if (has(args, 'stroke')) {
check(['tone', 'char']);
} else {
check(['tone']);
}
} else if (type === 'xhy') {
}
warnArgs(useless, '无效', type);
warnArgs(ignore, '被忽略', type);
@ -299,6 +309,41 @@ function warnArgs (arr, txt, type) {
_wran(`以下参数${txt}:${JSON.stringify(arr)}; 可选值:[${Object.keys(_cnchar.type[type])}]`);
}
}
// lv2 => {spell:'lü', tone: 2, index: 2, isTrans: true}
// lǘ => {spell:'lü', tone: 2, index: 2, isTrans: false}
// needTone = true: lv2 => {spell:'lǘ', tone: 2, index: 2, isTrans: true}
function transformTone (spell, needTone, type = 'low') {
if (spell.indexOf('v') !== -1) {
spell = spell.replace('v', 'ü');
}
let tone = spell[spell.length - 1];
let index = -1;
let isTrans = false;
if (parseInt(tone).toString() === tone) { // lv2
spell = spell.substr(0, spell.length - 1);
let info = spellInfo(spell);
index = info.index;
tone = parseInt(tone);
isTrans = true;
if (needTone) {
spell = setTone(spell, index - 1, tone);
}
} else { // lǘ
let info = spellInfo(spell);
index = info.index;
tone = info.tone; // 声调已经带好了的情况
if (!needTone && tone !== 0) { // 需要去除音调并且有音调
spell = info.spell;
}
}
if (type === 'low') {
spell = spell.toLowerCase();
} else if (type === 'up') {
spell = spell.toUpperCase();
}
return {spell, tone, index, isTrans};
}
module.exports = {
_throw, _wran, arg, isCnChar, has, spell, stroke, dealUpLowFirst, removeTone, sumStroke, checkArgs, initCnchar, tones
_throw, _wran, arg, isCnChar, has, spell, stroke, dealUpLowFirst, removeTone, sumStroke, checkArgs, initCnchar, tones, transformTone
};

View File

@ -0,0 +1,110 @@
/**
* 汉字 ['a','','c','']
* 笔画 [1,0,1,0]
* 拼音 ['shi', '']
*/
const dict = require('./idiom.json');
let arg = {
char: 'char',
stroke: 'stroke',
spell: 'spell',
tone: 'tone'
};
let _cnchar = null;
// spell > stroke > char
// spell 和 stroke 仅在 引入cnchar之后才可用
function idiom (...args) {
if (args.length === 0) {
console.warn('idiom: 请输入搜索项');
return;
}
let input = args[0];
args = args.slice(1);
if (!(input instanceof Array || (typeof input === 'number' && args.indexOf(arg.stroke) !== -1))) {
console.warn('idiom 输入参数仅支持数组 或 stroke模式下的数字');
return;
}
if (!_cnchar) { // 单独使用的idiom 只支持汉字查询方式
checkArg(args, arg.stroke);
checkArg(args, arg.spell);
checkArg(args, arg.tone);
return idiomWithChar(input);
} else {
_cnchar._.checkArgs('idiom', args);
if (_cnchar._.has(args, arg.spell)) {
return idiomWithSpell(input, _cnchar._.has(args, arg.tone));
} else if (_cnchar._.has(args, arg.stroke)) {
return idiomWithStroke(input);
} else {
return idiomWithChar(input);
}
}
}
function idiomWithChar (input) {
return dict.filter((item) => {
return compareCommon(input, item.split(''));
});
}
// needTone是否需要匹配音调
function idiomWithSpell (input, needTone) {
transformSpell(input, needTone);
let args = ['low', 'array'];
if (needTone) {
args.push('tone');
}
return dict.filter((item) => {
return compareCommon(input, _cnchar.spell(item, ...args));
});
}
function transformSpell (input, needTone) {
input.forEach((item, i) => {
if (item) {
let info = _cnchar._.transformTone(item, needTone);
input[i] = info.spell;
}
});
}
function idiomWithStroke (input) {
if (typeof input === 'number') { // 总笔画数为多少
return dict.filter((item) => {
return input === _cnchar.stroke(item);
});
}
return dict.filter((item) => {
return compareCommon(input, _cnchar.stroke(item, 'array'));
});
}
/**
* ['五','','十',''],['五','光','十','色'] => true
* ['wu','','shi',''],['wu','guang','shi','se'] => true
* [4, 0, 2, 0],[4, 6, 2, 6] => true
*/
//
//
//
function compareCommon (input, target) {
for (let i = 0; i < input.length; i++) {
if (input[i] && input[i] !== target[i] ) {
return false;
}
}
return true;
}
function checkArg (args, name) {
if (args.indexOf(name) !== -1) {
console.warn(`未引入cnchar,idiom不支持${name}参数`);
}
}
function setCnchar (cnchar) {
_cnchar = cnchar;
}
module.exports = {idiom, arg, setCnchar};

21
src/plugin/idiom/index.d.ts vendored Normal file
View File

@ -0,0 +1,21 @@
declare type DrawType = 'normal' | 'animation' | 'stroke' | 'test';
declare type TestStatusType = 'mistake' | 'correct' | 'complete';
export declare interface Draw {
(text:string, option?:DrawOption):Writer;
TYPE: {
ANIMATION: 'animation',
NORMAL: 'normal',
STROKE: 'stroke',
TEST: 'test'
},
TEST_STATUS: {
MISTAKE: 'mistake',
CORRECT: 'correct',
COMPLETE: 'complete'
}
}
declare const draw: Draw;
export default draw;

View File

@ -1,6 +1,4 @@
// powerd by hanzi-writer v2.2.2
const idiom = require('./writer');
const {idiom, arg, setCnchar} = require('./idiom');
function main (cnchar) {
if (cnchar.plugins.indexOf('idiom') !== -1) {
@ -8,16 +6,19 @@ function main (cnchar) {
}
cnchar.plugins.push('idiom');
cnchar.idiom = idiom;
cnchar.type.idiom = arg;
}
function init (cnchar) {
if (typeof window === 'object') {
if (typeof window === 'object' && !window.CncharIdiom) {
window.CncharIdiom = idiom;
}
if (typeof window === 'object' && window.CnChar) {
main(window.CnChar);
setCnchar(window.CnChar);
} else if (typeof cnchar !== 'undefined') {
main(cnchar);
setCnchar(cnchar);
}
}

21
src/plugin/xhy/index.d.ts vendored Normal file
View File

@ -0,0 +1,21 @@
declare type DrawType = 'normal' | 'animation' | 'stroke' | 'test';
declare type TestStatusType = 'mistake' | 'correct' | 'complete';
export declare interface Draw {
(text:string, option?:DrawOption):Writer;
TYPE: {
ANIMATION: 'animation',
NORMAL: 'normal',
STROKE: 'stroke',
TEST: 'test'
},
TEST_STATUS: {
MISTAKE: 'mistake',
CORRECT: 'correct',
COMPLETE: 'complete'
}
}
declare const draw: Draw;
export default draw;

View File

@ -1,19 +1,18 @@
// powerd by hanzi-writer v2.2.2
const draw = require('./writer');
const xhy = require('./writer');
function main (cnchar) {
if (cnchar.plugins.indexOf('draw') !== -1) {
if (cnchar.plugins.indexOf('xhy') !== -1) {
return;
}
cnchar.plugins.push('draw');
cnchar.draw = draw;
cnchar.plugins.push('xhy');
cnchar.xhy = xhy;
}
function init (cnchar) {
if (typeof window === 'object') {
window.CncharIdiom = draw;
window.CncharIdiom = draw;
if (typeof window === 'object' && !window.CncharXHY) {
window.CncharXHY = xhy;
}
if (typeof window === 'object' && window.CnChar) {
main(window.CnChar);
@ -22,8 +21,8 @@ function init (cnchar) {
}
}
draw.init = init;
xhy.init = init;
init();
module.exports = draw;
module.exports = xhy;

View File

@ -157,9 +157,9 @@ module.exports = [
];
},
expect: [
'個人',
'個人',
'①个亾',
'个人'
'个人'
]
}
];