Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions javascript/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export class Parser {
const tw4 = this.model.get('TW4');

for (let i = 1; i < sentence.length; i++) {
// Don't separate the two halves of a surrogate pair.
if ((sentence.codePointAt(i - 1) ?? 0) > 0xffff) continue;
let score = this.baseScore;
// NOTE: Score values in models may be negative.
score += uw1?.get(sentence.substring(i - 3, i - 2)) || 0;
Expand Down
9 changes: 9 additions & 0 deletions javascript/src/tests/parser_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ describe('Parser.parse', () => {
expect(result).toEqual(['a', 'bcdea', 'bcd']);
});

it('should not separate inside a surrogate pair.', () => {
const model = {
UW4: {a: -10000}, // means "should separate everywhere except before 'a'".
};
const parser = new Parser(model);
const result = parser.parse('𠮷野家');
expect(result).toEqual(['𠮷', '野', '家']);
});

it('should return an empty list when the input is a blank string.', () => {
const parser = new Parser({});
const result = parser.parse('');
Expand Down
Loading