修改 字符bug 和 繁简转化

This commit is contained in:
toolgood
2020-05-17 09:57:09 +08:00
parent fa87710500
commit 3665836fdc
9 changed files with 202 additions and 161 deletions
+42 -30
View File
@@ -1,6 +1,9 @@
// ToolGood.Words.Pinyin.js
// 2020, Lin Zhijun, https://github.com/toolgood/ToolGood.Words
// Licensed under the Apache License 2.0
// 更新日志
// 2020.05.17 修改,支持大于0xffff的字符
function Pinyin() {
function WordsSearch() {
function TrieNode() {
@@ -39,6 +42,9 @@ function Pinyin() {
this.maxflag = 0;
this.Add = function (c, node3) {
if (typeof c !== 'number') {
c = parseInt(c);
}
if (this.minflag > c) { this.minflag = c; }
if (this.maxflag < c) { this.maxflag = c; }
this.m_values[c] = node3;
@@ -109,7 +115,7 @@ function Pinyin() {
nd.Failure = root;
else {
nd.Failure = r.m_values[c];
for (const key2 in nd.Failure.Results) {
for (var key2 in nd.Failure.Results) {
if (nd.Failure.Results.hasOwnProperty(key2) == false) { continue; }
var result = nd.Failure.Results[key2];
nd.SetResults(result);
@@ -126,19 +132,19 @@ function Pinyin() {
var oldNode = allNode[i];
var newNode = allNode2[i];
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
var index = oldNode.m_values[key].Index;
newNode.Add(key, allNode2[index]);
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
while (oldNode != root) {
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
if (newNode.HasKey(key) == false) {
var index = oldNode.m_values[key].Index;
@@ -146,7 +152,7 @@ function Pinyin() {
}
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
@@ -155,15 +161,15 @@ function Pinyin() {
allNode = null;
root = null;
var first = [];
for (let index = 0; index < 0xffff; index++) {
first.push(null);
}
for (const key in allNode2[0].m_values) {
if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
first[key] = allNode2[0].m_values[key];
}
_first = first;
// var first = [];
// for (let index = 0; index < 0xffff; index++) {
// first.push(null);
// }
// for (var key in allNode2[0].m_values) {
// if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
// first[key] = allNode2[0].m_values[key];
// }
_first = allNode2[0];
}
this.FindAll = function (text) {
@@ -171,20 +177,20 @@ function Pinyin() {
var list = [];
for (let i = 0; i < text.length; i++) {
const t = text.charCodeAt(i);
var t = text.charCodeAt(i);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
if (tn.End) {
for (let j = 0; j < tn.Results.length; j++) {
const item = tn.Results[j];
var item = tn.Results[j];
var keyword = _keywords[item];
list.push({
Keyword: keyword,
@@ -217,9 +223,10 @@ function Pinyin() {
* @param {any} text
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetPinyinList = function (text, tone = 0) {
this.GetPinyinList = function (text, tone) {
InitPyWords();
if (tone) { tone = 1; }
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
var list = [];
for (var i = 0; i < text.length; i++) {
list.push(null);
@@ -257,8 +264,9 @@ function Pinyin() {
* @param {any} text
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetPinyin = function (text, tone = 0) {
if (tone) { tone = 1; }
this.GetPinyin = function (text, tone) {
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
var list = this.GetPinyinList(text, tone);
var sb = "";
@@ -277,8 +285,9 @@ function Pinyin() {
* @param {any} text
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetFirstPinyin = function (text, tone = 0) {
if (tone) { tone = 1; }
this.GetFirstPinyin = function (text, tone) {
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
var list = this.GetPinyinList(text, tone);
var sb = "";
@@ -296,8 +305,9 @@ function Pinyin() {
* @param {any} text
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetAllPinyin = function (c, tone = 0) {
if (tone) { tone = 1; }
this.GetAllPinyin = function (c, tone) {
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
var idx = c.charCodeAt(0);
if (idx >= 0x3400 && idx <= 0x9fd5) {
@@ -324,8 +334,9 @@ function Pinyin() {
* @param {any} name
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetPinyinListForName = function (name, tone = 0) {
if (tone) { tone = 1; }
this.GetPinyinListForName = function (name, tone) {
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
var list = [];
var xing;
@@ -375,8 +386,9 @@ function Pinyin() {
* @param {any} name
* @param {any} tone 是否带声调 1 是 0否
*/
this.GetPinyinForName = function (name, tone = 0) {
if (tone) { tone = 1; }
this.GetPinyinForName = function (name, tone) {
if (tone == undefined) { tone = 0; }
else if (tone) { tone = 1; }
return this.GetPinyinListForName(name, tone).join("");
}
File diff suppressed because one or more lines are too long
+36 -29
View File
@@ -1,6 +1,9 @@
// ToolGood.Words.StringSearch.js
// 2020, Lin Zhijun, https://github.com/toolgood/ToolGood.Words
// Licensed under the Apache License 2.0
// 更新日志
// 2020.05.17 修改,支持大于0xffff的字符
function StringSearch() {
function TrieNode() {
this.Index = 0;
@@ -60,7 +63,7 @@ function StringSearch() {
return null;
}
}
var _first = [];
var _keywords = [];
@@ -115,7 +118,7 @@ function StringSearch() {
nd.Failure = root;
else {
nd.Failure = r.m_values[c];
for (const key2 in nd.Failure.Results) {
for (var key2 in nd.Failure.Results) {
if (nd.Failure.Results.hasOwnProperty(key2) == false) { continue; }
var result = nd.Failure.Results[key2];
nd.SetResults(result);
@@ -132,18 +135,18 @@ function StringSearch() {
var oldNode = allNode[i];
var newNode = allNode2[i];
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
var index = oldNode.m_values[key].Index;
newNode.Add(key, allNode2[index]);
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
while (oldNode != root) {
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
if (newNode.HasKey(key) == false) {
var index = oldNode.m_values[key].Index;
@@ -151,7 +154,7 @@ function StringSearch() {
}
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
@@ -160,15 +163,15 @@ function StringSearch() {
allNode = null;
root = null;
var first = [];
for (let index = 0; index < 0xffff; index++) {
first.push(null);
}
for (const key in allNode2[0].m_values) {
if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
first[key] = allNode2[0].m_values[key];
}
_first = first;
// var first = [];
// for (let index = 0; index < 0xffff; index++) {
// first.push(null);
// }
// for (var key in allNode2[0].m_values) {
// if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
// first[key] = allNode2[0].m_values[key];
// }
_first = allNode2[0];
}
/**
* 查找第一个匹配 字符串
@@ -177,14 +180,14 @@ function StringSearch() {
this.FindFirst = function (text) {
var ptr = null;
for (let index = 0; index < text.length; index++) {
const t = text.charCodeAt(index);
var t = text.charCodeAt(index);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
@@ -206,20 +209,20 @@ function StringSearch() {
var list = [];
for (let index = 0; index < text.length; index++) {
const t = text.charCodeAt(index);
var t = text.charCodeAt(index);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
if (tn.End) {
for (let j = 0; j < tn.Results.length; j++) {
const item = tn.Results[j];
var item = tn.Results[j];
list.push(_keywords[item]);
}
}
@@ -236,14 +239,14 @@ function StringSearch() {
this.ContainsAny = function (text) {
var ptr = null;
for (let index = 0; index < text.length; index++) {
const t = text.charCodeAt(index);
var t = text.charCodeAt(index);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
@@ -261,20 +264,24 @@ function StringSearch() {
* @param {any} text
* @param {any} replaceChar
*/
this.Replace = function (text, replaceChar = '*') {
this.Replace = function (text, replaceChar) {
if (replaceChar == undefined) {
replaceChar = '*'
}
var result = text.split('');
var ptr = null;
for (var i = 0; i < text.length; i++) {
const t = text.charCodeAt(i);
var t = text.charCodeAt(i);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
+3 -1
View File
@@ -1,4 +1,6 @@
// ToolGood.Words.StringSearch.js
// 2020, Lin Zhijun, https://github.com/toolgood/ToolGood.Words
// Licensed under the Apache License 2.0
function StringSearch(){function q(){this.Layer=this.Index=0;this.End=!1;this.Char="";this.Results=[];this.m_values={};this.Parent=this.Failure=null;this.Add=function(c){if(null!=this.m_values[c])return this.m_values[c];var a=new q;a.Parent=this;a.Char=c;return this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);this.Results.push(c)}}function r(){this.End=!1;this.Results=[];this.m_values={};this.minflag=65535;this.maxflag=0;this.Add=function(c,a){this.minflag>c&&(this.minflag=c);this.maxflag<c&&(this.maxflag=c);this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);-1==this.Results.indexOf(c)&&this.Results.push(c)};this.HasKey=function(c){return void 0!=this.m_values[c]};this.TryGetValue=function(c){return this.minflag<=c&&this.maxflag>=c?this.m_values[c]:null}}var k=[],n=[];this.SetKeywords=function(c){n=c;c=new q;for(var a={},d=0;d<n.length;d++){for(var e=n[d],b=c,g=0;g<e.length;g++)b=b.Add(e.charCodeAt(g)),0==b.Layer&&(b.Layer=g+1,a[b.Layer]||(a[b.Layer]=[]),a[b.Layer].push(b));b.SetResults(d)}e=[];e.push(c);for(var f in a)for(b=a[f],d=0;d<b.length;d++)e.push(b[d]);for(d=1;d<e.length;d++){b=e[d];b.Index=d;f=b.Parent.Failure;for(a=b.Char;null!=f&&!f.m_values[a];)f=f.Failure;if(null==f)b.Failure=c;else{b.Failure=f.m_values[a];for(var h in b.Failure.Results)0!=b.Failure.Results.hasOwnProperty(h)&&b.SetResults(b.Failure.Results[h])}}c.Failure=c;h=[];for(d=0;d<e.length;d++)h.push(new r);for(d=0;d<h.length;d++){b=e[d];f=h[d];for(var l in b.m_values)0!=b.m_values.hasOwnProperty(l)&&(a=b.m_values[l].Index,f.Add(l,h[a]));for(a=0;a<b.Results.length;a++)f.SetResults(b.Results[a]);for(b=b.Failure;b!=c;){for(var m in b.m_values)0!=b.m_values.hasOwnProperty(m)&&0==f.HasKey(m)&&(a=b.m_values[m].Index,f.Add(m,h[a]));for(a=0;a<b.Results.length;a++)f.SetResults(b.Results[a]);b=b.Failure}}l=[];for(m=0;65535>m;m++)l.push(null);for(var p in h[0].m_values)0!=h[0].m_values.hasOwnProperty(p)&&(l[p]=h[0].m_values[p]);k=l};this.FindFirst=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=k[e]:(a=a.TryGetValue(e))||(a=k[e]);if(null!=a&&a.End)return n[a.Results[0]]}return null};this.FindAll=function(c){for(var a=null,d=[],e=0;e<c.length;e++){var b=c.charCodeAt(e);null==a?a=k[b]:(a=a.TryGetValue(b))||(a=k[b]);if(null!=a&&a.End)for(b=0;b<a.Results.length;b++)d.push(n[a.Results[b]])}return d};this.ContainsAny=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=k[e]:(a=a.TryGetValue(e))||(a=k[e]);if(null!=a&&a.End)return!0}return!1};this.Replace=function(c,a){a=void 0===a?"*":a;for(var d=c.split(""),e=null,b=0;b<c.length;b++){var g=c.charCodeAt(b);null==e?e=k[g]:(e=e.TryGetValue(g))||(e=k[g]);if(null!=e&&e.End)for(g=b+1-n[e.Results[0]].length;g<=b;g++)d[g]=a}return d.join("")}};
// 更新日志
// 2020.05.17 修改,支持大于0xffff的字符
function StringSearch(){function n(){this.Layer=this.Index=0;this.End=!1;this.Char="";this.Results=[];this.m_values={};this.Parent=this.Failure=null;this.Add=function(c){if(null!=this.m_values[c])return this.m_values[c];var a=new n;a.Parent=this;a.Char=c;return this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);this.Results.push(c)}}function p(){this.End=!1;this.Results=[];this.m_values={};this.minflag=65535;this.maxflag=0;this.Add=function(c,a){this.minflag>c&&(this.minflag=c);this.maxflag<c&&(this.maxflag=c);this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);-1==this.Results.indexOf(c)&&this.Results.push(c)};this.HasKey=function(c){return void 0!=this.m_values[c]};this.TryGetValue=function(c){return this.minflag<=c&&this.maxflag>=c?this.m_values[c]:null}}var h=[],m=[];this.SetKeywords=function(c){m=c;c=new n;for(var a={},d=0;d<m.length;d++){for(var e=m[d],b=c,f=0;f<e.length;f++)b=b.Add(e.charCodeAt(f)),0==b.Layer&&(b.Layer=f+1,a[b.Layer]||(a[b.Layer]=[]),a[b.Layer].push(b));b.SetResults(d)}e=[];e.push(c);for(var g in a)for(b=a[g],d=0;d<b.length;d++)e.push(b[d]);for(d=1;d<e.length;d++){b=e[d];b.Index=d;a=b.Parent.Failure;for(f=b.Char;null!=a&&!a.m_values[f];)a=a.Failure;if(null==a)b.Failure=c;else{b.Failure=a.m_values[f];for(var k in b.Failure.Results)0!=b.Failure.Results.hasOwnProperty(k)&&b.SetResults(b.Failure.Results[k])}}c.Failure=c;k=[];for(d=0;d<e.length;d++)k.push(new p);for(d=0;d<k.length;d++){b=e[d];a=k[d];for(g in b.m_values)0!=b.m_values.hasOwnProperty(g)&&(f=b.m_values[g].Index,a.Add(g,k[f]));for(var l=0;l<b.Results.length;l++)f=b.Results[l],a.SetResults(f);for(b=b.Failure;b!=c;){for(g in b.m_values)0!=b.m_values.hasOwnProperty(g)&&0==a.HasKey(g)&&(f=b.m_values[g].Index,a.Add(g,k[f]));for(l=0;l<b.Results.length;l++)f=b.Results[l],a.SetResults(f);b=b.Failure}}h=k[0]};this.FindFirst=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=h.TryGetValue(e):(a=a.TryGetValue(e))||(a=h.TryGetValue(e));if(null!=a&&a.End)return m[a.Results[0]]}return null};this.FindAll=function(c){for(var a=null,d=[],e=0;e<c.length;e++){var b=c.charCodeAt(e);null==a?a=h.TryGetValue(b):(a=a.TryGetValue(b))||(a=h.TryGetValue(b));if(null!=a&&a.End)for(b=0;b<a.Results.length;b++)d.push(m[a.Results[b]])}return d};this.ContainsAny=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=h.TryGetValue(e):(a=a.TryGetValue(e))||(a=h.TryGetValue(e));if(null!=a&&a.End)return!0}return!1};this.Replace=function(c,a){void 0==a&&(a="*");for(var d=c.split(""),e=null,b=0;b<c.length;b++){var f=c.charCodeAt(b);null==e?e=h.TryGetValue(f):(e=e.TryGetValue(f))||(e=h.TryGetValue(f));if(null!=e&&e.End)for(f=b+1-m[e.Results[0]].length;f<=b;f++)d[f]=a}return d.join("")}};
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+38 -29
View File
@@ -1,6 +1,9 @@
// ToolGood.Words.WordsSearch.js
// 2020, Lin Zhijun, https://github.com/toolgood/ToolGood.Words
// Licensed under the Apache License 2.0
// 更新日志
// 2020.05.17 修改,支持大于0xffff的字符
function WordsSearch() {
function TrieNode() {
this.Index = 0;
@@ -38,6 +41,9 @@ function WordsSearch() {
this.maxflag = 0;
this.Add = function (c, node3) {
if (typeof c !== 'number') {
c = parseInt(c);
}
if (this.minflag > c) { this.minflag = c; }
if (this.maxflag < c) { this.maxflag = c; }
this.m_values[c] = node3;
@@ -61,7 +67,7 @@ function WordsSearch() {
}
}
var _first = [];
var _first = {};
var _keywords = [];
var _indexs = [];
/**
@@ -118,7 +124,7 @@ function WordsSearch() {
nd.Failure = root;
else {
nd.Failure = r.m_values[c];
for (const key2 in nd.Failure.Results) {
for (var key2 in nd.Failure.Results) {
if (nd.Failure.Results.hasOwnProperty(key2) == false) { continue; }
var result = nd.Failure.Results[key2];
nd.SetResults(result);
@@ -136,19 +142,19 @@ function WordsSearch() {
var oldNode = allNode[i];
var newNode = allNode2[i];
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
var index = oldNode.m_values[key].Index;
newNode.Add(key, allNode2[index]);
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
while (oldNode != root) {
for (const key in oldNode.m_values) {
for (var key in oldNode.m_values) {
if (oldNode.m_values.hasOwnProperty(key) == false) { continue; }
if (newNode.HasKey(key) == false) {
var index = oldNode.m_values[key].Index;
@@ -156,7 +162,7 @@ function WordsSearch() {
}
}
for (let index = 0; index < oldNode.Results.length; index++) {
const item = oldNode.Results[index];
var item = oldNode.Results[index];
newNode.SetResults(item);
}
oldNode = oldNode.Failure;
@@ -165,15 +171,15 @@ function WordsSearch() {
allNode = null;
root = null;
var first = [];
for (let index = 0; index < 0xffff; index++) {
first.push(null);
}
for (const key in allNode2[0].m_values) {
if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
first[key] = allNode2[0].m_values[key];
}
_first = first;
// var first = [];
// for (let index = 0; index < 0xffff; index++) {
// first.push(null);
// }
// for (var key in allNode2[0].m_values) {
// if (allNode2[0].m_values.hasOwnProperty(key) == false) { continue; }
// first[key] = allNode2[0].m_values[key];
// }
_first = allNode2[0];
}
/**
@@ -183,14 +189,14 @@ function WordsSearch() {
this.FindFirst = function (text) {
var ptr = null;
for (let i = 0; i < text.length; i++) {
const t = text.charCodeAt(i);
var t = text.charCodeAt(i);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
@@ -220,20 +226,20 @@ function WordsSearch() {
var list = [];
for (let i = 0; i < text.length; i++) {
const t = text.charCodeAt(i);
var t = text.charCodeAt(i);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
if (tn.End) {
for (let j = 0; j < tn.Results.length; j++) {
const item = tn.Results[j];
var item = tn.Results[j];
var keyword = _keywords[item];
list.push({
Keyword: keyword,
@@ -257,14 +263,14 @@ function WordsSearch() {
this.ContainsAny = function (text) {
var ptr = null;
for (let index = 0; index < text.length; index++) {
const t = text.charCodeAt(index);
var t = text.charCodeAt(index);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
@@ -282,20 +288,23 @@ function WordsSearch() {
* @param {any} text
* @param {any} replaceChar
*/
this.Replace = function (text, replaceChar = '*') {
this.Replace = function (text, replaceChar) {
if (replaceChar == undefined) {
replaceChar = '*'
}
var result = text.split('');
var ptr = null;
for (var i = 0; i < text.length; i++) {
const t = text.charCodeAt(i);
var t = text.charCodeAt(i);
var tn = null;
if (ptr == null) {
tn = _first[t];
tn = _first.TryGetValue(t);
} else {
tn = ptr.TryGetValue(t);
if (!tn) {
tn = _first[t];
tn = _first.TryGetValue(t);
}
}
if (tn != null) {
+3 -1
View File
@@ -1,4 +1,6 @@
// ToolGood.Words.WordsSearch.js
// 2020, Lin Zhijun, https://github.com/toolgood/ToolGood.Words
// Licensed under the Apache License 2.0
function WordsSearch(){function r(){this.Layer=this.Index=0;this.End=!1;this.Char="";this.Results=[];this.m_values={};this.Parent=this.Failure=null;this.Add=function(b){if(null!=this.m_values[b])return this.m_values[b];var a=new r;a.Parent=this;a.Char=b;return this.m_values[b]=a};this.SetResults=function(b){0==this.End&&(this.End=!0);this.Results.push(b)}}function t(){this.End=!1;this.Results=[];this.m_values={};this.minflag=65535;this.maxflag=0;this.Add=function(b,a){this.minflag>b&&(this.minflag=b);this.maxflag<b&&(this.maxflag=b);this.m_values[b]=a};this.SetResults=function(b){0==this.End&&(this.End=!0);-1==this.Results.indexOf(b)&&this.Results.push(b)};this.HasKey=function(b){return void 0!=this.m_values[b]};this.TryGetValue=function(b){return this.minflag<=b&&this.maxflag>=b?this.m_values[b]:null}}var k=[],l=[],p=[];this.SetKeywords=function(b){l=b;for(var a=0;a<b.length;a++)p.push(a);b=new r;for(var d={},a=0;a<l.length;a++){for(var e=l[a],c=b,g=0;g<e.length;g++)c=c.Add(e.charCodeAt(g)),0==c.Layer&&(c.Layer=g+1,d[c.Layer]||(d[c.Layer]=[]),d[c.Layer].push(c));c.SetResults(a)}e=[];e.push(b);for(var f in d)for(c=d[f],a=0;a<c.length;a++)e.push(c[a]);for(a=1;a<e.length;a++){c=e[a];c.Index=a;f=c.Parent.Failure;for(d=c.Char;null!=f&&!f.m_values[d];)f=f.Failure;if(null==f)c.Failure=b;else{c.Failure=f.m_values[d];for(var h in c.Failure.Results)0!=c.Failure.Results.hasOwnProperty(h)&&c.SetResults(c.Failure.Results[h])}}b.Failure=b;h=[];for(a=0;a<e.length;a++)h.push(new t);for(a=0;a<h.length;a++){f=e[a];var c=h[a],m;for(m in f.m_values)0!=f.m_values.hasOwnProperty(m)&&(d=f.m_values[m].Index,c.Add(m,h[d]));for(d=0;d<f.Results.length;d++)c.SetResults(f.Results[d]);for(f=f.Failure;f!=b;){for(var n in f.m_values)0!=f.m_values.hasOwnProperty(n)&&0==c.HasKey(n)&&(d=f.m_values[n].Index,c.Add(n,h[d]));for(d=0;d<f.Results.length;d++)c.SetResults(f.Results[d]);f=f.Failure}}m=[];for(n=0;65535>n;n++)m.push(null);for(var q in h[0].m_values)0!=h[0].m_values.hasOwnProperty(q)&&(m[q]=h[0].m_values[q]);k=m};this.FindFirst=function(b){for(var a=null,d=0;d<b.length;d++){var e=b.charCodeAt(d);null==a?a=k[e]:(a=a.TryGetValue(e))||(a=k[e]);if(null!=a&&a.End)return b=a.Results[0],{Keyword:l[b],Success:!0,End:d,Start:d+1-l[b].length,Index:p[b]}}return null};this.FindAll=function(b){for(var a=null,d=[],e=0;e<b.length;e++){var c=b.charCodeAt(e);null==a?a=k[c]:(a=a.TryGetValue(c))||(a=k[c]);if(null!=a&&a.End)for(c=0;c<a.Results.length;c++){var g=a.Results[c];d.push({Keyword:l[g],Success:!0,End:e,Start:e+1-l[g].length,Index:p[g]})}}return d};this.ContainsAny=function(b){for(var a=null,d=0;d<b.length;d++){var e=b.charCodeAt(d);null==a?a=k[e]:(a=a.TryGetValue(e))||(a=k[e]);if(null!=a&&a.End)return!0}return!1};this.Replace=function(b,a){a=void 0===a?"*":a;for(var d=b.split(""),e=null,c=0;c<b.length;c++){var g=b.charCodeAt(c);null==e?e=k[g]:(e=e.TryGetValue(g))||(e=k[g]);if(null!=e&&e.End)for(g=c+1-l[e.Results[0]].length;g<=c;g++)d[g]=a}return d.join("")}};
// 更新日志
// 2020.05.17 修改,支持大于0xffff的字符
function WordsSearch(){function p(){this.Layer=this.Index=0;this.End=!1;this.Char="";this.Results=[];this.m_values={};this.Parent=this.Failure=null;this.Add=function(c){if(null!=this.m_values[c])return this.m_values[c];var a=new p;a.Parent=this;a.Char=c;return this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);this.Results.push(c)}}function q(){this.End=!1;this.Results=[];this.m_values={};this.minflag=65535;this.maxflag=0;this.Add=function(c,a){"number"!==typeof c&&(c=parseInt(c));this.minflag>c&&(this.minflag=c);this.maxflag<c&&(this.maxflag=c);this.m_values[c]=a};this.SetResults=function(c){0==this.End&&(this.End=!0);-1==this.Results.indexOf(c)&&this.Results.push(c)};this.HasKey=function(c){return void 0!=this.m_values[c]};this.TryGetValue=function(c){return this.minflag<=c&&this.maxflag>=c?this.m_values[c]:null}}var h={},l=[],n=[];this.SetKeywords=function(c){l=c;for(var a=0;a<c.length;a++)n.push(a);c=new p;for(var d={},a=0;a<l.length;a++){for(var e=l[a],b=c,f=0;f<e.length;f++)b=b.Add(e.charCodeAt(f)),0==b.Layer&&(b.Layer=f+1,d[b.Layer]||(d[b.Layer]=[]),d[b.Layer].push(b));b.SetResults(a)}e=[];e.push(c);for(var g in d)for(b=d[g],a=0;a<b.length;a++)e.push(b[a]);for(a=1;a<e.length;a++){b=e[a];b.Index=a;d=b.Parent.Failure;for(f=b.Char;null!=d&&!d.m_values[f];)d=d.Failure;if(null==d)b.Failure=c;else{b.Failure=d.m_values[f];for(var k in b.Failure.Results)0!=b.Failure.Results.hasOwnProperty(k)&&b.SetResults(b.Failure.Results[k])}}c.Failure=c;k=[];for(a=0;a<e.length;a++)k.push(new q);for(a=0;a<k.length;a++){b=e[a];d=k[a];for(g in b.m_values)0!=b.m_values.hasOwnProperty(g)&&(f=b.m_values[g].Index,d.Add(g,k[f]));for(var m=0;m<b.Results.length;m++)f=b.Results[m],d.SetResults(f);for(b=b.Failure;b!=c;){for(g in b.m_values)0!=b.m_values.hasOwnProperty(g)&&0==d.HasKey(g)&&(f=b.m_values[g].Index,d.Add(g,k[f]));for(m=0;m<b.Results.length;m++)f=b.Results[m],d.SetResults(f);b=b.Failure}}h=k[0]};this.FindFirst=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=h.TryGetValue(e):(a=a.TryGetValue(e))||(a=h.TryGetValue(e));if(null!=a&&a.End)return c=a.Results[0],{Keyword:l[c],Success:!0,End:d,Start:d+1-l[c].length,Index:n[c]}}return null};this.FindAll=function(c){for(var a=null,d=[],e=0;e<c.length;e++){var b=c.charCodeAt(e);null==a?a=h.TryGetValue(b):(a=a.TryGetValue(b))||(a=h.TryGetValue(b));if(null!=a&&a.End)for(b=0;b<a.Results.length;b++){var f=a.Results[b];d.push({Keyword:l[f],Success:!0,End:e,Start:e+1-l[f].length,Index:n[f]})}}return d};this.ContainsAny=function(c){for(var a=null,d=0;d<c.length;d++){var e=c.charCodeAt(d);null==a?a=h.TryGetValue(e):(a=a.TryGetValue(e))||(a=h.TryGetValue(e));if(null!=a&&a.End)return!0}return!1};this.Replace=function(c,a){void 0==a&&(a="*");for(var d=c.split(""),e=null,b=0;b<c.length;b++){var f=c.charCodeAt(b);null==e?e=h.TryGetValue(f):(e=e.TryGetValue(f))||(e=h.TryGetValue(f));if(null!=e&&e.End)for(f=b+1-l[e.Results[0]].length;f<=b;f++)d[f]=a}return d.join("")}};
+39 -39
View File
@@ -15,7 +15,7 @@
</div>
<script src="ToolGood.Words.StringSearch.js"></script>
<script src="ToolGood.Words.WordsSearch.js"></script>
<script src="ToolGood.Words.PinYin.js"></script>
<script src="ToolGood.Words.Pinyin.js"></script>
<script src="ToolGood.Words.Translate.js"></script>
<script>
function log(txt) {
@@ -111,79 +111,79 @@
}
log("----------------------------------- PinYin -----------------------------------");
var pinyin = new PinYin();
log("----------------------------------- Pinyin -----------------------------------");
var pinyin = new Pinyin();
log("PinYin GetPinYinForName is run.");
py = pinyin.GetPinYinForName("单一一");
log("Pinyin GetPinyinForName is run.");
py = pinyin.GetPinyinForName("单一一");
if (py != "ShanYiYi") {
log("PinYin GetPinYinForName is error.............................");
log("Pinyin GetPinyinForName is error.............................");
}
log("PinYin GetPinYinForName is run.");
py = pinyin.GetPinYinForName("单一一", true);
log("Pinyin GetPinyinForName is run.");
py = pinyin.GetPinyinForName("单一一", true);
if (py != "ShànYīYī") {
log("PinYin GetPinYinForName is error.............................");
log("Pinyin GetPinyinForName is error.............................");
}
log("PinYin GetAllPinYin is run.");
var t = pinyin.GetAllPinYin('芃');
log("Pinyin GetAllPinyin is run.");
var t = pinyin.GetAllPinyin('芃');
if (t[0] != "Peng") {
log("PinYin GetAllPinYin is error.............................");
log("Pinyin GetAllPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
var b = pinyin.GetPinYin("摩擦棒");
log("Pinyin GetPinyin is run.");
var b = pinyin.GetPinyin("摩擦棒");
if (b != "MoCaBang") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
b = pinyin.GetPinYin("秘鲁");
log("Pinyin GetPinyin is run.");
b = pinyin.GetPinyin("秘鲁");
if (b != "BiLu") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
py = pinyin.GetPinYin("快乐,乐清");
log("Pinyin GetPinyin is run.");
py = pinyin.GetPinyin("快乐,乐清");
if (py != "KuaiLe,YueQing") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
py = pinyin.GetPinYin("快乐清理");
log("Pinyin GetPinyin is run.");
py = pinyin.GetPinyin("快乐清理");
if (py != "KuaiLeQingLi") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
py = pinyin.GetPinYin("我爱中国");
log("Pinyin GetPinyin is run.");
py = pinyin.GetPinyin("我爱中国");
if (py != "WoAiZhongGuo") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetPinYin is run.");
py = pinyin.GetPinYin("我爱中国", true);
log("Pinyin GetPinyin is run.");
py = pinyin.GetPinyin("我爱中国", true);
if (py != "WǒÀiZhōngGuó") {
log("PinYin GetPinYin is error.............................");
log("Pinyin GetPinyin is error.............................");
}
log("PinYin GetFirstPinYin is run.");
py = pinyin.GetFirstPinYin("我爱中国");
log("Pinyin GetFirstPinyin is run.");
py = pinyin.GetFirstPinyin("我爱中国");
if (py != "WAZG") {
log("PinYin GetFirstPinYin is error.............................");
log("Pinyin GetFirstPinyin is error.............................");
}
log("PinYin GetAllPinYin is run.");
var pys = pinyin.GetAllPinYin('传');
log("Pinyin GetAllPinyin is run.");
var pys = pinyin.GetAllPinyin('传');
if (pys[0] != "Chuan") {
log("PinYin GetAllPinYin is error.............................");
log("Pinyin GetAllPinyin is error.............................");
}
if (pys[1] != "Zhuan") {
log("PinYin GetAllPinYin is error.............................");
log("Pinyin GetAllPinyin is error.............................");
}
log("----------------------------------- Translate -----------------------------------");
var translate = new Translate();
@@ -197,13 +197,13 @@
log("Translate ToTraditionalChinese is run.");
var tw = translate.ToTraditionalChinese("我爱中国");
if (tw != "我愛中國") {
log("Translate ToTraditionalChinese is error.............................");
log("Translate 我爱中国 ToTraditionalChinese is error.............................");
}
log("Translate ToTraditionalChinese is run.");
var tw = translate.ToTraditionalChinese("原代码11", 2);
if (tw != "原始碼11") {
log("Translate ToTraditionalChinese is error.............................");
log("Translate 原代码11 ToTraditionalChinese is error.............................");
}
log("Translate ToTraditionalChinese is run.");