Skip to content

Commit

Permalink
Merge v2.3.3 from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Jul 29, 2014
2 parents 54678fb + 945d5e1 commit 91e8508
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 61 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ via npm:
npm install pinyin
```

via spm@3.x:
via spm3:

```bash
spm install pinyin
Expand Down Expand Up @@ -158,6 +158,7 @@ API 和使用方式完成一致。
## 参考

* [在线汉语字典](http://zi.artx.cn/zi/)
* [汉典网](http://www.zdic.net/)
* [快典网](http://py.kdd.cc/)
* [将汉字转换成拼音](https://code.google.com/p/chinese-character-2-pinyin/)
* [字符转拼音 javascript pinyin](http://www.cnblogs.com/jinweijie/archive/2008/02/03/1063289.html)
Expand Down
4 changes: 2 additions & 2 deletions bin/pinyin
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node

var commander = require('commander');
//require('colors');
var pinyin = require("../src/pinyin");

commander.
version(require('../package').version).
usage('[options] 汉字').
option('-v, --version', 'output the version number').
option('-s, --style <style>', 'pinyin styles').
//option('-h, --heteronym', 'output heteronym pinyins').
option('-h, --heteronym', 'output heteronym pinyins').
parse(process.argv);

if (commander.list) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pinyin",
"version": "2.3.2",
"version": "2.3.3",
"description": "汉语拼音转换工具。",
"keywords": ["拼音", "Pinyin"],
"homepage": "http://pinyin.hotoo.me/",
Expand Down
2 changes: 1 addition & 1 deletion src/phrases-dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ module.exports = {
"朝廷": [["cháo"], ["tíng"]],
"朝夕": [["zhāo"], ["xī"]],
"朝霞": [["zhāo"], ["xiá"]],
"朝阳": [["zhāo"], ["yáng"]],
"朝阳": [["zhāo", "cháo"], ["yáng"]],
"朝向": [["cháo"], ["xiàng"]],
"朝野": [["cháo"], ["yě"]],
"朝政": [["cháo"], ["zhèng"]],
Expand Down
70 changes: 34 additions & 36 deletions src/pinyin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ if(isNode){
PINYIN_DICT = module["require"]("./dict-zi");
}else{
PINYIN_DICT = buildPinyinCache(require("./dict-zi-web"));
console.log(PINYIN_DICT)
}


Expand Down Expand Up @@ -74,6 +73,8 @@ var DEFAULT_OPTIONS = {


// 将 more 的属性值,覆盖 origin 中已有的属性。
// @param {Object} origin.
// @param {Object} more.
// @return 返回新的对象。
function extend(origin, more){
var obj = {};
Expand All @@ -87,12 +88,10 @@ function extend(origin, more){
return obj;
}

/**
* 修改拼音词库表中的格式。
* @param {String} pinyin, 单个拼音。
* @param {PINYIN_STYLE} style, 拼音风格。
* @return {String}
*/
// 修改拼音词库表中的格式。
// @param {String} pinyin, 单个拼音。
// @param {PINYIN_STYLE} style, 拼音风格。
// @return {String}
function toFixed(pinyin, style){
var tone = ""; // 声调。
switch(style){
Expand Down Expand Up @@ -126,11 +125,9 @@ function toFixed(pinyin, style){
}
}

/**
* 单字拼音转换。
* @param {String} han, 单个汉字
* @return {Array} 返回拼音列表,多音字会有多个拼音项。
*/
// 单字拼音转换。
// @param {String} han, 单个汉字
// @return {Array} 返回拼音列表,多音字会有多个拼音项。
function single_pinyin(han, options){

if("string" !== typeof han){return [];}
Expand Down Expand Up @@ -160,19 +157,23 @@ function single_pinyin(han, options){
return pinyins;
}

/**
* 词语注音
* @param {String} phrases, 指定的词组。
* @param {Object} options, 选项。
* @return {Array}
*/
// 词语注音
// @param {String} phrases, 指定的词组。
// @param {Object} options, 选项。
// @return {Array}
function phrases_pinyin(phrases, options){
var py = [];
if(PHRASES_DICT.hasOwnProperty(phrases)){
//! copy pinyin result.
py = PHRASES_DICT[phrases].slice();
py.forEach(function(item, idx, arr){
arr[idx] = [toFixed(item[0], options.style)];
PHRASES_DICT[phrases].forEach(function(item, idx){
py[idx] = [];
if (options.heteronym){
item.forEach(function(py_item, py_index){
py[idx][py_index] = toFixed(py_item, options.style);
});
} else {
py[idx][0] = toFixed(item[0], options.style);
}
});
}else{
for(var i=0,l=phrases.length; i<l; i++){
Expand All @@ -182,11 +183,9 @@ function phrases_pinyin(phrases, options){
return py;
}

/**
* @param {String} hans 要转为拼音的目标字符串(汉字)。
* @param {Object} options, 可选,用于指定拼音风格,是否启用多音字。
* @return {Array} 返回的拼音列表。
*/
// @param {String} hans 要转为拼音的目标字符串(汉字)。
// @param {Object} options, 可选,用于指定拼音风格,是否启用多音字。
// @return {Array} 返回的拼音列表。
function pinyin(hans, options){

if("string" !== typeof hans){return [];}
Expand Down Expand Up @@ -230,11 +229,9 @@ function pinyin(hans, options){
}


/**
* 声母(Initials)、韵母(Finals)。
* @param {String/Number/RegExp/Date/Function/Array/Object}
* @return {String/Number/RegExp/Date/Function/Array/Object}
*/
// 格式化为声母(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){
Expand All @@ -244,9 +241,10 @@ function initials(pinyin){
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;

module.exports = pinyin;
module.exports.STYLE_NORMAL = PINYIN_STYLE.NORMAL;
module.exports.STYLE_TONE = PINYIN_STYLE.TONE;
module.exports.STYLE_TONE2 = PINYIN_STYLE.TONE2;
module.exports.STYLE_INITIALS = PINYIN_STYLE.INITIALS;
module.exports.STYLE_FIRST_LETTER = PINYIN_STYLE.FIRST_LETTER;
23 changes: 3 additions & 20 deletions tests/phrases_test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
/**
* @overview
*
* @author 闲耘™ (hotoo.cn[AT]gmail.com)
* @version 2013/02/02
*/

var pinyin = require("../src/pinyin.js");
var S = require("simplebig")
var pinyin = require("../src/pinyin");

var hans = [
"中心思想",
"中弹受伤",
"重心不稳",
"影碟重重",
"冒顿单于",
"这个没有",
"狗出没注意",
"惊天地泣鬼神"
"中国(china)",
];

for(var i=0,l=hans.length; i<l; i++){
console.log(hans[i], pinyin(hans[i]));
console.log(hans[i], pinyin(hans[i], {heteronym:true}));
}

console.log(S.t2s("乳臭未乾"));
console.log(S.t2s("旁徵博引"));
2 changes: 2 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,5 @@ function deepEquals(a, b){
break;
}
}

console.log("DEEP EQUALS:", deepEquals([["zhong"],["guo"]] , [["zhong"],["guo"]]))

0 comments on commit 91e8508

Please sign in to comment.