The Unicode::Normalize might be kind of a sledge hammer for the task that you're talking about. "Normalization" covers the equivalences between single-codepoint "complex" characters, e.g. é (U00C9) and concatentations of "component" characters, e.g. e + ́ (U0045 + U0301) -- this also known as character (de)composition. These issues are especially knotty in some languages, and this is what the Unicode::Normalize module is all about. (Note that the result of Normalization often still consists of wide characters.)

You're task seems more like "replace a wide character whenever there is an obvious ascii substitute", which is much simpler; this could apply to various quotes, brackets and other punctuation as well as spaces and hyphens/dashes. (The use of wide-character "smart quotes" seems to be on the rise).

If you have wide-character spaces in a utf8 string that was decoded from HTML, turning them all into ascii spaces is easy:

s/\s/ /g;
(Of course, that will apply to newlines and tabs as well, but with html data, this isn't likely to be a problem.)

As for the various punctuation marks, if you already know which wide characters to expect, just put those into a regex character class:

my $dashes = join '', map { chr() } ( 0xAD, 0x2010 .. 0x2015, 0xFE63, +0xFF0D ); my $squots = join '', map { chr() } ( 0x02BC, 0x2018 .. 0x201B ); my $dquots = join '', map { chr() } ( 0x02EE, 0x201C .. 0x201F ); s/[$dashes]/-/g; s/[$squots]/'/g; s/[$dquots]/"/g;
If you run across any wide characters besides those, you can look them up pretty easily and add to your character classes as needed. Here's a simple script for getting the names of various codepoints, codepoints that match various names, etc.

(update: fixed a typo in the assignment to "$dashes")


In reply to Re: unicode normalization by graff
in thread unicode normalization by mscudder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.