-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhanyupinyin.js
262 lines (206 loc) · 4.91 KB
/
hanyupinyin.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* hanyupinyin.js, JavaScript Hanyu Pinyin object
* - Pinyin Tone mark conversion
* - Hanzi to Pinyin conversion
* - Pinyin Tone removal
*
* @version 1.1
* @license The Unlicense, http://unlicense.org/
* @author The Pffy Authors, https://github.com/pffy/
* @updated 2014-06-05
* @link https://github.com/pffy/javascript-hanyupinyin
*
*/
var HanyuPinyin = function () {
var _hpjson = 'idx/json/IdxHanyuPinyin.json',
_tmjson = 'idx/json/IdxToneMarks.json',
_tnjson = 'idx/json/IdxToneNumbers.json',
_toneMode = 1,
_ready = false,
_hpdx = {},
_tmdx = {},
_tndx = {},
_output = '',
_input = '';
// starts up
_ready = init();
// converts to tone marks
function convertToToneMarks () {
convert(_tmdx);
}
// converts to tone numbers
function convertToToneNumbers () {
convert(_tndx);
atomize();
}
// converts hanzi or pinyin to pinyin
function convertToHanyuPinyin() {
convert(_hpdx);
atomize();
}
// zhong1wen2 -> zhong1 wen2
// adds exactly one space between pinyin units
function atomize() {
// TODO regex may cost less and be more accurate. find out.
// https://github.com/pffy/idioms#atomize
for(var i = 1; i < 6; i++) {
_output = replaceAll('' + i,'' + i + ' ', _output);
}
vacuum();
}
// vac uum -> vac uum --> vac uum
// reduces many spaces to exactly one space
function vacuum() {
// TODO regex may cost less. find out.
// https://github.com/pffy/idioms#vacuum
_output = replaceAll(' ', ' ', _output);
}
// normalizes umlaut
function normalizeUmlaut() {
_output = replaceAll('ü', 'uu', _output);
_output = replaceAll('u:', 'uu', _output);
}
// remove tone numbers
function removeToneNumbers() {
convertToToneNumbers();
for(var i = 1; i < 6; i++) {
_output = replaceAll(i + " ", " ", _output);
}
}
// replaceAll(find, replace, str)
// Found at http://stackoverflow.com/questions/1144783
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'gi'), replace);
}
// converts using idx key-value pairs
function convert(idx) {
for(var n in idx) {
_output = replaceAll(n, idx[n], _output);
}
}
// loads idx objects
function init() {
var loaded = false;
var req = new XMLHttpRequest();
// loads hpdx
req.open('GET', _hpjson, false);
req.overrideMimeType('text/javascript; charset=utf-8');
req.onload = function(e) {
try {
_hpdx = JSON.parse(req.responseText);
} catch (err) {
console.log(e);
}
};
req.send();
if ( !_hpdx.length) {
return false;
}
// loads tmdx
req.open('GET', _tmjson, false);
req.onload = function(e) {
try {
_tmdx = JSON.parse(req.responseText);
} catch (err) {
console.log(e);
}
};
req.send();
if ( !_tmdx.length) {
return false;
}
// loads tndx
req.open('GET', _tnjson, false);
req.onload = function(e) {
try {
_tndx = JSON.parse(req.responseText);
} catch (err) {
console.log(e);
}
};
req.send();
if ( !_tndx.length) {
return false;
}
return true;
}
return {
/**
* toString() Returns output text
*
* @return string output
*/
toString: function() {
return _output.toString();
},
/**
* setInput() Sets input text
* Returns this object
*
* @param string inputPinyin
* @return HanyuPinyin object
*/
setInput: function (inputString) {
_input = _output = inputString;
// normalize umlaut
normalizeUmlaut();
// convert hanzi to pinyin
convertToHanyuPinyin();
// formats by pinyin display mode
switch(_toneMode)
{
case 2:
convertToToneMarks();
break;
case 3:
removeToneNumbers();
break;
default:
convertToToneNumbers();
break;
}
return this;
},
/**
* getInput() Returns input text
*
* @return string input
*/
getInput: function () {
return _input;
},
/**
* getMode() Returns tone display mode
*
* @return integer toneMode
*/
getMode: function() {
return _toneMode;
},
/**
* setMode() Sets tone display mode
* 2: tone marks
* 3: no tones
* otherwise: tone numbers
*
* Returns this object
*
* @param integer toneInteger
* @return HanyuPinyin object
*/
setMode: function(toneInteger) {
_toneMode = toneInteger;
return this.setInput(this.getInput());
},
/**
* isReady() Returns status of pinyin idx loading
*
* @return boolean object
*/
isReady: function() {
return _ready;
}
};
};
// wait, was it one newline ... or two?
// https://github.com/airbnb/javascript/pull/170