Skip to content

Instantly share code, notes, and snippets.

@hoangtuan151
Last active September 6, 2018 10:21
Show Gist options
  • Save hoangtuan151/872092cfda76c4d556df06ed2fce2e18 to your computer and use it in GitHub Desktop.
Save hoangtuan151/872092cfda76c4d556df06ed2fce2e18 to your computer and use it in GitHub Desktop.

#1. Chuyển chuỗi tiếng Việt có dấu thành ko dấu

Lang: Python

def chuyen_tieng_viet_khong_dau(s):
    s = s
    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)
    s = re.sub('-', ' ', s)
    return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment