| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 |
1
1
1
1
1
1
1
27
1
1
1
190
190
38
38
38
7
38
38
37
38
37
37
38
38
1
150
150
150
150
150
150
75
75
75
75
115
115
21
94
94
75
1
120
120
120
120
360
360
360
150
150
210
120
50
50
120
1
38
627
24
14
1
1
1
1
1
1
1
1
1
| "use strict";
var PINYIN_DICT = {};
var utils = require("./utils");
var PINYIN_STYLE = utils.PINYIN_STYLE;
// 声母表。
var INITIALS = "b,p,m,f,d,t,n,l,g,k,h,j,q,x,r,zh,ch,sh,z,c,s".split(",");
// 韵母表。
//var FINALS = "ang,eng,ing,ong,an,en,in,un,er,ai,ei,ui,ao,ou,iu,ie,ve,a,o,e,i,u,v".split(",");
// 带音标字符。
var PHONETIC_SYMBOL = require("./phonetic-symbol.js");
var re_phonetic_symbol_source = "";
for(var k in PHONETIC_SYMBOL){
re_phonetic_symbol_source += k;
}
var RE_PHONETIC_SYMBOL = new RegExp("([" + re_phonetic_symbol_source + "])", "g");
var RE_TONE2 = /([aeoiuvnm])([0-4])$/;
// 修改拼音词库表中的格式。
// @param {String} pinyin, 单个拼音。
// @param {PINYIN_STYLE} style, 拼音风格。
// @return {String}
function toFixed(pinyin, style){
var tone = ""; // 声调。
switch(style){
case PINYIN_STYLE.INITIALS:
return initials(pinyin);
case PINYIN_STYLE.FIRST_LETTER:
var first_letter = pinyin.charAt(0);
if(PHONETIC_SYMBOL.hasOwnProperty(first_letter)){
first_letter = PHONETIC_SYMBOL[first_letter].charAt(0);
}
return first_letter;
case PINYIN_STYLE.NORMAL:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0, $1_phonetic){
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1");
});
case PINYIN_STYLE.TONE2:
var py = pinyin.replace(RE_PHONETIC_SYMBOL, function($0, $1){
// 声调数值。
tone = PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$2");
return PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$1");
});
return py + tone;
case PINYIN_STYLE.TONE:
default:
return pinyin;
}
}
// 单字拼音转换。
// @param {String} han, 单个汉字
// @return {Array} 返回拼音列表,多音字会有多个拼音项。
function single_pinyin(han, options){
Iif(typeof han !== "string"){
return [];
}
Iif(han.length !== 1){
return single_pinyin(han.charAt(0), options);
}
var hanCode = han.charCodeAt(0);
Iif(!PINYIN_DICT[hanCode]){
return [han];
}
var pys = PINYIN_DICT[hanCode].split(",");
if(!options.heteronym){
return [toFixed(pys[0], options.style)];
}
// 临时存储已存在的拼音,避免多音字拼音转换为非注音风格出现重复。
var py_cached = {};
var pinyins = [];
for(var i = 0, py, l = pys.length; i < l; i++){
py = toFixed(pys[i], options.style);
if(py_cached.hasOwnProperty(py)){
continue;
}
py_cached[py] = py;
pinyins.push(py);
}
return pinyins;
}
// @param {String} hans 要转为拼音的目标字符串(汉字)。
// @param {Object} options, 可选,用于指定拼音风格,是否启用多音字。
// @return {Array} 返回的拼音列表。
function pinyin(hans, options){
Iif(typeof hans !== "string"){
return [];
}
options = utils.extend(utils.DEFAULT_OPTIONS, options || {});
var pys = [];
for(var i = 0, nohans = "", firstCharCode, words, l = hans.length; i < l; i++){
words = hans[i];
firstCharCode = words.charCodeAt(0);
if(PINYIN_DICT[firstCharCode]){
// ends of non-chinese words.
Iif(nohans.length > 0){
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
pys.push(single_pinyin(words, options));
}else{
nohans += words;
}
}
// 清理最后的非中文字符串。
if(nohans.length > 0){
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
return pys;
}
// 格式化为声母(Initials)、韵母(Finals)。
// @param {String}
// @return {String}
function initials(pinyin) {
for (var i = 0, l = INITIALS.length; i < l; i++){
if (pinyin.indexOf(INITIALS[i]) === 0) {
return INITIALS[i];
}
}
return "";
}
pinyin.STYLE_NORMAL = PINYIN_STYLE.NORMAL;
pinyin.STYLE_TONE = PINYIN_STYLE.TONE;
pinyin.STYLE_TONE2 = PINYIN_STYLE.TONE2;
pinyin.STYLE_INITIALS = PINYIN_STYLE.INITIALS;
pinyin.STYLE_FIRST_LETTER = PINYIN_STYLE.FIRST_LETTER;
pinyin.toFixed = toFixed;
pinyin.setDict = function(dict) {
PINYIN_DICT = dict;
};
module.exports = pinyin;
|