Skip to content

Commit

Permalink
Merge 2.3.0, both support node and web commonjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed May 15, 2014
2 parents 052ed14 + 3fe6cd9 commit 87a9cfa
Show file tree
Hide file tree
Showing 34 changed files with 504,259 additions and 421 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
language: node_js

node_js:
- "0.8"
- "0.10"

install:
- npm install mocha-browser nico
- npm install spm@ninja coveralls mocha

before_script:
- git clone git://github.com/aralejs/nico-arale.git _theme
- node_modules/.bin/nico build --theme _theme -C _theme/nico.js
- node_modules/spm/bin/spm-install
- npm install

script:
- node_modules/.bin/mocha-browser _site/tests/runner.html -S
- node_modules/spm/bin/spm-test
- node_modules/.bin/mocha -R spec tests/test.js

after_success:
- npm install jscoverage coveralls
- node_modules/.bin/jscoverage --encoding=utf8 src _site/src-cov
- node_modules/.bin/mocha-browser _site/tests/runner.html?cov -S -R lcov | node_modules/.bin/coveralls
- node_modules/spm/bin/spm-test --coveralls | node_modules/.bin/coveralls
62 changes: 43 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
THEME = $(HOME)/.spm/themes/arale
version = $(shell cat package.json | grep version | awk -F'"' '{print $$4}')

build-doc:
@nico build -C $(THEME)/nico.js
install:
@spm install
@npm install

build:
@spm build

debug:
@nico server -C $(THEME)/nico.js --watch debug
publish: build publish-doc
@spm publish
@npm publish
@git tag $(version)
@git push origin $(version)

server:
@nico server -C $(THEME)/nico.js
build-doc:
@spm doc build

watch:
@nico server -C $(THEME)/nico.js --watch
@spm doc watch

publish-doc: clean build-doc
@ghp-import _site
@git push origin gh-pages

build:
@spm build

publish:
@spm publish -s spmjs

clean:
@rm -fr _site


runner = _site/tests/runner.html
test-src:
@mocha-browser ${runner} -S
test-dist:
@mocha-browser ${runner}?dist -S
test: test-src test-dist

test-npm:
@mocha -R spec tests/test.js

test-spm:
@spm test

test: test-npm test-spm

output = _site/coverage.html
coverage: build-doc
Expand All @@ -41,4 +45,24 @@ coverage: build-doc
@echo "Build coverage to ${output}"


ZI_DICT_FREQUENT = ./tools/dict/zi-frequent.js
ZI_DICT_INFREQUENT = ./tools/dict/zi-infrequent.js
ZI_DICT= ./tools/dict/dict-zi.js

dict-web:
@echo 'module.exports = {' > $(ZI_DICT_FREQUENT)
@node ./tools/robot-frequent.js >> $(ZI_DICT_FREQUENT)
@echo '};' >> $(ZI_DICT_FREQUENT)
@echo 'module.exports = {' > $(ZI_DICT_INFREQUENT)
@node ./tools/robot-infrequent.js >> $(ZI_DICT_INFREQUENT)
@echo '};' >> $(ZI_DICT_INFREQUENT)

dict-node:
@echo 'var dict = [];' > $(ZI_DICT)
@node ./tools/robot-zdic-zi.js >> $(ZI_DICT)
@echo 'module.exports = dict;' >> $(ZI_DICT)

infrequent:
@node ./tools/infrequent.js > ./tools/zi/infrequent.js

.PHONY: build-doc publish-doc server clean test coverage
122 changes: 106 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,77 @@

---

[![NPM version](https://badge.fury.io/js/pinyin.png)](http://badge.fury.io/js/pinyin)
[![spm package](http://spmjs.io/badge/pinyin)](http://spmjs.io/package/pinyin)
[![Build Status](https://secure.travis-ci.org/hotoo/pinyin.png?branch=master)](https://travis-ci.org/hotoo/pinyin)
[![Coverage Status](https://coveralls.io/repos/hotoo/pinyin/badge.png?branch=master)](https://coveralls.io/r/hotoo/pinyin)

转换中文字符为拼音。
转换中文字符为拼音。可以用于汉字注音、排序、检索。

注:这是基于浏览器运行的版本,另外还同步提供了更强的 [NodeJS 版本](https://github.com/hotoo/node-pinyin)
> 注:这个版本同时支持在 Node 和 Web 浏览器环境运行,
> 之前的 [hotoo/node-pinyin](https://github.com/hotoo/node-pinyin) 仓库即将下线。
>
> Python 版请关注 [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin)
---

## 使用说明
## 特性

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

## 安装

via npm:

```bash
npm install pinyin
```

via [email protected]:

```bash
spm install pinyin
```

## 用法

开发者:

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

pinyin("重点");
pinyin("重点", {
style: pinyin.STYLE_NORMAL,
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' ] ]
```

命令行:

```bash
$ pinyin 中心
zhōng xīn
$ pinyin -h
```

## API

### 方法 `<Array> pinyin(words[, options])`

将传入的中文字符(words)转换成拼音符号
将传入的中文字符串(words)转换成拼音符号串

options 是可选的,可以设定拼音风格,或打开多音字选项。

返回二维数组,第一维每个数组项位置对应每个中文字符串位置。
第二维是各个汉字的读音列表,多音字会有多个拼音项。

### 参数 `<Boolean> options.heteronym`

是否启用多音字模式,默认关闭。
Expand All @@ -39,35 +81,83 @@ options 是可选的,可以设定拼音风格,或打开多音字选项。

启用多音字模式时,返回多音字的所有拼音列表。

### 属性 `.STYLE_NORMAL`
### 参数 `<Object> options.style`

指定拼音 风格。可以通过以下几种 `STYLE_` 开头的静态属性进行指定。

### 静态属性 `.STYLE_NORMAL`

普通风格,即不带音标。

如:`pin yin`

### 属性 `.STYLE_TONE`
### 静态属性 `.STYLE_TONE`

声调风格,拼音声调在韵母第一个字母上。

注:这是默认的风格。

如:`pīn yīn`

### 属性 `.STYLE_TONE2`
### 静态属性 `.STYLE_TONE2`

声调风格2,即拼音声调在各个拼音之后,用数字 [0-4] 进行表示。

如:`pin1 yin1`

### 属性 `.STYLE_INITIALS`
### 静态属性 `.STYLE_INITIALS`

声母风格,只返回各个拼音的声母部分。

如:`中国` 的拼音 `zh g`

例外,对于只有韵母的汉字(如『爱、啊』等),会先转成不带音标的普通风格。

### 静态属性 `.STYLE_FIRST_LETTER`

首字母风格,只返回拼音的首字母部分。

如:`p y`

### 属性 `.STYLE_FIRST_LETTER`

首字母风格,只返回拼音的首字母部分。
## Test

```
npm test
```

## Q&A

### 为什么 node 版和 Web 版的拼音程序要有两个不同的仓库?

`pinyin` 目前可以同时运行在 Node 服务器端和 Web 浏览器端。

但 Web 版较 Node 版稍简单,拼音库只有常用字部分,没有使用分词算法,
并且考虑了网络传输对词库进行了压缩处理。

| 特性 | Web 版 | Node 版 |
|--------------|--------------------------------|----------------------------------|
| 拼音库 | 常用字库。压缩、合并 | 完整字库。不压缩、合并 |
| 分词 | 没有分词 | 使用分词算法,多音字拼音更准确。 |
| 拼音频度排序 | 有根据拼音使用频度优先级排序。 | 同 Web 版。 |
| 繁体中文 | 没有繁体中文支持。 | 有简单的繁简汉字转换。 |

由于这些区别,测试不同运行环境的用例也不尽相同。

### 为什么不使用更好的繁简转换?

对于拼音来说,把繁简汉字做简单的映射并转换成拼音是合理的。
而使用类似 [OpenCC](https://github.com/BYVoid/OpenCC)
[cconv](https://code.google.com/p/cconv/) 这样的转换库,虽然转换结果更
符合本地化语境,但是对于汉字变化后的拼音来说,已经不是繁体原字的拼音了。


## 参考

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/hotoo/pinyin.js/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
* [在线汉语字典](http://zi.artx.cn/zi/)
* [快典网](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)
([pinyin.rar](http://cid-80b2ed83de3c7c17.skydrive.live.com/self.aspx/Code/pinyin.rar))
* [Javascript Input Method Editors](http://jsime.sourceforge.net/) ([中文](http://leen.name/ime/pinyin.html), [En](http://leen.name/ime/english.html))
* [现在汉语的词汇数量及分布](http://blog.cathayan.org/item/1593)
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
*/
2 changes: 1 addition & 1 deletion examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ textarea{width:90%; height:100px;}


<script>
seajs.use('pinyin', function(pinyin){
seajs.use('../src/pinyin', function(pinyin){

var $ = function(id){return document.getElementById(id);}
var styles = document.getElementsByName("style");
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require("src/pinyin");
38 changes: 30 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
{
"name": "pinyin",
"version": "2.1.2",
"family": "hotoo",
"description": "汉字拼音转换工具 Web 版。",
"version": "2.3.0",
"description": "汉语拼音转换工具。",
"keywords": ["拼音", "Pinyin"],
"homepage": "http://hotoo.me/pinyin.js",
"homepage": "http://pinyin.hotoo.me/",
"author": "闲耘 <[email protected]>",
"main": "index.js",
"bin": {
"pinyin": "./bin/pinyin"
},
"directories": {
"example": "examples",
"test": "tests"
},
"repository": {
"type": "git",
"url": "https://github.com/hotoo/pinyin.js.git"
"url": "https://github.com/hotoo/pinyin.git"
},
"bugs": {
"url": "https://github.com/hotoo/pinyin.js/issues"
"url": "https://github.com/hotoo/pinyin/issues"
},
"scripts": {
"prepublish": "spm build"
"prepublish": "spm build",
"test": "make test"
},
"dependencies": {
"segment": "0.0.5",
"commander": "~1.1.1"
},
"devDependencies": {
"mocha": "1.17.1",
"expect.js": "0.3.1",
"iconv": "2.1.0",
"request": "2.33.0"
},
"spm": {
"output": ["pinyin.js"]
"main": "index.js",
"devDependencies": {
"expect.js": "0.3.1"
},
"tests": "tests/*-spec.js"
},
"license": "MIT"
}
Loading

0 comments on commit 87a9cfa

Please sign in to comment.