Skip to content

Tweak example of using a Chinese word segmenter to one where the segmenter segments in a way that seems more correct (or at least more self-consistent) to a native Chinese speaker #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions test/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ describe('WordDiff', function() {

it('supports tokenizing with an Intl.Segmenter', () => {
// Example 1: Diffing Chinese text with no spaces.
// I am not a Chinese speaker but I believe these sentences to mean:
// 1. "I have (我有) many (很多) tables (桌子)"
// 2. "Mei (梅) has (有) many (很多) sons (儿子)"
// a. "He (他) has (有) many (很多) tables (桌子)"
// b. "Mei (梅) has (有) many (很多) sons (儿子)"
// We want to see that diffWords will get the word counts right and won't try to treat the
// trailing 子 as common to both texts (since it's part of a different word each time).
// TODO: Check with a Chinese speaker that this example is correct Chinese.
const chineseSegmenter = new Intl.Segmenter('zh', {granularity: 'word'});
const diffResult = diffWords('我有很多桌子。', '梅有很多儿子。', {intlSegmenter: chineseSegmenter});
expect(diffResult).to.deep.equal([
{ count: 1, added: false, removed: true, value: '我有' },
{ count: 2, added: true, removed: false, value: '梅有' },
{ count: 1, added: false, removed: false, value: '很多' },
{ count: 1, added: false, removed: true, value: '' },
{ count: 1, added: true, removed: false, value: '' },
{ count: 2, added: false, removed: false, value: '有很多' },
{ count: 1, added: false, removed: true, value: '桌子' },
{ count: 1, added: true, removed: false, value: '儿子' },
{ count: 1, added: false, removed: false, value: '。' }
Expand Down