• Stars
    star
    200
  • Rank 190,891 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a module for tanslating Chinese(汉字) into /\w+/ or pinyin

Han(汉)

Build Status Coverage Status

a module for tanslating Chinese(汉字) into pinyin.

Installation

$ npm install han

Usage

1. han.letter()

han.letter(chinese ,[separator ,[callback(err, data)])

Use the letter method to translate Chinese in to english letter:

var han = require('han');

han.letter('中文') // zhongwen
han.letter('中文', '-') // zhong-wen
han.letter('中文', function(err, result){
  console.log(result) // zhongwen
})

The letter method has 3 params by default, they can all be optional, it depends on you(^^). follow the message below, you can also find it at lib/translate.js:

/* @param `chinese` {String} Chinese word
 * @param [optional] `separator` {String} separator for the letters
 * @param [optional] `callback(err, result)` {Function} if a callback is specified,
 *   the program will use an async way to do the translation 
 */

2. han.pinyin(chinese)

User the pinyin method to translate Chinese into Pinyin(letters with notation):

han.pinyin('中文') // [ [ 'zhōng', 'zhòng' ], [ 'wén', 'wèn' ] ]

// mixin are allowed, like:
han.pinyin('My Chinese name is 小鱼(sofish)');
// [ 'My Chinese name is ', [ 'xǐao' ], [ 'yú' ], '(sofish)' ]

// also running with messy code, see the testcase below

WHY the return value is an array?

Beause that a Chinese word always having more than one pronunciation. You may want to allow users to correct your output that is generated by your program in an automatic way(like google?! yep!).

Testcase

$ npm install han
# or git clone git://github.com/sofish/han.git

$ cd node_modules/han && make test

Translating Messy Code

Messy code? orginal file is here testcase.js:

var han = require('han');

console.log('orginal:要实现 Speaker Deck 那种中文转拼音的')
console.log(han.letter('要实现 Speaker Deck 那种中文转拼音的', '-'), '\n')
// orginal:要实现 Speaker Deck 那种中文转拼音的
// yao-shi-xian-speaker-deck-na-zhong-zhong-wen-zhuan-pin-yin-de

han.letter('中aaaaa中¢∞§¶•誩aa文喳aa', function(err, result){
  if(err) throw err;
  console.log('orginal:中aaaaa中¢∞§¶•誩aa文喳aa')
  console.log('callback:' + result, '\n')
  // orginal:中aaaaa中¢∞§¶•誩aa文喳aa
  // callback:zhong aaaaa 4e2da2 221ea7b6 2022 jing aa wen zha aa
});

han.letter('中EnglishWords¢∞§¶•ªº文', '-', function(err, result){
  console.log('orginal:中EnglishWords¢∞§¶•ªº文')
  console.log('callback:' + result)
  // original:中EnglishWords¢∞§¶•ªº文
  // callback:zhong-englishwords-221ea7b6-2022aaba-wen
});


console.log('original: My Chinese name is 小鱼(sofish)')
console.log(han.pinyin('My Chinese name is 小鱼(sofish)'))
// original: My Chinese name is 小鱼(sofish)
// [ 'My Chinese name is ', [ 'xǐao' ], [ 'yú' ], '(sofish)' ]

console.log('original: #$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english')
console.log(han.pinyin('#$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english'))
// original: #$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english
// [ '#$%^&*', [ 'zhōng', 'zhòng' ], '23¢∞§¶•ª52849', [ 'wén', 'wèn' ],
//  '@#$%^&*(', [ 'yì' ], [ 'sī', 'sì', 'sāi' ], ',', [ 'húan', 'hái', 'xúan' ],
//  [ 'yǒu', 'yòu' ], [ 'yīng', 'yāng' ], [ 'wén', 'wèn' ], ':english' ]

License

Licensed under MIT.

Contributers:

Specail thank to fayland for providing the unicode table of Chinese.

More Repositories

1

pen

enjoy live editing (+markdown)
JavaScript
4,788
star
2

typo.css

中文网页重设与排版:一致化浏览器排版效果,构建最适合中文阅读的网页排版
HTML
4,477
star
3

wechat.js

微信相关的 js 操作:分享、网络、菜单
JavaScript
920
star
4

express-guide

对英文版 ExpressJS 的一个翻译
363
star
5

validator.js

一个简单、轻量级,但功能强大的 Validator 组件
JavaScript
284
star
6

essage

a more elegant way to show message
JavaScript
187
star
7

learn-js

老婆想学 js 这件事就是一个政治任务
142
star
8

everyday

从今天开始,要么写代码要么写文档
JavaScript
133
star
9

sofi.sh

program of http://sofi.sh
118
star
10

restcookbook

REST 小书
HTML
84
star
11

weather

weather app for iOS device
CSS
84
star
12

weather-api

提供一个从 weather.com.cn 上根据城市接口取天气信息的方法
JavaScript
56
star
13

node-api-docs

解读 node.js api 文档
54
star
14

stacktrace.js

record js stack trace
JavaScript
52
star
15

imageXSS.js

防止外部链接通过图片进行 XSS 攻击
JavaScript
46
star
16

performance.js

calculate performance timing for web page
JavaScript
46
star
17

proportion.js

meet the best proportion of the device.
HTML
41
star
18

hire

理论上是长期的,不定时更新
29
star
19

recorder.js

Using HTML5 to capture photo / video via the camera of your device
JavaScript
29
star
20

writedown

Stop thinking, start creating.
JavaScript
28
star
21

A-Poem-A-Day

给儿子念诗,每天一首
27
star
22

storage.js

a jQuery plugin that make text storage works with all mayor browsers (include IE6+)
19
star
23

sofish

13
star
24

mongoimport

import JSON to mongo collection
JavaScript
11
star
25

resize.js

use canvas to resize image
JavaScript
10
star
26

log2json

transform nginx log to json
JavaScript
9
star
27

goyd

自用有道翻译 CLI 小工具
Go
6
star
28

mask.js

create an opacity mask to a(all) specific element(s)
JavaScript
3
star
29

nodebb-plugin-qiniu-file

save images/files to qiniu.com
JavaScript
2
star
30

bx_suggest

百姓网搜索自动完成 workflow.
PHP
2
star
31

hasFlashPlayer.js

a snippet to detect whether a FlashPlayer is installed in browser.
JavaScript
1
star
32

log2mongo

automatic log to mongodb
JavaScript
1
star
33

donuts

hybrid sucks
JavaScript
1
star