Skip to content

Instantly share code, notes, and snippets.

@thuandt
Created August 22, 2012 03:07
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save thuandt/3421905 to your computer and use it in GitHub Desktop.
Save thuandt/3421905 to your computer and use it in GitHub Desktop.
Chuyển đổi từ Tiếng Việt có dấu sang Tiếng Việt không dấu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Chương trình chuyển đổi từ Tiếng Việt có dấu sang Tiếng Việt không dấu
Chỉnh sửa từ mã nguồn của anh NamNT
http://www.vithon.org/2009/06/14/x%E1%BB%AD-ly-ti%E1%BA%BFng-vi%E1%BB%87t-trong-python
"""
import re
INTAB = "ạảãàáâậầấẩẫăắằặẳẵóòọõỏôộổỗồốơờớợởỡéèẻẹẽêếềệểễúùụủũưựữửừứíìịỉĩýỳỷỵỹđẠẢÃÀÁÂẬẦẤẨẪĂẮẰẶẲẴÓÒỌÕỎÔỘỔỖỒỐƠỜỚỢỞỠÉÈẺẸẼÊẾỀỆỂỄÚÙỤỦŨƯỰỮỬỪỨÍÌỊỈĨÝỲỶỴỸĐ"
INTAB = [ch.encode('utf8') for ch in unicode(INTAB, 'utf8')]
OUTTAB = "a" * 17 + "o" * 17 + "e" * 11 + "u" * 11 + "i" * 5 + "y" * 5 + "d" + \
"A" * 17 + "O" * 17 + "E" * 11 + "U" * 11 + "I" * 5 + "Y" * 5 + "D"
r = re.compile("|".join(INTAB))
replaces_dict = dict(zip(INTAB, OUTTAB))
def no_accent_vietnamese(utf8_str):
return r.sub(lambda m: replaces_dict[m.group(0)], utf8_str)
if __name__ == '__main__':
print no_accent_vietnamese("Việt Nam Đất Nước Con Người")
print no_accent_vietnamese("Welcome to Vietnam !")
print no_accent_vietnamese("VIỆT NAM ĐẤT NƯỚC CON NGƯỜI")
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Chương trình chuyển đổi từ Tiếng Việt có dấu sang Tiếng Việt không dấu
"""
import re
def no_accent_vietnamese(s):
s = s.decode('utf-8')
s = re.sub(u'[àáạảãâầấậẩẫăằắặẳẵ]', 'a', s)
s = re.sub(u'[ÀÁẠẢÃĂẰẮẶẲẴÂẦẤẬẨẪ]', 'A', s)
s = re.sub(u'èéẹẻẽêềếệểễ', 'e', s)
s = re.sub(u'ÈÉẸẺẼÊỀẾỆỂỄ', 'E', s)
s = re.sub(u'òóọỏõôồốộổỗơờớợởỡ', 'o', s)
s = re.sub(u'ÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠ', 'O', s)
s = re.sub(u'ìíịỉĩ', 'i', s)
s = re.sub(u'ÌÍỊỈĨ', 'I', s)
s = re.sub(u'ùúụủũưừứựửữ', 'u', s)
s = re.sub(u'ƯỪỨỰỬỮÙÚỤỦŨ', 'U', s)
s = re.sub(u'ỳýỵỷỹ', 'y', s)
s = re.sub(u'ỲÝỴỶỸ', 'Y', s)
s = re.sub(u'Đ', 'D', s)
s = re.sub(u'đ', 'd', s)
return s.encode('utf-8')
if __name__ == '__main__':
print no_accent_vietnamese("Việt Nam Đất Nước Con Người")
print no_accent_vietnamese("Welcome to Vietnam !")
print no_accent_vietnamese("VIỆT NAM ĐẤT NƯỚC CON NGƯỜI")
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Chương trình chuyển đổi từ Tiếng Việt có dấu sang Tiếng Việt không dấu
"""
import re
import unicodedata
def no_accent_vietnamese(s):
s = s.decode('utf-8')
s = re.sub(u'Đ', 'D', s)
s = re.sub(u'đ', 'd', s)
return unicodedata.normalize('NFKD', unicode(s)).encode('ASCII', 'ignore')
if __name__ == '__main__':
print no_accent_vietnamese("Việt Nam Đất Nước Con Người")
print no_accent_vietnamese("Welcome to Vietnam !")
print no_accent_vietnamese("VIỆT NAM ĐẤT NƯỚC CON NGƯỜI")
@doxuanthang
Copy link

miss [ and ]

@tienhieuD
Copy link

nice, thank you 😍

@likelovez
Copy link

Hi, you need to replace s = s.decode('utf-8') by s = s.encode('utf-8').decode('utf-8') to avoiding some exception !!

@h6009
Copy link

h6009 commented Feb 18, 2019

from unidecode import unidecode; unidecode(u'Dương Tiến Thuận')

@behitek
Copy link

behitek commented Apr 11, 2019

Cái thứ 2 regex sai rồi ạ.

def no_accent_vietnamese(s):
    s = s.lower()
    s = re.sub('[áàảãạăắằẳẵặâấầẩẫậ]', 'a', s)
    s = re.sub('[éèẻẽẹêếềểễệ]', 'e', s)
    s = re.sub('[óòỏõọôốồổỗộơớờởỡợ]', 'o', s)
    s = re.sub('[íìỉĩị]', 'i', s)
    s = re.sub('[úùủũụưứừửữự]', 'u', s)
    s = re.sub('[ýỳỷỹỵ]', 'y', s)
    s = re.sub('đ', 'd', s)
    return s

@trieuhaivo
Copy link

trieuhaivo commented Jul 10, 2019

Code đúng phải là như này:

import re


def no_accent_vietnamese(s):
    s = re.sub(r'[àáạảãâầấậẩẫăằắặẳẵ]', 'a', s)
    s = re.sub(r'[ÀÁẠẢÃĂẰẮẶẲẴÂẦẤẬẨẪ]', 'A', s)
    s = re.sub(r'[èéẹẻẽêềếệểễ]', 'e', s)
    s = re.sub(r'[ÈÉẸẺẼÊỀẾỆỂỄ]', 'E', s)
    s = re.sub(r'[òóọỏõôồốộổỗơờớợởỡ]', 'o', s)
    s = re.sub(r'[ÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠ]', 'O', s)
    s = re.sub(r'[ìíịỉĩ]', 'i', s)
    s = re.sub(r'[ÌÍỊỈĨ]', 'I', s)
    s = re.sub(r'[ùúụủũưừứựửữ]', 'u', s)
    s = re.sub(r'[ƯỪỨỰỬỮÙÚỤỦŨ]', 'U', s)
    s = re.sub(r'[ỳýỵỷỹ]', 'y', s)
    s = re.sub(r'[ỲÝỴỶỸ]', 'Y', s)
    s = re.sub(r'[Đ]', 'D', s)
    s = re.sub(r'[đ]', 'd', s)
    return s

if __name__ == '__main__':
    print(no_accent_vietnamese("Việt Nam Đất Nước Con Người"))
    print(no_accent_vietnamese("Welcome to Vietnam !"))
    print(no_accent_vietnamese("VIỆT NAM ĐẤT NƯỚC CON NGƯỜI"))

# Output
# Viet Nam Dat Nuoc Con Nguoi
# Welcome to Vietnam !
# VIET NAM DAT NUOC CON NGUOI

Hoặc có thể cài và sử dụng thư viện unidecode:

pip install unidecode
from unidecode import unidecode

print(unidecode("Việt Nam Đất Nước Con Người"))
print(unidecode("Welcome to Vietnam !"))
print(unidecode("VIỆT NAM ĐẤT NƯỚC CON NGƯỜI"))

# Output
# Viet Nam Dat Nuoc Con Nguoi
# Welcome to Vietnam !
# VIET NAM DAT NUOC CON NGUOI

@thuandt
Copy link
Author

thuandt commented Jul 11, 2019

Mấy script chơi chơi từ cả 7-8 năm trước mà giờ ae vẫn có người cần à :D

@kiemrong08
Copy link

quá cần luôn =))

@trongvanhpkt99
Copy link

em muốn hỏi tí là kiểu em muốn làm ngược lại, kiểu từ 1 chữ ko có dấu ví dụ như cho, sinh ra các trường hợp có thể thêm dấu của nó là chò, chó, chỏ, chọ, chõ thì làm như thế nào được ạ :v

@behitek
Copy link

behitek commented Aug 18, 2020

em muốn hỏi tí là kiểu em muốn làm ngược lại, kiểu từ 1 chữ ko có dấu ví dụ như cho, sinh ra các trường hợp có thể thêm dấu của nó là chò, chó, chỏ, chọ, chõ thì làm như thế nào được ạ :v

Em tham khảo bài viết này có cái em cần: https://nguyenvanhieu.vn/thuat-toan-beam-search/#tim-tat-ca-cac-cach-danh-dau-cau

@hrm209
Copy link

hrm209 commented Dec 28, 2020

Cho e hỏi có cách nào chuyển từ kg dấu khi gõ sang có dấu kg ạ (kg bật gõ tiếng việt) ví dụ : thuongfw -> thường

@Hoainam25699
Copy link

Hoainam25699 commented May 14, 2021

có bạn nhé @hrm209, bạn có thể sử dụng thư viện bogo :)

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