Skip to content

Instantly share code, notes, and snippets.

@J2TEAM
Forked from cinoss/remove_accents.py
Created August 31, 2016 17:11
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save J2TEAM/9992744f15187ba51d46aecab21fd469 to your computer and use it in GitHub Desktop.
Save J2TEAM/9992744f15187ba51d46aecab21fd469 to your computer and use it in GitHub Desktop.
Remove Vietnamese Accents - Xoá dấu tiếng việt in Python
s1 = u'ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝàáâãèéêìíòóôõùúýĂăĐđĨĩŨũƠơƯưẠạẢảẤấẦầẨẩẪẫẬậẮắẰằẲẳẴẵẶặẸẹẺẻẼẽẾếỀềỂểỄễỆệỈỉỊịỌọỎỏỐốỒồỔổỖỗỘộỚớỜờỞởỠỡỢợỤụỦủỨứỪừỬửỮữỰựỲỳỴỵỶỷỸỹ'
s0 = u'AAAAEEEIIOOOOUUYaaaaeeeiioooouuyAaDdIiUuOoUuAaAaAaAaAaAaAaAaAaAaAaAaEeEeEeEeEeEeEeEeIiIiOoOoOoOoOoOoOoOoOoOoOoOoUuUuUuUuUuUuUuYyYyYyYy'
def remove_accents(input_str):
s = ''
print input_str.encode('utf-8')
for c in input_str:
if c in s1:
s += s0[s1.index(c)]
else:
s += c
return s
@lacls
Copy link

lacls commented Apr 7, 2022

Error case:
no_accent_vietnamese("Nguyễn Võ Tấn Đạt")
Output: 'Nguyẽn Võ Tán Dạt'

@maycuatroi
Copy link

new method:

pip install unidecode
import unidecode
accented_string = u'Việt Nam đất nước con người'
# accented_string is of type 'unicode'

unaccented_string = unidecode.unidecode(accented_string)
print(unaccented_string)
# output: Viet Nam dat nuoc con nguoi

@ejinguyen
Copy link

mình cũng đang gặp 1 case như bên dưới:
"Phú Mỹ Hưng"
"Phú Mỹ Hưng"

2 từ trên trông giống nhau nhưng khi encode thì không giống nhau!
Danh sách ký tự lên mở rộng cho nhiều bảng mã khác!

@phineas-pta
Copy link

mình có làm 1 phiên bản khác hoàn thiện hơn và xử lí dc các trường hợp không thành công ở trên:

https://gist.github.com/phineas-pta/05cad38a29fea000ab6d9e13a6f7e623

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment