Remove all diacritic marks from Latin base characters
(txt)
| 43 | |
| 44 | # tag::SHAVE_MARKS_LATIN[] |
| 45 | def shave_marks_latin(txt): |
| 46 | """Remove all diacritic marks from Latin base characters""" |
| 47 | norm_txt = unicodedata.normalize('NFD', txt) # <1> |
| 48 | latin_base = False |
| 49 | preserve = [] |
| 50 | for c in norm_txt: |
| 51 | if unicodedata.combining(c) and latin_base: # <2> |
| 52 | continue # ignore diacritic on Latin base char |
| 53 | preserve.append(c) # <3> |
| 54 | # if it isn't a combining char, it's a new base char |
| 55 | if not unicodedata.combining(c): # <4> |
| 56 | latin_base = c in string.ascii_letters |
| 57 | shaved = ''.join(preserve) |
| 58 | return unicodedata.normalize('NFC', shaved) # <5> |
| 59 | # end::SHAVE_MARKS_LATIN[] |
| 60 | |
| 61 | # tag::ASCIIZE[] |