-
Notifications
You must be signed in to change notification settings - Fork 3
/
fullwidth.go
50 lines (40 loc) · 1.4 KB
/
fullwidth.go
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
package correct
import (
"regexp"
)
var (
fullwidthMaps = map[string]string{
",": ",",
".": "。",
";": ";",
":": ":",
"!": "!",
"?": "?",
"~": "~",
// "(": "(",
// ")": ")",
}
spcialPunctuations = `[.:]`
normalPunctuations = `[,\!\?~]`
punctuationWithLeftCJKRe = regexp.MustCompile(normalPunctuations + `[` + cjk + `]+`)
punctuationWithRightCJKRe = regexp.MustCompile(`[` + cjk + `]+` + normalPunctuations)
punctuationWithSpeicalCJKRe = regexp.MustCompile(`[` + cjk + `]+` + spcialPunctuations + `[` + cjk + `]+`)
punctuationWithSpeicalLastCJKRe = regexp.MustCompile(`[` + cjk + `]+` + spcialPunctuations + "$")
punctuationsRe = regexp.MustCompile(`(` + spcialPunctuations + `|` + normalPunctuations + `)`)
)
// fullwidth correct punctuations near the CJK chars
func fullwidth(text string) (out string) {
out = text
out = punctuationWithLeftCJKRe.ReplaceAllStringFunc(out, fullwidthReplacePart)
out = punctuationWithRightCJKRe.ReplaceAllStringFunc(out, fullwidthReplacePart)
out = punctuationWithSpeicalCJKRe.ReplaceAllStringFunc(out, fullwidthReplacePart)
out = punctuationWithSpeicalLastCJKRe.ReplaceAllStringFunc(out, fullwidthReplacePart)
return
}
func fullwidthReplacePart(part string) string {
part = punctuationsRe.ReplaceAllStringFunc(part, func(str string) string {
str = fullwidthMaps[str]
return str
})
return part
}