Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add `/tokenize` API endpoint (#2162)

* Add tokenize API endpoint to native messaging interface

- Add new 'tokenize' action to YomitanApi for text segmentation
- Uses internal parseText with fixed scanLength of 32 characters
- Returns full parsing results with readings and dictionary info
- Useful for external applications needing Japanese text tokenization

* Make scanLength configurable in tokenize endpoint

- Add scanLength as optional parameter (defaults to 10)
- Allows callers to customize tokenization granularity
- Shorter lengths for faster processing, longer for compound words

* add decriptive errors, remove default

authored by

Hamish Macpherson and committed by
GitHub
cb34f708 789ea2fc

+25
+20
ext/js/comm/yomitan-api.js
··· 236 236 }; 237 237 break; 238 238 } 239 + case 'tokenize': { 240 + /** @type {import('yomitan-api.js').tokenizeInput} */ 241 + // @ts-expect-error - Allow this to error 242 + const {text, scanLength} = parsedBody; 243 + if (typeof text !== 'string') { 244 + throw new Error('Invalid input for tokenize, expected "text" to be a string but got ' + typeof text); 245 + } 246 + if (typeof scanLength !== 'number') { 247 + throw new Error('Invalid input for tokenize, expected "scanLength" to be a number but got ' + typeof scanLength); 248 + } 249 + const invokeParams = { 250 + text: text, 251 + optionsContext: {index: optionsFull.profileCurrent}, 252 + scanLength: scanLength, 253 + useInternalParser: true, 254 + useMecabParser: false, 255 + }; 256 + result = await this._invoke('parseText', invokeParams); 257 + break; 258 + } 239 259 default: 240 260 statusCode = 400; 241 261 }
+5
types/ext/yomitan-api.d.ts
··· 31 31 includeMedia?: boolean; 32 32 }; 33 33 34 + export type tokenizeInput = { 35 + text: string; 36 + scanLength: number; 37 + }; 38 + 34 39 export type remoteVersionResponse = { 35 40 version: number; 36 41 };