feat: split text support multiple languages #593

pr675
Hufe921 2 years ago
parent bcf311ff19
commit 1cb07af75e

@ -107,6 +107,13 @@ export function getUUID(): string {
export function splitText(text: string): string[] { export function splitText(text: string): string[] {
const data: string[] = [] const data: string[] = []
if (Intl.Segmenter) {
const segmenter = new Intl.Segmenter()
const segments = segmenter.segment(text)
for (const { segment } of segments) {
data.push(segment)
}
} else {
const symbolMap = new Map<number, string>() const symbolMap = new Map<number, string>()
for (const match of text.matchAll(UNICODE_SYMBOL_REG)) { for (const match of text.matchAll(UNICODE_SYMBOL_REG)) {
symbolMap.set(match.index!, match[0]) symbolMap.set(match.index!, match[0])
@ -122,6 +129,7 @@ export function splitText(text: string): string[] {
t++ t++
} }
} }
}
return data return data
} }

Loading…
Cancel
Save