GithubHelp home page GithubHelp logo

renpingjun2046 / han-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sofish/han

0.0 2.0 0.0 431 KB

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

License: MIT License

Makefile 7.04% JavaScript 92.96%

han-1's Introduction

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.

han-1's People

Contributors

jacksontian avatar sofish avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.