Skip to content

Commit

Permalink
fix: mixed Chinese and non-Chinese words
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 3, 2020
1 parent a0c40e1 commit 40be9e7
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 125 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org

- name: 'Cargo test'
run: cargo test

- name: 'Build'
if: matrix.os != 'macos-latest'
run: yarn build
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ napi = {git = "https://github.com/napi-rs/napi-rs"}
napi-derive = {git = "https://github.com/napi-rs/napi-rs"}
once_cell = "1.4"
pinyin = "0.8"
rayon = "1.5"

[target.'cfg(all(unix, not(target_env = "musl")))'.dependencies]
jemallocator = {version = "0.3", features = ["disable_initial_exec_tls"]}
Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ yarn add @napi-rs/pinyin
Benchmark over `pinyin` package:

```bash
yarn run v1.22.10
$ node -r @swc-node/register benchmark/bench.ts
Running "Short input without segment" suite...
Progress: 100%

@napi-rs/pinyin:
1 037 055 ops/s, ±0.62% | fastest
962 035 ops/s, ±0.68% | fastest

node-pinyin:
461 335 ops/s, ±0.87% | slowest, 55.51% slower
434 241 ops/s, ±0.66% | slowest, 54.86% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Expand All @@ -60,10 +58,10 @@ Running "Long input without segment" suite...
Progress: 100%

@napi-rs/pinyin:
325 ops/s, ±0.54% | fastest
59 ops/s, ±0.83% | fastest

node-pinyin:
2 ops/s, ±3.44% | slowest, 99.38% slower
2 ops/s, ±3.30% | slowest, 96.61% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Expand All @@ -72,10 +70,10 @@ Running "Short input with segment" suite...
Progress: 100%

@napi-rs/pinyin:
1 010 506 ops/s, ±2.59% | fastest
530 228 ops/s, ±1.94% | fastest

node-pinyin:
315 076 ops/s, ±1.02% | slowest, 68.82% slower
307 788 ops/s, ±0.83% | slowest, 41.95% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Expand All @@ -84,15 +82,15 @@ Running "Long input with segment" suite...
Progress: 100%

@napi-rs/pinyin:
330 ops/s, ±1.08% | fastest
152 ops/s, ±1.09% | fastest

node-pinyin:
3 ops/s, ±2.44% | slowest, 99.09% slower
3 ops/s, ±3.08% | slowest, 98.03% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Slowest: node-pinyin
✨ Done in 52.94s.
✨ Done in 53.36s.
```

## 用法
Expand Down
13 changes: 13 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ for (const fixture of fixtures) {
}
}

test('mixed with segment', (t) => {
t.deepEqual(pinyin('特殊天-1', { style: PINYIN_STYLE.Plain, segment: true }), ['te', 'shu', 'tian', '-1'])
})

test('mixed with multi segment', (t) => {
t.deepEqual(pinyin('特殊天-1', { style: PINYIN_STYLE.Plain, segment: true, heteronym: true }), [
['te'],
['shu'],
['tian'],
['-1'],
])
})

test('我,要,排,序 => 序,我,排,要', (t) => {
const data = '我要排序'.split('')
const sortedData = data.sort(compare)
Expand Down
Loading

1 comment on commit 40be9e7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 40be9e7 Previous: b21547b Ratio
Short input without segment#@napi-rs/pinyin 577606 ops/sec (±0.91%) 488927 ops/sec (±1.11%) 0.85
Short input without segment#node-pinyin 320882 ops/sec (±0.77%) 268041 ops/sec (±1.21%) 0.84
Long input without segment#@napi-rs/pinyin 45 ops/sec (±1.48%) 174 ops/sec (±1.03%) 3.87
Long input without segment#node-pinyin 1 ops/sec (±1.74%) 1 ops/sec (±7.73%) 1
Short input with segment#@napi-rs/pinyin 350149 ops/sec (±1.51%) 501747 ops/sec (±1.2%) 1.43
Short input with segment#node-pinyin 210479 ops/sec (±1%) 180016 ops/sec (±1.46%) 0.86
Long input with segment#@napi-rs/pinyin 99 ops/sec (±0.95%) 169 ops/sec (±1.84%) 1.71
Long input with segment#node-pinyin 2 ops/sec (±1.62%) 2 ops/sec (±4.25%) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.