switch-chinese 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/stcasc.lib.js +79 -73
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "switch-chinese",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for
|
|
3
|
+
"version": "1.0.10",
|
|
4
|
+
"description": "簡繁轉換,支援簡繁雙向轉換、智慧分詞、自訂詞庫、文字偵測及多種輸出格式,零依賴。 Lightweight Chinese converter library for conversion between Simplified and Traditional Chinese. 轻量级简繁体中文智能转换库,支持简繁双向转换、智能分词、自定义词库、文本检测及多种输出格式,零依赖。",
|
|
5
5
|
"main": "stcasc.lib.js",
|
|
6
6
|
"types": "stcasc.d.ts",
|
|
7
7
|
"type": "module",
|
package/stcasc.lib.js
CHANGED
|
@@ -837,84 +837,90 @@ function detect(text) {
|
|
|
837
837
|
}
|
|
838
838
|
|
|
839
839
|
function stcasc(cache, custom, disableTerms) {
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
840
|
+
if (!cache) cache = {};
|
|
841
|
+
if (cache.sc2tcCombTree && cache.tc2scCombTree) {
|
|
842
|
+
sc2tcCombTree = cache.sc2tcCombTree;
|
|
843
843
|
tc2scCombTree = cache.tc2scCombTree;
|
|
844
|
-
|
|
844
|
+
} else {
|
|
845
845
|
if (disableTerms) sc2tcComb = {};
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
846
|
+
if (custom && custom.length) {
|
|
847
|
+
for (let sc in custom) {
|
|
848
|
+
sc2tcComb[sc] = custom[sc];
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function makeCombTree(key, value) {
|
|
852
|
+
let curTree = sc2tcCombTree;
|
|
853
|
+
for (let i = 0; i < key.length; i++) {
|
|
854
|
+
let curKey = key.charAt(i);
|
|
855
|
+
let branch = curTree[curKey];
|
|
856
|
+
let newTree = {};
|
|
857
|
+
if (i == key.length - 1) {
|
|
858
|
+
newTree = {"end": value};
|
|
859
|
+
if (branch) {
|
|
860
|
+
branch.end = value;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
if (branch) {
|
|
864
|
+
curTree = branch;
|
|
865
|
+
} else {
|
|
866
|
+
curTree[curKey] = newTree;
|
|
867
|
+
curTree = newTree;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
curTree = tc2scCombTree;
|
|
871
|
+
for (let i = 0; i < value.length; i++) {
|
|
872
|
+
let curKey = value.charAt(i);
|
|
873
|
+
let branch = curTree[curKey];
|
|
874
|
+
let newTree = {};
|
|
875
|
+
if (i == value.length - 1) {
|
|
876
|
+
newTree = {"end": key};
|
|
877
|
+
if (branch) {
|
|
878
|
+
branch.end = value;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
if (branch) {
|
|
882
|
+
curTree = branch;
|
|
883
|
+
} else {
|
|
884
|
+
curTree[curKey] = newTree;
|
|
885
|
+
curTree = newTree;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
for (let key in sc2tcComb) {
|
|
890
|
+
let value = sc2tcComb[key];
|
|
891
|
+
if (Array.isArray(value)) {
|
|
892
|
+
value.forEach(v => {
|
|
893
|
+
makeCombTree(key, v);
|
|
894
|
+
});
|
|
895
|
+
} else {
|
|
896
|
+
makeCombTree(key, value);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
cache.sc2tcCombTree = sc2tcCombTree;
|
|
894
900
|
cache.tc2scCombTree = tc2scCombTree;
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
901
|
+
}
|
|
902
|
+
if (cache.stDict && cache.tsDict) {
|
|
903
|
+
stDict = cache.stDict;
|
|
898
904
|
tsDict = cache.tsDict;
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
905
|
+
} else {
|
|
906
|
+
for (let i = 0; i < scStr.length; i++) {
|
|
907
|
+
let _sc = scStr[i];
|
|
908
|
+
let _tc = tcStr[i];
|
|
909
|
+
if (!stDict[_sc]) stDict[_sc] = _tc;
|
|
910
|
+
if (!tsDict[_tc]) tsDict[_tc] = _sc;
|
|
911
|
+
}
|
|
912
|
+
Object.keys(oc2tc).forEach(key => {
|
|
913
|
+
let ocList = oc2tc[key];
|
|
914
|
+
for (let i = 0; i < ocList.length; i++) {
|
|
915
|
+
let oc = ocList[i];
|
|
916
|
+
stDict[oc] = key;
|
|
917
|
+
tsDict[oc] = tsDict[key] || key;
|
|
918
|
+
}
|
|
919
|
+
})
|
|
920
|
+
cache.stDict = stDict;
|
|
915
921
|
cache.tsDict = tsDict;
|
|
916
|
-
|
|
917
|
-
|
|
922
|
+
}
|
|
923
|
+
return {simplized, traditionalized, detect, cache};
|
|
918
924
|
}
|
|
919
925
|
|
|
920
926
|
export default stcasc;
|