Skip to content

Instantly share code, notes, and snippets.

@hmit208
Last active March 2, 2023 09:44
Show Gist options
  • Save hmit208/eb319c7497fac01e0d2d5ada9d65511b to your computer and use it in GitHub Desktop.
Save hmit208/eb319c7497fac01e0d2d5ada9d65511b to your computer and use it in GitHub Desktop.
Remove accents for Vietnamese
package main
import (
"fmt"
"regexp"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
// RemoveAccent function
func RemoveAccent(s string) string {
s = strings.ReplaceAll(s, "Đ", "D") // Don't know why Đ|đ cannot be transformed to D|d so we need to use this
s = strings.ReplaceAll(s, "đ", "d")
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
res, _, _ := transform.String(t, s)
return res
}
func main() {
fmt.Println(RemoveAccent(`à|á|ạ|ã|ả|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ
è|ẻ|ẽ|é|ẹ|ê|ề|ể|ễ|ế|ệ
ì|ỉ|ĩ|í|ị
ù|ủ|ũ|ú|ụ|ư|ừ|ử|ữ|ứ|ự
ỳ|ỷ|ỹ|ý|ỵ
ò|ỏ|õ|ó|ọ|ô|ồ|ổ|ỗ|ố|ộ|ơ|ờ|ở|ỡ|ớ|ợ
Đ|đ
`))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment