|
| 1 | +// SPDX-FileCopyrightText: 2024 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | +import { SentenceTokenizer } from './index.js'; |
| 6 | + |
| 7 | +const TEXT = |
| 8 | + 'Hi! ' + |
| 9 | + 'LiveKit is a platform for live audio and video applications and services. \n\n' + |
| 10 | + 'R.T.C stands for Real-Time Communication... again R.T.C. ' + |
| 11 | + 'Mr. Theo is testing the sentence tokenizer. ' + |
| 12 | + '\nThis is a test. Another test. ' + |
| 13 | + 'A short sentence.\n' + |
| 14 | + 'A longer sentence that is longer than the previous sentence. ' + |
| 15 | + 'f(x) = x * 2.54 + 42. ' + |
| 16 | + 'Hey!\n Hi! Hello! ' + |
| 17 | + '\n\n' + |
| 18 | + 'This is a sentence. 这是一个中文句子。これは日本語の文章です。' + |
| 19 | + '你好!LiveKit是一个直播音频和视频应用程序和服务的平台。' + |
| 20 | + '\nThis is a sentence contains consecutive spaces.'; |
| 21 | + |
| 22 | +// BlingFire may split sentences differently than the basic tokenizer |
| 23 | +// These are the expected results when using BlingFire with minSentenceLength=20 |
| 24 | +const EXPECTED_MIN_20 = [ |
| 25 | + 'Hi! LiveKit is a platform for live audio and video applications and services.', |
| 26 | + 'R.T.C stands for Real-Time Communication... again R.T.C. Mr. Theo is testing the sentence tokenizer.', |
| 27 | + 'This is a test. Another test.', |
| 28 | + 'A short sentence. A longer sentence that is longer than the previous sentence. f(x) = x * 2.54 + 42.', |
| 29 | + 'Hey! Hi! Hello! This is a sentence.', |
| 30 | + '这是一个中文句子。これは日本語の文章です。', |
| 31 | + '你好!LiveKit是一个直播音频和视频应用程序和服务的平台。', |
| 32 | + 'This is a sentence contains consecutive spaces.', |
| 33 | +]; |
| 34 | + |
| 35 | +const SIMPLE_TEXT = 'This is a sentence. This is another sentence. And a third one.'; |
| 36 | + |
| 37 | +describe('blingfire tokenizer', () => { |
| 38 | + describe('SentenceTokenizer', () => { |
| 39 | + const tokenizer = new SentenceTokenizer(); |
| 40 | + |
| 41 | + it('should tokenize simple sentences correctly', () => { |
| 42 | + const result = tokenizer.tokenize(SIMPLE_TEXT); |
| 43 | + expect(result).toBeDefined(); |
| 44 | + expect(result.length).toBeGreaterThan(0); |
| 45 | + // BlingFire should split the text into sentences |
| 46 | + expect(result.some((s) => s.includes('This is a sentence'))).toBeTruthy(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should tokenize complex text correctly', () => { |
| 50 | + const result = tokenizer.tokenize(TEXT); |
| 51 | + expect(result).toBeDefined(); |
| 52 | + expect(result.length).toBeGreaterThan(0); |
| 53 | + // Verify we get similar structure to expected |
| 54 | + expect(result.length).toBe(EXPECTED_MIN_20.length); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle empty string', () => { |
| 58 | + const result = tokenizer.tokenize(''); |
| 59 | + expect(result).toEqual([]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle single sentence', () => { |
| 63 | + const result = tokenizer.tokenize('This is a single sentence.'); |
| 64 | + expect(result).toBeDefined(); |
| 65 | + expect(result.length).toBeGreaterThan(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should respect minSentenceLength option', () => { |
| 69 | + const tokenizerMin50 = new SentenceTokenizer({ minSentenceLength: 50 }); |
| 70 | + const result = tokenizerMin50.tokenize(TEXT); |
| 71 | + expect(result).toBeDefined(); |
| 72 | + // All tokens except possibly the last should be >= 50 chars |
| 73 | + result.slice(0, -1).forEach((token) => { |
| 74 | + expect(token.length).toBeGreaterThanOrEqual(50); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should stream tokenize sentences correctly', async () => { |
| 79 | + const pattern = [1, 2, 4]; |
| 80 | + let text = TEXT; |
| 81 | + const chunks = []; |
| 82 | + const patternIter = Array(Math.ceil(text.length / pattern.reduce((sum, num) => sum + num, 0))) |
| 83 | + .fill(pattern) |
| 84 | + .flat() |
| 85 | + [Symbol.iterator](); |
| 86 | + |
| 87 | + // @ts-ignore |
| 88 | + for (const size of patternIter) { |
| 89 | + if (!text) break; |
| 90 | + chunks.push(text.slice(undefined, size)); |
| 91 | + text = text.slice(size); |
| 92 | + } |
| 93 | + |
| 94 | + const stream = tokenizer.stream(); |
| 95 | + for (const chunk of chunks) { |
| 96 | + stream.pushText(chunk); |
| 97 | + } |
| 98 | + stream.endInput(); |
| 99 | + |
| 100 | + const tokens = []; |
| 101 | + for await (const value of stream) { |
| 102 | + tokens.push(value.token); |
| 103 | + } |
| 104 | + |
| 105 | + expect(tokens).toBeDefined(); |
| 106 | + expect(tokens.length).toBeGreaterThan(0); |
| 107 | + // Should produce the same number of tokens as batch mode |
| 108 | + expect(tokens.length).toBe(EXPECTED_MIN_20.length); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should handle flush correctly', async () => { |
| 112 | + const stream = tokenizer.stream(); |
| 113 | + stream.pushText('This is the first part. '); |
| 114 | + stream.flush(); |
| 115 | + stream.pushText('This is the second part.'); |
| 116 | + stream.endInput(); |
| 117 | + |
| 118 | + const tokens = []; |
| 119 | + for await (const value of stream) { |
| 120 | + tokens.push(value.token); |
| 121 | + } |
| 122 | + |
| 123 | + expect(tokens.length).toBeGreaterThan(0); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should handle multiple pushText calls', async () => { |
| 127 | + const stream = tokenizer.stream(); |
| 128 | + stream.pushText('First sentence. '); |
| 129 | + stream.pushText('Second sentence. '); |
| 130 | + stream.pushText('Third sentence.'); |
| 131 | + stream.endInput(); |
| 132 | + |
| 133 | + const tokens = []; |
| 134 | + for await (const value of stream) { |
| 135 | + tokens.push(value.token); |
| 136 | + } |
| 137 | + |
| 138 | + expect(tokens.length).toBeGreaterThan(0); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle abbreviations correctly', () => { |
| 142 | + const text = 'Dr. Smith went to Washington D.C. yesterday. It was nice.'; |
| 143 | + const result = tokenizer.tokenize(text); |
| 144 | + expect(result).toBeDefined(); |
| 145 | + expect(result.length).toBeGreaterThan(0); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should handle numbers with decimals', () => { |
| 149 | + const text = 'The value is 3.14159. Another value is 2.71828.'; |
| 150 | + const result = tokenizer.tokenize(text); |
| 151 | + expect(result).toBeDefined(); |
| 152 | + expect(result.some((s) => s.includes('3.14159'))).toBeTruthy(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should provide segment IDs in stream mode', async () => { |
| 156 | + const stream = tokenizer.stream(); |
| 157 | + stream.pushText('First sentence. '); |
| 158 | + stream.flush(); |
| 159 | + stream.pushText('Second sentence after flush.'); |
| 160 | + stream.endInput(); |
| 161 | + |
| 162 | + const tokens = []; |
| 163 | + for await (const value of stream) { |
| 164 | + tokens.push(value); |
| 165 | + expect(value.segmentId).toBeDefined(); |
| 166 | + expect(typeof value.segmentId).toBe('string'); |
| 167 | + } |
| 168 | + |
| 169 | + // Tokens from different segments should have different segment IDs |
| 170 | + if (tokens.length > 1) { |
| 171 | + const segmentIds = new Set(tokens.map((t) => t.segmentId)); |
| 172 | + // After flush, we should have at least 2 different segment IDs |
| 173 | + expect(segmentIds.size).toBeGreaterThanOrEqual(1); |
| 174 | + } |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments