mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
pinyin-match
This commit is contained in:
parent
9798688496
commit
45bc1b7afd
21
src/node_modules/pinyin-match/LICENSE
generated
vendored
Normal file
21
src/node_modules/pinyin-match/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 All contributors to pinyin-match
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
72
src/node_modules/pinyin-match/README.md
generated
vendored
Normal file
72
src/node_modules/pinyin-match/README.md
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
# pinyin-match
|
||||||
|
|
||||||
|
能够使用拼音快速检索目标。
|
||||||
|
|
||||||
|
1. 28.5KB (gzip => 20.3KB)
|
||||||
|
2. 支持多音字、拼音首字母匹配,具备分词功能
|
||||||
|
3. 覆盖 6763 个汉字
|
||||||
|
4. 返回位置信息,可用于高亮匹配字符
|
||||||
|
5. 在长多音字串下依然有高性能
|
||||||
|
|
||||||
|
在线演示:<http://139.199.181.200:8686>;
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm install pinyin-match --save
|
||||||
|
```
|
||||||
|
|
||||||
|
也支持`<script>`引入
|
||||||
|
|
||||||
|
`<script src="pinyin-match/dist/main.js"></script>`
|
||||||
|
|
||||||
|
`PinyinMatch.match('xxx', 'x')`
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### .match(input, keyword)
|
||||||
|
|
||||||
|
查询匹配拼音的数据。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
|
||||||
|
1. input `{string}` 目标字符串
|
||||||
|
2. keyword `{string}` 输入的拼音或其他关键词
|
||||||
|
|
||||||
|
返回:
|
||||||
|
|
||||||
|
`{[Array]|{Boolean}}`
|
||||||
|
|
||||||
|
## 使用范例
|
||||||
|
|
||||||
|
列表项为字符串:
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
const PinyinMatch = require('pinyin-match');
|
||||||
|
let test = '123曾经沧海难为水除却巫山不是云'
|
||||||
|
|
||||||
|
PinyinMatch.match(test, '23曾'); // [1, 3]
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'cjc') // [3, 5]
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'cengjingcanghai') // [3, 6]
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'cengjingcangha') // [3, 6]
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'engjingcanghai') // false
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'zengjingcang') // [3, 5]
|
||||||
|
|
||||||
|
PinyinMatch.match(test, 'sdjkelwqf') // false
|
||||||
|
|
||||||
|
PinyinMatch.match(text, 'zengji ng cang') // [3, 5]
|
||||||
|
|
||||||
|
PinyinMatch.match(text, 'zengji ng cangsdjfkl') // false
|
||||||
|
|
||||||
|
PinyinMatch.match(' 我 爱你 中 国 ', 'nzg') // [6, 12]
|
||||||
|
|
||||||
|
PinyinMatch.match(' 我 爱你 中 国 ', '爱你中') // [5, 8]
|
||||||
|
|
||||||
|
```
|
||||||
|
**如果对你项目有帮助请给个Star**
|
1
src/node_modules/pinyin-match/dist/main.js
generated
vendored
Normal file
1
src/node_modules/pinyin-match/dist/main.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
6
src/node_modules/pinyin-match/index.d.ts
generated
vendored
Normal file
6
src/node_modules/pinyin-match/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
declare namespace Pinyin {
|
||||||
|
export function match(input: string, keys: string): [number, number] | boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export as namespace Pinyin;
|
||||||
|
export = Pinyin;
|
63
src/node_modules/pinyin-match/package.json
generated
vendored
Normal file
63
src/node_modules/pinyin-match/package.json
generated
vendored
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"_from": "pinyin-match",
|
||||||
|
"_id": "pinyin-match@1.1.1",
|
||||||
|
"_inBundle": false,
|
||||||
|
"_integrity": "sha512-stJVNJGHOEl3B3qXgeXJhWk3xfYQ3X7VaHBIIQHI2/9E1hNK9cmkigei0F+1l+uBM2XPVgxtmzg08AuY7n5Rnw==",
|
||||||
|
"_location": "/pinyin-match",
|
||||||
|
"_phantomChildren": {},
|
||||||
|
"_requested": {
|
||||||
|
"type": "tag",
|
||||||
|
"registry": true,
|
||||||
|
"raw": "pinyin-match",
|
||||||
|
"name": "pinyin-match",
|
||||||
|
"escapedName": "pinyin-match",
|
||||||
|
"rawSpec": "",
|
||||||
|
"saveSpec": null,
|
||||||
|
"fetchSpec": "latest"
|
||||||
|
},
|
||||||
|
"_requiredBy": [
|
||||||
|
"#USER",
|
||||||
|
"/"
|
||||||
|
],
|
||||||
|
"_resolved": "https://registry.npmjs.org/pinyin-match/-/pinyin-match-1.1.1.tgz",
|
||||||
|
"_shasum": "b13000800e4c0c2f22904ecf4a07423ddf9c1023",
|
||||||
|
"_spec": "pinyin-match",
|
||||||
|
"_where": "C:\\Users\\fofol\\OneDrive\\Configs\\uTools\\QuickerCommand\\src",
|
||||||
|
"author": {
|
||||||
|
"name": "774898896@qq.com"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/xmflswood/pinyin-match/issues"
|
||||||
|
},
|
||||||
|
"bundleDependencies": false,
|
||||||
|
"deprecated": false,
|
||||||
|
"description": "支持多音字,首字母缩写的拼音搜索引擎",
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-core": "^6.26.3",
|
||||||
|
"babel-loader": "^7.1.5",
|
||||||
|
"babel-preset-env": "^1.7.0",
|
||||||
|
"mocha": "^5.2.0",
|
||||||
|
"webpack": "^4.16.1",
|
||||||
|
"webpack-cli": "^3.1.2"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/xmflswood/pinyin-match#readme",
|
||||||
|
"keywords": [
|
||||||
|
"拼音",
|
||||||
|
"chinese",
|
||||||
|
"pinyin",
|
||||||
|
"match"
|
||||||
|
],
|
||||||
|
"license": "ISC",
|
||||||
|
"main": "dist/main.js",
|
||||||
|
"name": "pinyin-match",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/xmflswood/pinyin-match.git"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"test": "mocha"
|
||||||
|
},
|
||||||
|
"typings": "index.d.ts",
|
||||||
|
"version": "1.1.1"
|
||||||
|
}
|
169
src/node_modules/pinyin-match/src/index.js
generated
vendored
Normal file
169
src/node_modules/pinyin-match/src/index.js
generated
vendored
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
const dict = require('./pinyin_dict_notone').pinyin_dict_notone
|
||||||
|
const allPinyin = require('./pinyin_dict_notone').allPinyin
|
||||||
|
const notone = parseDict()
|
||||||
|
let storage = {}
|
||||||
|
function parseDict() {
|
||||||
|
let parseDict = {}
|
||||||
|
for (let i in dict) {
|
||||||
|
let temp = dict[i]
|
||||||
|
for (let j = 0, len = temp.length; j < len; j++) {
|
||||||
|
if (!parseDict[temp[j]]) {
|
||||||
|
parseDict[temp[j]] = i
|
||||||
|
} else {
|
||||||
|
parseDict[temp[j]] = parseDict[temp[j]] + ' ' + i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parseDict
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPinyin(cn) {
|
||||||
|
let result = []
|
||||||
|
for (let i = 0, len = cn.length; i < len; i ++) {
|
||||||
|
let temp = cn.charAt(i)
|
||||||
|
result.push(notone[temp] || temp)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
// 对输入拼音进行切分
|
||||||
|
function wordBreak(s) {
|
||||||
|
let result = []
|
||||||
|
let solutions = []
|
||||||
|
let len = s.length
|
||||||
|
let possible = []
|
||||||
|
for (let i = 0; i <= s.length; i++) {
|
||||||
|
possible.push(true)
|
||||||
|
}
|
||||||
|
getAllSolutions(0, s, result, solutions, possible)
|
||||||
|
return solutions
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAllSolutions(start, s, result, solutions, possible) {
|
||||||
|
if (start === s.length) {
|
||||||
|
solutions.push(result.join(' '))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for (let i = start; i < s.length; i++) {
|
||||||
|
let piece = s.substring(start, i + 1)
|
||||||
|
let match = false
|
||||||
|
// 最后一个音特殊处理,不需要全部打完整
|
||||||
|
if (allPinyin.some(i => i.indexOf(piece) === 0) && !s[i + 1] && possible[i + 1]) {
|
||||||
|
if (piece.length === 1) {
|
||||||
|
result.push(piece)
|
||||||
|
} else {
|
||||||
|
let s = []
|
||||||
|
allPinyin.forEach(i => {
|
||||||
|
if (i.indexOf(piece) === 0) {
|
||||||
|
s.push(i)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
result.push(s)
|
||||||
|
}
|
||||||
|
match = true
|
||||||
|
} else {
|
||||||
|
if (allPinyin.indexOf(piece) !== -1 && possible[i + 1]) {
|
||||||
|
result.push(piece)
|
||||||
|
match = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (match) {
|
||||||
|
let beforeChange = solutions.length
|
||||||
|
getAllSolutions(i + 1, s, result, solutions, possible)
|
||||||
|
if (solutions.length === beforeChange) {
|
||||||
|
possible[i + 1] = false
|
||||||
|
}
|
||||||
|
result.pop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 获取输入拼音的所有组合(切分 + 首字母)
|
||||||
|
function getFullKey(key) {
|
||||||
|
let result = []
|
||||||
|
wordBreak(key).forEach(i => {
|
||||||
|
let item = i.split(' ')
|
||||||
|
let last = item.length - 1
|
||||||
|
if (item[last].indexOf(',')) {
|
||||||
|
let keys = item[last].split(',')
|
||||||
|
keys.forEach(j => {
|
||||||
|
item.splice(last, 1, j)
|
||||||
|
result.push(JSON.parse(JSON.stringify(item)))
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
result.push(item)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (result.length === 0 || (result[0].length !== key.length)) {
|
||||||
|
result.push(key.split(''))
|
||||||
|
}
|
||||||
|
// 缓存当前结果 避免重复计算
|
||||||
|
storage = {[key]: result}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
function point2point(test, key, last, extend) {
|
||||||
|
if (!test) return false
|
||||||
|
let a = test.split(' ')
|
||||||
|
a.forEach(i => {
|
||||||
|
if (i.length > 0 && extend) {
|
||||||
|
a.push(i.charAt(0))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (!last) {
|
||||||
|
return a.indexOf(key) !== -1
|
||||||
|
}
|
||||||
|
return a.some((i) => i.indexOf(key) === 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function match(input, keys) {
|
||||||
|
input = input.toLowerCase()
|
||||||
|
keys = keys.replace(/\s+/g, '').toLowerCase()
|
||||||
|
let indexOf = input.indexOf(keys)
|
||||||
|
if (indexOf !== -1) {
|
||||||
|
return [indexOf, indexOf + keys.length - 1]
|
||||||
|
}
|
||||||
|
// 原文匹配(带空格)
|
||||||
|
let noPyIndex = getIndex(input.split(''), [keys.split('')], keys)
|
||||||
|
if (noPyIndex) return noPyIndex
|
||||||
|
// pinyin匹配
|
||||||
|
let py = getPinyin(input)
|
||||||
|
let fullString = storage[keys] || getFullKey(keys)
|
||||||
|
return getIndex(py, fullString, keys)
|
||||||
|
}
|
||||||
|
function getIndex(py, fullString, keys) {
|
||||||
|
for (let p = 0; p < py.length; p++) {
|
||||||
|
for (let k = 0; k < fullString.length; k++) {
|
||||||
|
let key = fullString[k]
|
||||||
|
let keyLength = key.length
|
||||||
|
let extend = (keyLength === keys.length)
|
||||||
|
let isMatch = true
|
||||||
|
let i = 0
|
||||||
|
let preSpaceNum = 0
|
||||||
|
let spaceNum = 0
|
||||||
|
if (keyLength <= py.length) {
|
||||||
|
for (; i < key.length; i++) {
|
||||||
|
if (i === 0 && py[p + i + preSpaceNum] === ' ') {
|
||||||
|
preSpaceNum += 1
|
||||||
|
i -= 1
|
||||||
|
} else {
|
||||||
|
if (py[p + i + spaceNum] === ' ') {
|
||||||
|
spaceNum += 1
|
||||||
|
i -= 1
|
||||||
|
} else {
|
||||||
|
if (!point2point(py[p + i + spaceNum], key[i], (py[p + i + 1] && key[i + 1]) ? false : true, extend)) {
|
||||||
|
isMatch = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isMatch) {
|
||||||
|
return [p + preSpaceNum, spaceNum + p + i - 1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
const pinyin = {
|
||||||
|
match
|
||||||
|
}
|
||||||
|
module.exports = pinyin
|
8
src/node_modules/pinyin-match/src/pinyin_dict_notone.js
generated
vendored
Normal file
8
src/node_modules/pinyin-match/src/pinyin_dict_notone.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
50
src/node_modules/pinyin-match/test/index.test.js
generated
vendored
Normal file
50
src/node_modules/pinyin-match/test/index.test.js
generated
vendored
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
const assert = require('assert')
|
||||||
|
const PinyinMatch = require('../src/index')
|
||||||
|
let text = '123曾经沧海难为水除却巫山不是云'
|
||||||
|
|
||||||
|
describe('PinyinMatch', () => {
|
||||||
|
describe('#match', () => {
|
||||||
|
it('精准匹配', () => {
|
||||||
|
assert.deepEqual([1, 3], PinyinMatch.match(text, '23曾'))
|
||||||
|
})
|
||||||
|
it('首字母匹配', () => {
|
||||||
|
assert.deepEqual([3, 5], PinyinMatch.match(text, 'cjc'))
|
||||||
|
})
|
||||||
|
it('完整拼音匹配', () => {
|
||||||
|
assert.deepEqual([3, 6], PinyinMatch.match(text, 'cengjingcanghai'))
|
||||||
|
})
|
||||||
|
it('最后一个拼音不完整', () => {
|
||||||
|
assert.deepEqual([3, 6], PinyinMatch.match(text, 'cengjingcangha'))
|
||||||
|
})
|
||||||
|
it('分词功能', () => {
|
||||||
|
assert.equal(false, PinyinMatch.match(text, 'engjing'))
|
||||||
|
})
|
||||||
|
it('多音字', () => {
|
||||||
|
assert.deepEqual([3, 5], PinyinMatch.match(text, 'zengjingcang'))
|
||||||
|
})
|
||||||
|
it('忽略空格', () => {
|
||||||
|
assert.deepEqual([3, 5], PinyinMatch.match(text, 'zengji ng cang'))
|
||||||
|
})
|
||||||
|
it('超出', () => {
|
||||||
|
assert.deepEqual(false, PinyinMatch.match(text, 'zengji ng cangsdjfkl'))
|
||||||
|
})
|
||||||
|
it('忽略空格', () => {
|
||||||
|
assert.deepEqual([6, 12], PinyinMatch.match(' 我 爱你 中 国 ', 'nzg'))
|
||||||
|
})
|
||||||
|
it('忽略空格原文', () => {
|
||||||
|
assert.deepEqual([5, 8], PinyinMatch.match(' 我 爱你 中 国s ', '爱你中'))
|
||||||
|
})
|
||||||
|
it('忽略空格原文', () => {
|
||||||
|
assert.deepEqual([5, 13], PinyinMatch.match(' 我 爱你 中 国s ', '爱你中国s'))
|
||||||
|
})
|
||||||
|
it('超出原文', () => {
|
||||||
|
assert.deepEqual(false, PinyinMatch.match(' 我 爱你 中 国s ', '爱你中国sj'))
|
||||||
|
})
|
||||||
|
it('bao', () => {
|
||||||
|
assert.deepEqual([1, 1], PinyinMatch.match('淘宝', 'bao'))
|
||||||
|
})
|
||||||
|
it('issues 7', () => {
|
||||||
|
assert.deepEqual([0, 0], PinyinMatch.match('卡号打开', 'ka'))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
29
src/node_modules/pinyin-match/webpack.config.js
generated
vendored
Normal file
29
src/node_modules/pinyin-match/webpack.config.js
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const path = require('path')
|
||||||
|
const webpack = require('webpack')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: path.resolve(__dirname, 'src', 'index.js'),
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
filename: 'main.js',
|
||||||
|
library: 'PinyinMatch',
|
||||||
|
globalObject: 'typeof self !== \'undefined\' ? self : this',
|
||||||
|
libraryTarget: 'umd'
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [{
|
||||||
|
test: /\.js$/,
|
||||||
|
use: [{
|
||||||
|
loader: 'babel-loader',
|
||||||
|
options: {
|
||||||
|
presets: [
|
||||||
|
'env'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new webpack.optimize.ModuleConcatenationPlugin()
|
||||||
|
]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user