Linguistically correct Python text rendering

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Opstad

    Linguistically correct Python text rendering

    I have a question about text rendering I'm hoping someone here can
    answer. Is there a way of doing linguistically correct rendering of
    Unicode strings in Python? In simple cases like Latin or Japanese I can
    just print the string and see the correct results. However, I don't know
    how to get Python to do the right thing for writing systems which
    require contextual processing.

    For example, let's say I create this Unicode string in Arabic:

    string1 = u"\u0644\u062e\ u0645" # lam khah meem

    If I print string1, I get just the characters I specified above, which
    is not correct rendering behavior for Arabic. In the simple case, I
    should get an output string where the order is reversed, and the codes
    have been changed into their contextually correct forms: initial lam
    (\ufedf), medial khah (\ufea8) and final meem (\ufee2). Optionally, I
    could even ask for a single ligature combining all three of these;
    Unicode even encodes this at \ufd85.

    The situation is even more complicated for writing systems like
    Devanagari (used in Hindi and Marathi). In this case there are rules for
    ligatures (called "conjuncts" ) which are linguistically required, but
    unencoded by Unicode. Technology was developed 15 years ago to deal with
    this: fonts contain tables of extra information allowing access to the
    correct conjunct glyphs at rendering time, even if they're unencoded.

    Apple has software that deals with correctly rendering text in cases
    like this (ATSUI); Microsoft does as well. IBM provides the ICU software
    classes for this kind of rendering, and I believe FreeType has made a
    start on dealing with AAT and OpenType fonts as well. So my question is
    this: how do we get this functionality integrated into Python? I'd love
    to be able to print any Unicode string and have it come out
    linguistically correctly, even if I have to do a little extra formatting
    (e.g. something like adding a new string-formatting conversion character
    %t for typographically rich output).

    Any ideas?

    Dave Opstad
  • Josiah Carlson

    #2
    Re: Linguistically correct Python text rendering

    > Any ideas?

    Perhaps using a GUI call that hooks into Windows or MacOS would be
    sufficient for you. I don't know if tkinter or wxPython can handle it,
    but I would imagine that standard Windows gui calls wouldn't have a
    problem. Check out the pythonwin GUI extensions:


    - Josiah

    Comment

    • Mike Maxwell

      #3
      Re: Linguistically correct Python text rendering

      David Opstad wrote:[color=blue]
      > my question is this: how do we get this functionality integrated into
      > Python? I'd love to be able to print any Unicode string and have it
      > come out linguistically correctly[/color]

      Isn't this a function of whatever app you're running Python code inside (a
      terminal or something)? E.g. could you take the output of your Python
      program as a file and display it in Yudit or a Pango app?

      Mike Maxwell




      Comment

      • David Opstad

        #4
        Re: Linguistically correct Python text rendering

        In article <mailman.33.107 7633885.8594.py thon-list@python.org >,
        "Mike Maxwell" <maxwell@ldc.up enn.edu> wrote:
        [color=blue]
        > Isn't this a function of whatever app you're running Python code inside (a
        > terminal or something)? E.g. could you take the output of your Python
        > program as a file and display it in Yudit or a Pango app?[/color]

        In an interactive Python session on the Mac, the terminal window can
        display Asian or accented Latin with no problem, so that:
        [color=blue][color=green][color=darkred]
        >>> firstCJKChar = u"\u4e00"
        >>> print firstCJKChar.en code('utf-8')[/color][/color][/color]

        gives the correct output, the Chinese character "yi". (The terminal's
        defaults are for UTF-8 text display)

        All I'm wondering is whether this odd asymmetry (between parts of
        Unicode that display correctly with no further work and parts of Unicode
        that need more active processing) is something that could be addressed
        by adding more sophistication to Python's own output formatting. The
        alternative is, as you suggest, to export Unicode text and open it with
        another application, but I want Python to shine for all languages by
        default!

        Dave

        Comment

        • Michael Hudson

          #5
          Re: Linguistically correct Python text rendering

          David Opstad <opstad@batnet. com> writes:
          [color=blue]
          > In article <mailman.33.107 7633885.8594.py thon-list@python.org >,
          > "Mike Maxwell" <maxwell@ldc.up enn.edu> wrote:
          >[color=green]
          > > Isn't this a function of whatever app you're running Python code inside (a
          > > terminal or something)? E.g. could you take the output of your Python
          > > program as a file and display it in Yudit or a Pango app?[/color]
          >
          > In an interactive Python session on the Mac, the terminal window can
          > display Asian or accented Latin with no problem, so that:
          >[color=green][color=darkred]
          > >>> firstCJKChar = u"\u4e00"
          > >>> print firstCJKChar.en code('utf-8')[/color][/color]
          >
          > gives the correct output, the Chinese character "yi". (The terminal's
          > defaults are for UTF-8 text display)[/color]

          But it seems to be impossible to programmaticall y determine which
          encoding the terminal being printed to at a given moment is using (and
          the user can fiddle this at run time). If I'm wrong about this, I'd
          like to know.
          [color=blue]
          > All I'm wondering is whether this odd asymmetry (between parts of
          > Unicode that display correctly with no further work and parts of Unicode
          > that need more active processing) is something that could be addressed
          > by adding more sophistication to Python's own output formatting.[/color]

          I don't think so. Depends a bit what you mean by "Python's own output
          formatting". Since, 2.3 Python if sys.stdout is a terminal it
          attempts to determine the encoding in use via the
          "locale.nl_lang info(locale.COD ESET)" approach, but whether this
          actually works seems to be a bit random. It certainly isn't going to
          work on Mac OS X, which AFAICT ignores locales as much as the ISO C
          standard lets it get away with.

          What more would you have us do?

          If you're using some other means to display Unicode text, it's up to
          that means to ensure it gets displayed correctly. For PyObjC, It Just
          Works, I think.

          Cheers,
          mwh

          --
          Academic politics is the most vicious and bitter form of politics,
          because the stakes are so low. -- Wallace Sayre

          Comment

          • David Opstad

            #6
            Re: Linguistically correct Python text rendering

            In article <m3u11ffk1f.fsf @pc150.maths.br is.ac.uk>,
            Michael Hudson <mwh@python.net > wrote:
            [color=blue]
            > But it seems to be impossible to programmaticall y determine which
            > encoding the terminal being printed to at a given moment is using (and
            > the user can fiddle this at run time). If I'm wrong about this, I'd
            > like to know.[/color]

            The encoding issue is peripheral to my point; sorry if I wasn't clearer
            in my original message. It doesn't matter what the encoding is. The main
            issue is that for some writing systems (e.g. Arabic) simply outputting
            the characters in a Unicode string, irrespective of encoding, will
            produce garbled results.
            [color=blue]
            > What more would you have us do?[/color]

            Well, for those writing systems whose presentation forms are included in
            Unicode, how about a further processing step? So that at a minimum, if I
            start with an Arabic string like "abc" I can get out an Arabic string
            like "CBA" where bidi reordering has happened, and contextual
            substitution has been done. Then, outputting the processed Unicode
            string using stdout will work without further intervention (assuming a
            font for the writing system is present, of course).

            It's probably irrational of me, I admit, but I'd love to see Python
            correctly render *any* Unicode string, not just the subsets requiring no
            reordering or contextual processing.

            Dave

            Comment

            • Michael Hudson

              #7
              Re: Linguistically correct Python text rendering

              David Opstad <opstad@batnet. com> writes:
              [color=blue]
              > In article <m3u11ffk1f.fsf @pc150.maths.br is.ac.uk>,
              > Michael Hudson <mwh@python.net > wrote:
              >[color=green]
              > > But it seems to be impossible to programmaticall y determine which
              > > encoding the terminal being printed to at a given moment is using (and
              > > the user can fiddle this at run time). If I'm wrong about this, I'd
              > > like to know.[/color]
              >
              > The encoding issue is peripheral to my point; sorry if I wasn't clearer
              > in my original message. It doesn't matter what the encoding is. The main
              > issue is that for some writing systems (e.g. Arabic) simply outputting
              > the characters in a Unicode string, irrespective of encoding, will
              > produce garbled results.
              >[color=green]
              > > What more would you have us do?[/color]
              >
              > Well, for those writing systems whose presentation forms are included in
              > Unicode, how about a further processing step? So that at a minimum, if I
              > start with an Arabic string like "abc" I can get out an Arabic string
              > like "CBA" where bidi reordering has happened, and contextual
              > substitution has been done. Then, outputting the processed Unicode
              > string using stdout will work without further intervention (assuming a
              > font for the writing system is present, of course).[/color]

              Ah, OK. You are now officially beyond my level of expertise :-) You
              might want to talk to the i18n-sig.

              This sounds very much like the sort of thing that could/should be
              developed externally to Python and then perhaps folded in later, a la
              CJKCodecs.
              [color=blue]
              > It's probably irrational of me, I admit, but I'd love to see Python
              > correctly render *any* Unicode string, not just the subsets requiring no
              > reordering or contextual processing.[/color]

              I still think "render" is probably the wrong word to use here, though.

              Cheers,
              mwh

              --
              In short, just business as usual in the wacky world of floating
              point <wink>. -- Tim Peters, comp.lang.pytho n

              Comment

              Working...