Skip to content

Commit

Permalink
Merge 9298548 into 2c914c7
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed May 9, 2020
2 parents 2c914c7 + 9298548 commit be8fd30
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README-us_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ console.log(pinyin("中心", {
})); // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
heteronym: true, // Enable heteronym mode.
segment: true // Enable Chinese words eegmentation, fix most heteronym problem.
segment: true // Enable Chinese words segmentation, fix most heteronym problem.
})); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("我喜欢你", {
segment: true, // Enable segmentation. Needed for grouping.
group: true // Group pinyin segments
})); // [ [ [ 'wǒ' ] ], [ [ 'xǐ' ], [ 'huān' ] ], [ [ 'nǐ' ] ] ]
console.log(pinyin("中心", {
style: pinyin.STYLE_INITIALS, // Setting pinyin style.
heteronym: true
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ console.log(pinyin("中心", {
heteronym: true, // 启用多音字模式
segment: true // 启用分词,以解决多音字问题。
})); // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("我喜欢你", {
segment: true, // 启用分词
group: true // 启用词组
})); // [ [ [ 'wǒ' ] ], [ [ 'xǐ' ], [ 'huān' ] ], [ [ 'nǐ' ] ] ]
console.log(pinyin("中心", {
style: pinyin.STYLE_INITIALS, // 设置拼音风格
heteronym: true
Expand Down
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ class NodePinyin extends Pinyin {
nohans = ""; // reset non-chinese words.
}

if (words.length === 1) {
pys = pys.concat(super.convert(words, options));
const newPys = words.length === 1
? super.convert(words, options)
: this.phrases_pinyin(words, options);

if (options.group) {
pys.push(newPys);
} else {
pys = pys.concat(this.phrases_pinyin(words, options));
pys = pys.concat(newPys);
}

} else {
Expand All @@ -47,6 +51,9 @@ class NodePinyin extends Pinyin {

// 清理最后的非中文字符串。
if(nohans.length > 0){
if (options.group) {
nohans = [nohans];
}
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,17 @@ describe("pinyin.compare", function() {
expect(sortedData).to.eql("排我序要".split(""));
});
});

describe("pinyin group", function() {
it("groups segments", function () {
const han = "我喜欢你";
const py = pinyin(han, {segment: true, group: true, heteronym: true});
expect(py).to.eql([[["wǒ"]], [["xǐ"], ["huān"]], [["nǐ"]]]);
});

it("groups segments with heteronyms", function() {
const han = "我都喜欢";
const py = pinyin(han, {segment: true, group: true, heteronym: true});
expect(py).to.eql([[["wǒ"]], [["dū", "dōu"]], [["xǐ"], ["huān"]]]);
});
});

0 comments on commit be8fd30

Please sign in to comment.