The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Unicode::Collate - use UCA (Unicode Collation Algorithm)

SYNOPSIS

  use Unicode::Collate;

  #construct
  $UCA = Unicode::Collate->new(%tailoring);

  #sort
  @sorted = $UCA->sort(@not_sorted);

  #compare
  $result = $UCA->cmp($a, $b); # returns 1, 0, or -1. 

DESCRIPTION

Constructor and Tailoring

The new method returns a collator object.

   $UCA = Unicode::Collate->new(
      alternate => $alternate,
      backwards => $levelNumber, # or \@levelNumbers
      entry => $element,
      normalization  => $normalization_form,
      ignoreName => qr/$ignoreName/,
      ignoreChar => qr/$ignoreChar/,
      katakana_before_hiragana => $bool,
      level => $collationLevel,
      overrideCJK => \&overrideCJK,
      overrideHangul => \&overrideHangul,
      preprocess => \&preprocess,
      rearrange => \@charList,
      table => $filename,
      undefName => qr/$undefName/,
      undefChar => qr/$undefChar/,
      upper_before_lower => $bool,
   );
   # if %tailoring is false (empty),
   # $UCA should do the default collation.
alternate

-- see 3.2.2 Alternate Weighting, UTR #10.

   alternate => 'shifted', 'blanked', 'non-ignorable', or 'shift-trimmed'.

By default (if specification is omitted), 'shifted' is adopted.

backwards

-- see 3.1.2 French Accents, UTR #10.

     backwards => $levelNumber or \@levelNumbers

Weights in reverse order; ex. level 2 (diacritic ordering) in French. If omitted, forwards at all the levels.

entry

-- see 3.1 Linguistic Features; 3.2.1 File Format, UTR #10.

Overrides a default order or adds a new element

  entry => <<'ENTRIES', # use the UCA file format
00E6 ; [.0861.0020.0002.00E6] [.08B1.0020.0002.00E6] # ligature <ae> as <a e>
0063 0068 ; [.0893.0020.0002.0063]      # "ch" in traditional Spanish
0043 0068 ; [.0893.0020.0008.0043]      # "Ch" in traditional Spanish
ENTRIES
ignoreName
ignoreChar

-- see Completely Ignorable, 3.2.2 Alternate Weighting, UTR #10.

Ignores the entry in the table. If an ignored collation element appears in the string to be collated, it is ignored as if the element had been deleted from there.

E.g. when 'a' and 'e' are ignored, 'element' is equal to 'lament' (or 'lmnt').

level

-- see 4.3 Form a sort key for each string, UTR #10.

Set the maximum level. Any higher levels than the specified one are ignored.

  Level 1: alphabetic ordering
  Level 2: diacritic ordering
  Level 3: case ordering
  Level 4: tie-breaking (e.g. in the case when alternate is 'shifted')

  ex.level => 2,
normalization

-- see 4.1 Normalize each input string, UTR #10.

If specified, strings are normalized before preparation sort keys (the normalization is executed after preprocess).

As a form name, one of the following names must be used.

  'C'  or 'NFC'  for Normalization Form C
  'D'  or 'NFD'  for Normalization Form D
  'KC' or 'NFKC' for Normalization Form KC
  'KD' or 'NFKD' for Normalization Form KD

If omitted, the string is put into Normalization Form D.

If undefined explicitly (as normalization => undef), any normalization is not carried out (this may make tailoring easier if any normalization is not desired).

see CAVEAT.

overrideCJK
overrideHangul

-- see 7.1 Derived Collation Elements, UTR #10.

By default, mapping of CJK Unified Ideographs uses the Unicode codepoint order and Hangul Syllables are decomposed into Hangul Jamo.

The mapping of CJK Unified Ideographs or Hangul Syllables may be overrided.

ex. CJK Unified Ideographs in the JIS codepoint order.

  overrideCJK => sub {
    my $u = shift;               # get unicode codepoint
    my $b = pack('n', $u);       # to UTF-16BE
    my $s = your_unicode_to_sjis_converter($b); # convert
    my $n = unpack('n', $s);     # convert sjis to short
    [ $n, 1, 1 ];                # return collation element
  },

If you want to override the mapping of Hangul Syllables, the Normalization Forms D and KD are not appropriate (they will be decomposed before overriding).

preprocess

-- see 5.1 Preprocessing, UTR #10.

If specified, the coderef is used to preprocess before the formation of sort keys.

ex. dropping English articles, such as "a" or "the". Then, "the pen" is before "a pencil".

     preprocess => sub {
           my $str = shift;
           $str =~ s/\b(?:an?|the)\s+//g;
           $str;
        },
rearrange

-- see 3.1.3 Rearrangement, UTR #10.

Characters that are not coded in logical order and to be rearranged. By default,

    rearrange => [ 0x0E40..0x0E44, 0x0EC0..0x0EC4 ],
table

-- see 3.2 Default Unicode Collation Element Table, UTR #10.

You can use another element table if desired. The table file must be in your lib/Unicode/Collate directory.

By default, the file lib/Unicode/Collate/allkeys.txt is used.

undefName
undefChar

-- see 6.3.4 Reducing the Repertoire, UTR #10.

Undefines the collation element as if it were unassigned in the table. This reduces the size of the table. If an unassigned character appears in the string to be collated, the sort key is made from its codepoint as a single-character collation element, as it is greater than any other assigned collation elements (in the codepoint order among the unassigned characters). But, it'd be better to ignore characters unfamiliar to you and maybe never used.

katakana_before_hiragana
upper_before_lower

-- see 6.6 Case Comparisons; 7.3.1 Tertiary Weight Table, UTR #10.

By default, lowercase is before uppercase and hiragana is before katakana.

If the parameter is true, this is reversed.

Other methods

@sorted = $UCA->sort(@not_sorted)

Sorts a list of strings.

$result = $UCA->cmp($a, $b)

Returns 1 (when $a is greater than $b) or 0 (when $a is equal to $b) or -1 (when $a is lesser than $b).

$sortKey = $UCA->getSortKey($string)

-- see 4.3 Form a sort key for each string, UTR #10.

Returns a sort key.

You compare the sort keys using a binary comparison and get the result of the comparison of the strings using UCA.

   $UCA->getSortKey($a) cmp $UCA->getSortKey($b)

      is equivalent to

   $UCA->cmp($a, $b)
$position = $UCA->index($string, $substring)
($position, $length) = $UCA->index($string, $substring)

-- see 6.8 Searching, UTR #10.

If $substring matches a part of $string, returns the position of the first occurrence of the matching part in scalar context; in list context, returns a two-element list of the position and the length of the matching part.

Notice that the length of the matching part may differ from the length of $substring.

Note that the position and the length are counted on the string after the process of preprocess, normalization, and rearrangement. Therefore, in case the specified string is not binary equal to the preprocessed/normalized/rearranged string, the position and the length may differ form those on the specified string. But it is guaranteed that, if matched, it returns a non-negative value as $position.

If $substring does not match any part of $string, returns -1 in scalar context and an empty list in list context.

e.g. you say

  my $UCA = Unicode::Collate->new( normalization => undef, level => 1 );
  my $str = "Ich mu\x{00DF} studieren.";
  my $sub = "m\x{00FC}ss";
  my $match;
  if(my @tmp = $UCA->index($str, $sub)){
    $match = substr($str, $tmp[0], $tmp[1]);
  }

and get "mu\x{00DF}" in $match since "muß" is primary equal to "müss".

EXPORT

None by default.

CAVEAT

Use of the normalization parameter requires the Unicode::Normalize module.

If you need not it (e.g. in the case when you need not handle any combining characters), assign normalization => undef explicitly.

AUTHOR

SADAHIRO Tomoyuki, <SADAHIRO@cpan.org>

  http://homepage1.nifty.com/nomenclator/perl/

  Copyright(C) 2001, SADAHIRO Tomoyuki. Japan. All rights reserved.

  This program is free software; you can redistribute it and/or 
  modify it under the same terms as Perl itself.

SEE ALSO

Lingua::KO::Hangul::Util

utility functions for Hangul Syllables

Unicode::Normalize

normalized forms of Unicode text

Unicode Collation Algorithm - Unicode TR #10

http://www.unicode.org/unicode/reports/tr10/