feat: split text support multiple languages #593
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user