Skip to content

Commit

Permalink
A: bin/pinyin, fixed #9.
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Aug 9, 2013
1 parent 090722e commit 9d820ac
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

test:
@mocha -R spec tests/test.js
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@

# JavaScript 拼音

用 Javascript 将汉字转为拼音,方便汉字注音、排序。
将汉语转为拼音。可以用于汉字注音、排序、检索

支持将汉字转换为多种不同风格的拼音。
## 特性

## 安装
* 根据词组智能匹配最正确的拼音。
* 支持多音字。
* 简单的繁体支持。
* 支持多种不同拼音风格。

暂时未托管到 npmjs.org
## 安装

git clone git://github.com/hotoo/node-pinyin.git
npm install ./node-pinyin
```
npm install pinyinjs
```

## 用法

var pinyin = require("pinyin");

console.log(pinyin("中心")); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
heteronym: true // 启用多音字模式
})); // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
style: pinyin.STYLE_INITIALS, // 设置拼音风格
heteronym: true
})); // [ [ 'zh' ], [ 'x' ] ]

```js
var pinyin = require("pinyin");

console.log(pinyin("中心")); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
heteronym: true // 启用多音字模式
})); // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
style: pinyin.STYLE_INITIALS, // 设置拼音风格
heteronym: true
})); // [ [ 'zh' ], [ 'x' ] ]
```

## API

Expand Down
1 change: 0 additions & 1 deletion bin/cli.js

This file was deleted.

30 changes: 30 additions & 0 deletions bin/pinyin
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/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').
parse(process.argv);

if (commander.list) {
process.exit()
}

// output help and exit if no args found
if (commander.args.length === 0) {
commander.help();
}

console.log(pinyin(commander.args.join(" "), {
style: pinyin["STYLE_" + (commander.style || "TONE").toUpperCase()],
heteronym: commander.heteronym || false
}).join(" "));

/*
vim:ft=javascript
*/
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{
"name": "pinyin2",
"version": "0.1.0",
"description": "Chinese Pinyin converter.",
"name": "pinyinjs",
"version": "2.0.1",
"description": "汉语拼音转换工具。",
"main": "src/pinyin.js",
"bin": {
"pinyin": "./bin/pinyin"
},
"directories": {
"example": "examples",
"test": "tests"
},
"scripts": {
"test": "tests/pinyin.js"
"test": "mocha -R spec tests/test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/hotoo/node-pinyin.git"
},
"dependencies": {
"segment": "0.0.2",
"simplebig": "0.0.3"
"simplebig": "0.0.3",
"commander": "~1.1.1"
},
"keywords": ["拼音", "Pinyin"],
"author": "闲耘 <[email protected]>",
Expand Down
28 changes: 13 additions & 15 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@
*/

var assert = require("assert");
var should = require("should");
var pinyin = require("../src/pinyin");

/*
* 笛卡尔乘积,返回两个数组的所有可能的组合。
* @param {Array}
* @param {Array}
* @return {Array}
*/
function product(a,b){
var r=[];
for (var i=0,l=a.length; i<l; i++){
for (var j=0,m=b.length; j<m; j++){
r[r.length] = (a[i] instanceof Array) ? a[i].concat(b[j]) : [].concat(a[i],b[j]);
}
}
return r;
}
describe("单个汉字", function(){
it("单", function(){
var py = pinyin("单");
pinyin("单").should.eql([["dān"]]);
});
});

console.log(pinyin('本票通【航旅专业版】',{
style: pinyin.STYLE_NORMAL,
heteronym: false
}));

return;
var hans1 = "天将将大任于斯人也";
var hans2 = "在地球重力的影响下,重心不稳。";

Expand Down

0 comments on commit 9d820ac

Please sign in to comment.