How to use JavaScript native API to segment Chinese? Simple use of Intl.Segmenter

Intro

I remember that when I used Python to do word cloud analysis, I used jiebathis python library for Chinese word segmentation.
Today, I saw that there is a native API directly in JavaScript that can complete the function of "word segmentation", so I recorded the usage.

JavaScript word segmentation API Intl.Segmenter

function splitText(locales, text) {
    
    
    console.table(Array.from(new Intl.Segmenter(locales, {
    
     granularity: 'word' }).segment(text)));
}

var locales = 'cn';
var text = '因为我发现其实历史没有变化,技术变了,衣服变了,饮食变了,这都是外壳,里面什么都没变化,还是几千年前那一套,转来转去,该犯的错误还是要犯,该杀的人还是要杀,岳飞会死, 袁崇焕会死, 再过一千年,还是会死。';
splitText(locales, text);

insert image description here

Note the browser compatibility of this API:
insert image description here

reference

Guess you like

Origin blog.csdn.net/wuyujin1997/article/details/130451213