Last active
August 10, 2022 07:09
-
-
Save korakot/451926b68bc0baab5c9ddb1e28448289 to your computer and use it in GitHub Desktop.
Thai Word Segmentation in JavaScript using V8 Break Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* gen_words(text){ | |
const it = new Intl.v8BreakIterator(['th']) | |
it.adoptText(text) | |
let start = it.first() | |
while (true) { | |
let end = it.next() | |
if (end === -1) break | |
yield text.slice(start, end); | |
start = end | |
} | |
} | |
// Usage: | |
// segment('สวัสดีครับ สบายดีไหม') | |
// ['สวัสดี', 'ครับ', ' ', 'สบาย', 'ดี', 'ไหม'] | |
function segment(text){ | |
return [...gen_words(text)] | |
} | |
// Internally, it uses ICU | |
// https://chromium.googlesource.com/external/v8-i18n/+/refs/heads/master/src/break-iterator.cc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment