Skip to content

Commit

Permalink
Merge jieba into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed May 13, 2015
2 parents bd38517 + 3d9c29e commit d1604e6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 118,938 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Thumb.db
dist/
npm-debug.log
spm_modules/
tools/dict/error.log
coverage/
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "pinyin",
"version": "2.3.3",
"version": "2.4.0",
"description": "汉语拼音转换工具。",
"keywords": ["拼音", "Pinyin"],
"keywords": [
"拼音",
"Pinyin"
],
"homepage": "http://pinyin.hotoo.me/",
"author": "闲耘 <hotoo.cn@gmail.com>",
"main": "index.js",
Expand All @@ -24,8 +27,8 @@
"test": "make test"
},
"dependencies": {
"segment": "0.0.5",
"commander": "~1.1.1"
"commander": "~1.1.1",
"nodejieba": "^0.2.11"
},
"devDependencies": {
"mocha": "1.17.1",
Expand Down
16 changes: 8 additions & 8 deletions src/pinyin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var isNode = typeof process === "object" &&
process.toString() === "[object process]";

// 分词模块
var Segment;
var jieba;
var PHRASES_DICT;
var PINYIN_DICT;

Expand Down Expand Up @@ -31,11 +31,11 @@ function buildPinyinCache(dict_combo){
}

if(isNode){
Segment = module["require"]("segment").Segment;
var segment = new Segment();
// 使用默认的识别模块及字典
segment.useDefault();

jieba = module['require']('nodejieba');
jieba.loadDict(
"./node_modules/nodejieba/dict/jieba.dict.utf8",
"./node_modules/nodejieba/dict/hmm_model.utf8",
"./node_modules/nodejieba/dict/user.dict.utf8");

// 词语拼音库。
PHRASES_DICT = module["require"]("./phrases-dict");
Expand Down Expand Up @@ -192,13 +192,13 @@ function pinyin(hans, options){

options = extend(DEFAULT_OPTIONS, options || {});

var phrases = isNode ? segment.doSegment(hans) : hans;
var phrases = isNode ? jieba.cutSync(hans) : hans;
var len = hans.length;
var pys = [];

for(var i=0,nohans="",firstCharCode,words,l=phrases.length; i<l; i++){

words = isNode ? phrases[i].w : phrases[i];
words = phrases[i];
firstCharCode = words.charCodeAt(0);

if(PINYIN_DICT[firstCharCode]){
Expand Down
23 changes: 15 additions & 8 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ var cases = [
//STYLE_FIRST_LETTER: [["a a"]]
//} ],
[ "一 一", {
STYLE_NORMAL: [["yi"],["yi"]],
STYLE_TONE: [["yī"],["yī"]],
STYLE_TONE2: [["yi1"],["yi1"]],
STYLE_INITIALS: [["y"],["y"]],
STYLE_FIRST_LETTER: [["y"],["y"]]
STYLE_NORMAL: [["yi"],[' '],["yi"]],
STYLE_TONE: [["yī"],[' '],["yī"]],
STYLE_TONE2: [["yi1"],[' '],["yi1"]],
STYLE_INITIALS: [["y"],[' '],["y"]],
STYLE_FIRST_LETTER: [["y"],[' '],["y"]]
} ],

// 中英混合
Expand All @@ -110,7 +110,16 @@ var cases = [
STYLE_TONE2: [["zhong1"],["guo2"],["(china)"]],
STYLE_INITIALS: [["zh"],["g"],["(china)"]],
STYLE_FIRST_LETTER: [["z"],["g"],["(china)"]]
} ]
} ],

// 中英混合,多音字,单音词。
[ "0套价", {
STYLE_NORMAL: [["0"],["tao"],["jia","jie"]],
STYLE_TONE: [["0"],["tào"],["jià","jiè","jie"]],
STYLE_TONE2: [["0"],["tao4"],["jia4","jie4","jie"]],
STYLE_INITIALS: [["0"],["t"],["j"]],
STYLE_FIRST_LETTER: [["0"],["t"],["j"]]
} ],
];

describe('pinyin', function() {
Expand Down Expand Up @@ -189,5 +198,3 @@ function deepEquals(a, b){
break;
}
}

console.log("DEEP EQUALS:", deepEquals([["zhong"],["guo"]] , [["zhong"],["guo"]]))
Loading

0 comments on commit d1604e6

Please sign in to comment.