js how Chinese characters into pinyin

github address, above encapsulated conversion tool: https://github.com/sxei/pinyinjs

There are several libraries, according to the function, the library file size is not the same, you can go to the introduction of the use upon request.

Inside a good package method:

/ * * 
 * Get the first letter of alphabet characters 
 * @param str string of Chinese characters, if they are non-Chinese characters it is returned 
 * @param polyphone whether to support more than one pronunciation, default false, if true, will return an array of all possible combinations 
 * / 
pinyinUtil.getFirstLetter (str, polyphone); 
/ * * 
 * acquired according to pinyin of Chinese characters, Chinese characters, if not a direct return to the original character 
 * @param str to convert Chinese characters 
 * @param splitter separator character, default separated by a space 
 * @param withtone return whether the results include tone, the default is 
 whether * @param polyphone support more than one pronunciation, no default 
* / 
pinyinUtil.getPinyin (str, Splitter, withtone, polyphone); 
/ * * 
 * phonetic characters turn, supports only a single Chinese character, returns all matches Character composition 
 * @param pinyin single phonetic not contain tone 
 * / 
pinyinUtil.getHanzi (Pinyin);

The following are descriptions about how to use for different occasions.

If you only need to get the first letter of alphabet

<script type="text/javascript" src="pinyin_dict_firstletter.js"></script>
<script type="text/javascript" src="pinyinUtil.js"></script>

<script type="text/javascript">
pinyinUtil.getFirstLetter('小茗同学'); // 输出 XMTX
pinyinUtil.getFirstLetter('大中国', true); // 输出 ['DZG', 'TZG']
</script>

Of particular note is that if you introduce the other two dictionary files, they can also get the first letter of the alphabet, but said with the dictionary file is more suitable.

If you do not need to tone pinyin

<Script type = "text / JavaScript" the src = "pinyin_dict_notone.js"> </ Script> 
<Script type = "text / JavaScript" the src = "pinyinUtil.js"> </ Script> 

<Script type = "text / JavaScript "> 
pinyinUtil.getPinyin ( 'small Ming students'); // outputs 'Xiao Ming Xue Tong' 
pinyinUtil.getHanzi ( 'Ming'); // outputs 'command name Ming Ming Ming Ming Ming Ming Ming borer dark eyes open' 
</ script>

If you need to deal with uncommon words or tone

<script type="text/javascript" src="pinyin_dict_withtone.js"></script>
<script type="text/javascript" src="pinyinUtil.js"></script>

<script type="text/javascript">
pinyinUtil.getPinyin('小茗同学'); // 输出 'xiǎo míng tóng xué'
pinyinUtil.getPinyin('小茗同学', '-', true, true); // 输出 ['xiǎo-míng-tóng-xué', 'xiǎo-míng-tòng-xué']
</script>

If you need to accurately identify more than one pronunciation

Since the dictionary file is large, this example is not recommended for use in a web environment:

<Script type = "text / JavaScript" the src = "dict / pinyin_dict_withtone.js"> </ Script> 
<Script type = "text / JavaScript" the src = "dict / pinyin_dict_polyphone.js"> </ Script> 
<Script type = "text / JavaScript" the src = "pinyinUtil.js"> </ Script> 

<Script type = "text / JavaScript"> 
pinyinUtil.getPinyin ( 'Wall and grown up', '', to true , to true ); // output: chéng hé dà zhǎng cháng 
pinyinUtil.getPinyin ( 'drink and cheer', '', to true , to true ); // output: hē shuǐ hé hè cǎi 
pinyinUtil.getPinyin ( 'great doctor', '', to true ,to true ); // Output: wěi dà de dài fū 
</ Script>

About simple Pinyin input method

A formal input method need to consider too many things, such as thesaurus, enter the user's personal habits, here is the realization of a simple input method, there is no thesaurus (although you can also add, but web environment is not suitable the introduction of large files).

The second is recommended to use a dictionary file pinyin_dict_noletter.js, although the dictionary three more words, but not in accordance with the frequency of use of Chinese characters sort, but in front of some rare words.

<link rel="stylesheet" type="text/css" href="simple-input-method/simple-input-method.css">
<input type="text" class="test-input-method"/>
<script type="text/javascript" src="pinyin_dict_noletter.js"></script>
<script type="text/javascript" src="pinyinUtil.js"></script>
<script type="text/javascript" src="simple-input-method/simple-input-method.js"></script>
<script type="text/javascript">
    SimpleInputMethod.init('.test-input-method');
</script>

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/fqh123/p/12002069.html
Recommended