unicodedata . normalize (NFD - NFC) inconsistency

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christos TZOTZIOY Georgiou

    #1

    unicodedata . normalize (NFD - NFC) inconsistency

    I found at least one case where decombining and recombining a unicode
    character does not result in the same character (see at end).

    I have no extensive knowledge about Unicode, yet I believe that this
    must be a problem of the Unicode 3.2 specification and not Python's.
    However, I haven't found out how the decomp_data (in unicodedata_db. h)
    is built, and neither did I find much more info about the specifics of
    Unicode 3.2. I thought about posting here; anyone more knowing could
    give it a look.

    If we find out that it's a problem with Python, I'll open a bug report
    (and volunteer work).

    *** Example ***
    [color=blue][color=green][color=darkred]
    >>> import unicodedata as ud
    >>> def report(utext):[/color][/color][/color]
    for uchar in utext:
    print ord(uchar), ud.name(uchar)

    [color=blue][color=green][color=darkred]
    >>> u1=u'\N{greek small letter alpha with oxia}'
    >>> report(u1)[/color][/color][/color]
    8049 GREEK SMALL LETTER ALPHA WITH OXIA[color=blue][color=green][color=darkred]
    >>> u2=ud.normalize ('NFD', u1)
    >>> report(u2)[/color][/color][/color]
    945 GREEK SMALL LETTER ALPHA
    769 COMBINING ACUTE ACCENT[color=blue][color=green][color=darkred]
    >>> u3=ud.normalize ('NFC', u2)
    >>> report(u3)[/color][/color][/color]
    940 GREEK SMALL LETTER ALPHA WITH TONOS[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    *** End of Example ***

    I can understand this confusion; if, as I have found, there is no
    COMBINING GREEK TONOS or COMBINING TONOS ACCENT in the Unicode table,
    decombining, one has to use the 'oxeia' (acute) accent...
    --
    TZOTZIOY, I speak England very best,
    "Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
  • Martin v. Löwis

    #2
    Re: unicodedata . normalize (NFD - NFC) inconsistency

    Christos TZOTZIOY Georgiou wrote:[color=blue]
    > I have no extensive knowledge about Unicode, yet I believe that this
    > must be a problem of the Unicode 3.2 specification and not Python's.[/color]

    Without checking the details: very well possible. Could this be
    an instance of python.org/sf/1054943 ?

    Regards,
    Martin

    Comment

    • Brion Vibber

      #3
      Re: unicodedata . normalize (NFD - NFC) inconsistency

      Christos TZOTZIOY Georgiou wrote:[color=blue]
      > I found at least one case where decombining and recombining a unicode
      > character does not result in the same character (see at end).
      >
      > I have no extensive knowledge about Unicode, yet I believe that this
      > must be a problem of the Unicode 3.2 specification and not Python's.[/color]

      I've been spending some time lately writing a normalizer (in PHP of all
      things -- yeesh!), and yes Unicode is a scary world. :) Although it may
      seem counterintuitiv e, it is in fact perfectly legitimate for a
      character not to be its own canonical composition.
      [color=blue][color=green][color=darkred]
      >>>>u1=u'\N{gre ek small letter alpha with oxia}'
      >>>>report(u1 )[/color][/color]
      >
      > 8049 GREEK SMALL LETTER ALPHA WITH OXIA[/color]

      This character is a "singleton decomposition". It decomposes into GREEK
      SMALL LETTER ALPHA WITH TONOS, which further decomposes into GREEK SMALL
      LETTER ALPHA and a COMBINING ACUTE ACCENT.

      It is by definition not normalized, so when you normalize it to form C
      it will turn into GREEK SMALL LETTER ALPHA WITH TONOS; there is no way
      to get "back" to the original character in a normalized string. For some
      more info see:

      [color=blue][color=green][color=darkred]
      >>>>u2=ud.norma lize('NFD', u1)
      >>>>report(u2 )[/color][/color]
      >
      > 945 GREEK SMALL LETTER ALPHA
      > 769 COMBINING ACUTE ACCENT
      >[color=green][color=darkred]
      >>>>u3=ud.norma lize('NFC', u2)
      >>>>report(u3 )[/color][/color]
      >
      > 940 GREEK SMALL LETTER ALPHA WITH TONOS[/color]

      You should get this same result directly for ud.normalize('N FC', u1).
      Converting directly to NFC should always give the same result as
      converting to NFD and then NFC. Either will give you back the string you
      started with if and only if it's already normalized to form C.

      -- brion vibber (brion @ pobox.com)

      Comment

      • Christos TZOTZIOY Georgiou

        #4
        Re: unicodedata . normalize (NFD - NFC) inconsistency

        On Mon, 08 Nov 2004 17:40:47 -0800, rumours say that Brion Vibber
        <brion@pobox.co m> might have written:
        [color=blue]
        >I've been spending some time lately writing a normalizer (in PHP of all
        >things -- yeesh!), and yes Unicode is a scary world. :)[/color]

        ....
        [color=blue]
        >http://www.unicode.org/unicode/repor...ion_List_Table[/color]

        Thanks for the pointer, very informative, explaining why the observed
        behaviour is well inside the definition of Unicode. Thanks go to Martin
        also for taking a look at this.
        --
        TZOTZIOY, I speak England very best,
        "Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek

        Comment

        Working...