diff --git a/lib/index.js b/lib/index.js index 4db8fe0..9d797ed 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ -var translate = require('./translate') +var romanize = require('./romanize') module.exports = exports = { - letter: translate.letter - , pinyin: translate.pinyin + letter: romanize.letter + , pinyin: romanize.pinyin } diff --git a/lib/translate.js b/lib/romanize.js similarity index 90% rename from lib/translate.js rename to lib/romanize.js index fd63a04..0b4a3bc 100644 --- a/lib/translate.js +++ b/lib/romanize.js @@ -1,8 +1,8 @@ -var translate = {} +var romanize = {} , util = require('./util') , code = require('./Mandarin.json') -/* translate Chinese word into English letter +/* romanize Chinese word into English letter * @param `chinese` {String} Chinese words or whatever * @param [optional] `separator` {String} separator for the letters * @param [optional] `callback(err, result)` {Function} if a callback is specified, @@ -16,7 +16,7 @@ var translate = {} * console.log(result) // zhong wen * }) */ -translate.letter = function(){ +romanize.letter = function(){ var args = [].slice.call(arguments) , originalArgs = args.slice() , chinese = args.shift() @@ -35,10 +35,10 @@ translate.letter = function(){ make(chinese, code, separator); } -/* translate Chinese into Pinyin(letters with notation) +/* romanize Chinese into Pinyin(letters with notation) * @param `chinese` {String} Chinese words or wahtever */ -translate.pinyin = function(chinese){ +romanize.pinyin = function(chinese){ if(!chinese) return []; if(!util.isChinese(chinese)) return [chinese]; @@ -79,4 +79,4 @@ translate.pinyin = function(chinese){ return result; } -module.exports = exports = translate; +module.exports = exports = romanize; diff --git a/lib/util.js b/lib/util.js index 8a41389..3a7a019 100644 --- a/lib/util.js +++ b/lib/util.js @@ -18,7 +18,7 @@ module.exports = exports = { words = escape(words).split('%u'), words.shift(), words; words.forEach(function(item, i) { - // rember to translate non-Chinese words to it's own unicode + // rember to romanize non-Chinese words to it's own unicode words[i] = data[item] ? data[item].split(/\s+/)[0].replace(/[1-5]/,'') : item.replace(/[%@*]+/g,''); }) diff --git a/package.json b/package.json index 3973d6c..799099b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "han", - "description": "a module for tanslating Chinese(汉字) into pinyin", + "description": "Generate romanized Chinese strings", "version": "0.0.7", "author": "Sofish Lin ", "dependencies": {}, diff --git a/readme.md b/readme.md index e94bf1e..b1f32b4 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/sofish/han.png)](https://travis-ci.org/sofish/han) [![Coverage Status](https://coveralls.io/repos/sofish/han/badge.png)](https://coveralls.io/r/sofish/han) -a module for tanslating Chinese(汉字) into pinyin. +Generate romanized Chinese strings ## Installation @@ -13,50 +13,48 @@ $ npm install han ## Usage -### 1. han.letter() +### 1. `han.letter()` ```js han.letter(chinese ,[separator ,[callback(err, data)]) ``` -Use the `letter` method to translate Chinese in to english letter: +Use the `letter` method to generate a romanization without tone marks: ```js 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](https://github.com/sofish/han/blob/master/lib/translate.js): - ```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 + * the program will use an async way to do the translation */ ``` -### 2. han.pinyin(chinese) +### 2. `han.pinyin(chinese)` -User the `pinyin` method to translate Chinese into Pinyin(letters with notation): +Use the `pinyin` method to generate an array of lists consisting of all possible +pronunciations of corresponding characters: ```js han.pinyin('中文') // [ [ 'zhōng', 'zhòng' ], [ 'wén', 'wèn' ] ] -// mixin are allowed, like: +// Mixed text is allowed, e.g.: 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? +#### __Why return arrays?__ -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!). +Certain characters may have multiple pronuncations, primarily +differing in tone, the syllable itself in rare cases, or both. +An example of such a character is **的**, which can be read as +either *de* or *dí*. +Output is generated regardless of context. ## Testcase @@ -112,7 +110,6 @@ console.log(han.pinyin('#$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有 Licensed under [MIT](https://github.com/sofish/han/blob/master/LICENSE). -## Contributers: - -Specail thank to [fayland](https://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin/lib/Lingua/Han/PinYin) for providing the unicode table of Chinese. +## Contributors: +Character pronunciation data file courtesy of [fayland](https://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin/lib/Lingua/Han/PinYin). diff --git a/test/test.js b/test/test.js index 0a358c5..ad87988 100644 --- a/test/test.js +++ b/test/test.js @@ -8,7 +8,7 @@ describe('han', function () { han.letter('要实现 Speaker Deck 那种中文转拼音的', '-').should.be.equal(expect); }); - it('letter with 特殊符号', function () { + it('letter with input mixed with special characters', function () { han.letter('中aaaaa中¢∞§¶•誩aa文喳aa').should.be.equal('zhongaaaaa4e2da2221ea7b62022jingaawenzhaaa'); han.letter('中EnglishWords¢∞§¶•ªº文', '-').should.be.equal('zhong-englishwords-221ea7b6-2022aaba-wen'); });