Skip to content

Commit

Permalink
Change 'translate' to 'romanize'
Browse files Browse the repository at this point in the history
Correction
  • Loading branch information
dchan3 committed Apr 21, 2018
1 parent d4dc21d commit be0d805
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions lib/translate.js → lib/romanize.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()
Expand All @@ -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];

Expand Down Expand Up @@ -79,4 +79,4 @@ translate.pinyin = function(chinese){
return result;
}

module.exports = exports = translate;
module.exports = exports = romanize;
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,'');
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"dependencies": {},
Expand Down
35 changes: 16 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 **.
Output is generated regardless of context.

## Testcase

Expand Down Expand Up @@ -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).
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down

0 comments on commit be0d805

Please sign in to comment.