-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
77 lines (67 loc) · 1.98 KB
/
Copy pathutils.go
File metadata and controls
77 lines (67 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package semchunk
import (
"regexp"
"unicode"
)
// LookbehindSplit splits a string at a given splitter, but only if it is preceded by a given string
// This is a helper function to emulate lookbehind in regex
func LookbehindSplit(text string, precededBy string, splitter string) []string {
escapedPreceder := regexp.QuoteMeta(precededBy)
escapedSplitter := regexp.QuoteMeta(splitter)
re := regexp.MustCompile(escapedPreceder + escapedSplitter)
matches := re.FindAllStringIndex(text, -1)
parts := make([]string, 0)
lastIndex := 0
for _, match := range matches {
parts = append(parts, text[lastIndex:match[0]]+precededBy)
lastIndex = match[1]
}
parts = append(parts, text[lastIndex:])
return parts
}
// IsChinese checks if a string is Chinese
func IsChinese(text string) bool {
if len(text) == 0 {
return false
}
chineseCount := 0
totalCount := 0
for _, r := range text {
// Skip whitespace and punctuation
if unicode.IsSpace(r) || unicode.IsPunct(r) {
continue
}
totalCount++
// Check if character is in Chinese Unicode ranges
// CJK Unified Ideographs (4E00-9FFF)
// CJK Unified Ideographs Extension A (3400-4DBF)
// CJK Unified Ideographs Extension B (20000-2A6DF)
// CJK Unified Ideographs Extension C (2A700-2B73F)
// CJK Unified Ideographs Extension D (2B740-2B81F)
// CJK Unified Ideographs Extension E (2B820-2CEAF)
if (r >= 0x4E00 && r <= 0x9FFF) ||
(r >= 0x3400 && r <= 0x4DBF) ||
(r >= 0x20000 && r <= 0x2A6DF) ||
(r >= 0x2A700 && r <= 0x2B73F) ||
(r >= 0x2B740 && r <= 0x2B81F) ||
(r >= 0x2B820 && r <= 0x2CEAF) {
chineseCount++
}
}
// Consider text as Chinese if more than 50% of characters are Chinese
return totalCount > 0 && float64(chineseCount)/float64(totalCount) > 0.4
}
func GuessIsChinese(text string, n int) bool {
if n > len(text) || n <= 0 {
n = len(text)
}
return IsChinese(text[:n])
}
func ContainsSpace(text string) bool {
for _, r := range text {
if unicode.IsSpace(r) {
return true
}
}
return false
}