MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / shave_marks_latin

Function shave_marks_latin

04-text-byte/simplify.py:45–58  ·  view source on GitHub ↗

Remove all diacritic marks from Latin base characters

(txt)

Source from the content-addressed store, hash-verified

43
44# tag::SHAVE_MARKS_LATIN[]
45def 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[]

Callers 1

asciizeFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected