Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does unicodedata.normalize(form, unistr) work?

Tags:

On the API doc, http://docs.python.org/2/library/unicodedata.html#unicodedata.normalize. It says

Return the normal form form for the Unicode string unistr. Valid values for form are ‘NFC’, ‘NFKC’, ‘NFD’, and ‘NFKD’.`

The documentation is rather vague, can someone explain the valid values with some examples?

like image 863
alvas Avatar asked Feb 04 '13 07:02

alvas


People also ask

What does Unicodedata normalize do?

unicodedata.normalize(form, unistr) This function returns the normal form for the Unicode string unistr.

What is NFC normalization?

Normalization Form Canonical Decomposition. Characters are decomposed by canonical equivalence, and multiple combining characters are arranged in a specific order. NFC. Normalization Form Canonical Composition. Characters are decomposed and then recomposed by canonical equivalence.


1 Answers

I find the documentation pretty clear, but here are a few code examples:

from unicodedata import normalize  print '%r' % normalize('NFD', u'\u00C7')  # decompose: convert Ç to "C + ̧" print '%r' % normalize('NFC', u'C\u0327') # compose: convert "C + ̧" to Ç 

Both 'D' (=decompose) forms convert a single combined character (like ä) into two characters (a + two dots). Both 'C' (=compose) forms do the reverse.

The two "K" forms are used to convert characters added to Unicode for compatibility purposes. For example, to support software that cannot draw circles around symbols, there is a set of "circled numbers", like ① (unicode number 2460). When we apply the canonical decomposition (NFD) to it, it doesn't do anything:

print '%r' % normalize('NFD', u'\u2460')     # u'\u2460' 

However, the compatibility decomposition (NFKD) will return the corresponding "compatible" character:

print '%r' % normalize('NFKD', u'\u2460')    # 1 

See http://en.wikipedia.org/wiki/Unicode_equivalence for more details.

like image 78
georg Avatar answered Sep 20 '22 04:09

georg