| 1 | package WordLists::Sort::Typical; | ||
| 2 | 2 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Unicode::Normalize; #provides NFD |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use WordLists::Sort qw( atomic_compare complex_compare); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use WordLists::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | our $VERSION = $WordLists::Base::VERSION; | |
| 9 | 2 | our $AUTOLOAD; | |
| 10 | 2 | require Exporter; | |
| 11 | 2 | our @ISA = qw (Exporter); | |
| 12 | 2 | our @EXPORT = (); | |
| 13 | 2 | our @EXPORT_OK = qw( | |
| 14 | cmp_alnum | ||
| 15 | cmp_alnum_only | ||
| 16 | cmp_accnum | ||
| 17 | cmp_accnum_only | ||
| 18 | cmp_ver | ||
| 19 | cmp_dict | ||
| 20 | ); | ||
| 21 | 37 | ▶ | sub _cmp { $_[0] cmp $_[1] }; |
|
Subroutine
_cmp: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | sub cmp_dict | ||
| 24 | { | ||
| 25 | 36 | ▶ | my $norm_remove_the = sub{$_[0] =~ s/^the\s+//;$_[0];}; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
cmp_dict: called CC=1 SCAR=0.0
Pod
documented
| |||
| 26 | complex_compare ( | ||
| 27 | $_[0], $_[1], | ||
| 28 | { | ||
| 29 | functions => | ||
| 30 | [ | ||
| 31 | { | ||
| 32 | n => $norm_remove_the, | ||
| 33 | t=>[ | ||
| 34 | { | ||
| 35 | re=>qr/.+/, | ||
| 36 | c => \&cmp_accnum_only, | ||
| 37 | }, | ||
| 38 | ], | ||
| 39 | }, | ||
| 40 | { | ||
| 41 | 12 | ▶ | n => sub{&{$norm_remove_the}($_[0]); $_[0] =~ s/[^\p{Script: Latin}0-9]/ /g; $_[0] =~ s/[a-z\p{Lowercase}]/ /g; $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 42 | c => \&_cmp, | ||
| 43 | }, | ||
| 44 | { | ||
| 45 | n => $norm_remove_the, | ||
| 46 | t=>[ | ||
| 47 | { | ||
| 48 | re=>qr/.+/, | ||
| 49 | c => \&cmp_alnum_only, | ||
| 50 | }, | ||
| 51 | ], | ||
| 52 | }, | ||
| 53 | { | ||
| 54 | 2 | ▶ | n => sub{&{$norm_remove_the}($_[0]); $_[0] =~ s/[^\p{Script: Latin}0-9]/ /g; $_[0] =~ s/[a-zA-Z]/ /g; $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 55 | c => \&_cmp, | ||
| 56 | }, | ||
| 57 | { | ||
| 58 | 2 | ▶ | n => sub{&{$norm_remove_the}($_[0]); $_[0] =~ s/[\P{Uppercase}]/ /g; $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 59 | c => \&_cmp, | ||
| 60 | }, | ||
| 61 | { | ||
| 62 | 2 | ▶ | n => sub { &{$norm_remove_the}($_[0]); $_[0]=~ s/[^\p{Script: Latin}0-9]//g; $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 63 | c => \&_cmp, | ||
| 64 | }, | ||
| 65 | { | ||
| 66 | n => $norm_remove_the, | ||
| 67 | t=>[ | ||
| 68 | { | ||
| 69 | re=>qr/.+/, | ||
| 70 | c => \&cmp_alpha, | ||
| 71 | }, | ||
| 72 | ], | ||
| 73 | }, | ||
| 74 | { | ||
| 75 | 8 | ▶ | c => sub { ($_[0] =~ s/^the\s+//) cmp ($_[1] =~ s/^the\s+//) }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 76 | }, | ||
| 77 | 7 | ] | |
| 78 | } | ||
| 79 | ); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub cmp_alnum | ||
| 83 | { | ||
| 84 | atomic_compare ( | ||
| 85 | $_[0], $_[1], | ||
| 86 | { | ||
| 87 | 12 | ▶ | n => sub { lc $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 88 | t => | ||
| 89 | [ | ||
| 90 | { | ||
| 91 | re => qr/[0-9]+/, | ||
| 92 | 3 | ▶ | c => sub { $_[0] <=> $_[1]; } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 93 | }, | ||
| 94 | 6 | ▶ | ], |
|
Subroutine
cmp_alnum: called CC=1 SCAR=0.0
Pod
documented
| |||
| 95 | } | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | sub cmp_alpha | ||
| 99 | { | ||
| 100 | atomic_compare ( | ||
| 101 | $_[0], $_[1], | ||
| 102 | { | ||
| 103 | 0 | ▶ | n => sub { lc $_[0];}, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 104 | } | ||
| 105 | 1 | ▶ | ); |
|
Subroutine
cmp_alpha: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 106 | } | ||
| 107 | sub cmp_alnum_only | ||
| 108 | { | ||
| 109 | atomic_compare ( | ||
| 110 | $_[0], $_[1], | ||
| 111 | { | ||
| 112 | n => sub { | ||
| 113 | 8 | ▶ | $_[0] =~ s/[^\p{Script: Latin}0-9]//g; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 114 | 8 | $_[0]; | |
| 115 | }, | ||
| 116 | 5 | ▶ | t=>[ |
|
Subroutine
cmp_alnum_only: called CC=1 SCAR=0.0
Pod
documented
| |||
| 117 | { | ||
| 118 | re=>qr/.+/, | ||
| 119 | c => \&cmp_alnum, | ||
| 120 | } | ||
| 121 | ], | ||
| 122 | } | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | sub cmp_accnum_only | ||
| 126 | { | ||
| 127 | atomic_compare ( | ||
| 128 | $_[0], $_[1], | ||
| 129 | { | ||
| 130 | n => sub { | ||
| 131 | 18 | ▶ | $_[0] =~ s/[^\p{Script: Latin}0-9]//g; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 132 | 18 | $_[0]; | |
| 133 | }, | ||
| 134 | 10 | ▶ | t=>[ |
|
Subroutine
cmp_accnum_only: called CC=1 SCAR=0.0
Pod
documented
| |||
| 135 | { | ||
| 136 | re=>qr/.+/, | ||
| 137 | c => \&cmp_accnum, | ||
| 138 | } | ||
| 139 | ], | ||
| 140 | } | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | sub cmp_accnum | ||
| 144 | { | ||
| 145 | atomic_compare ( | ||
| 146 | $_[0], $_[1], | ||
| 147 | { | ||
| 148 | n => sub { | ||
| 149 | 30 | ▶ | $_[0] = NFD ($_[0]); |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 150 | 30 | $_[0] =~ s/\pM//g; | |
| 151 | 30 | lc $_[0]; | |
| 152 | }, | ||
| 153 | t => | ||
| 154 | [ | ||
| 155 | { | ||
| 156 | re => qr/[0-9]+/, | ||
| 157 | 1 | ▶ | c => sub { $_[0] <=> $_[1]; } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 158 | }, | ||
| 159 | 15 | ▶ | ], |
|
Subroutine
cmp_accnum: called CC=1 SCAR=0.0
Pod
documented
| |||
| 160 | } | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub cmp_ver # compares version strings: anything that is not alphanumeric is a separator and doesn't have any preference | ||
| 165 | # 1.1 = 1:1 | ||
| 166 | # 1.1.a < 1.1a < 1.1.1 | ||
| 167 | { | ||
| 168 | atomic_compare ( | ||
| 169 | $_[0], $_[1], | ||
| 170 | { | ||
| 171 | 26 | ▶ | n => sub {$_[0] =~ s/^v//i; $_[0];}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 172 | t => | ||
| 173 | [ | ||
| 174 | { | ||
| 175 | re => qr/[0-9]+/, | ||
| 176 | 25 | ▶ | c => sub { $_[0] <=> $_[1]; } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 177 | }, | ||
| 178 | { | ||
| 179 | re => qr/[a-zA-Z]+/, | ||
| 180 | 4 | ▶ | c => sub { lc $_[0] cmp lc $_[1]; } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 181 | }, | ||
| 182 | { | ||
| 183 | 14 | ▶ | re => qr/[^a-zA-Z0-9]+/, |
|
Subroutine
cmp_ver: called CC=1 SCAR=0.0
Pod
documented
| |||
| 184 | c => 0 | ||
| 185 | }, | ||
| 186 | |||
| 187 | |||
| 188 | ], | ||
| 189 | } | ||
| 190 | ); | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | 2 | 1; | |
| 196 | |||
| 197 | |||
| 198 | =pod | ||
| 199 | |||
| 200 | =head1 NAME | ||
| 201 | |||
| 202 | WordLists::Sort::Typical | ||
| 203 | |||
| 204 | =head1 SYNOPSIS | ||
| 205 | |||
| 206 | 'A14' cmp 'A2'; # sadly returns -1, so instead do this: | ||
| 207 | use WordLists::Sort::Typical qw(cmp_alnum); | ||
| 208 | cmp_alnum('A14', 'A2'); # returns 1 | ||
| 209 | |||
| 210 | =head1 DESCRIPTION | ||
| 211 | |||
| 212 | This provides functions for sorting text. | ||
| 213 | |||
| 214 | =head3 cmp_alnum | ||
| 215 | |||
| 216 | Compares alphanumeric values sensibly, e.g. "Unit 10" comes after "Unit 9", not before "Unit 2". Case-insensitive. | ||
| 217 | |||
| 218 | =head3 cmp_alnum_only | ||
| 219 | |||
| 220 | Compares alphanumeric values sensibly as C<cmp_alnum>, but ignores all values except alphanumeric characters, so "re-factor" sorts with "refactor", not between "re" and "react". Case-insensitive. | ||
| 221 | |||
| 222 | =head3 cmp_accnum | ||
| 223 | |||
| 224 | Compares alphanumeric values sensibly as C<cmp_alnum>, and considers accented characters to be equivalent to unaccented characters, so "café" sorts with "cafe", not after "caftan". | ||
| 225 | |||
| 226 | =head3 cmp_accnum_only | ||
| 227 | |||
| 228 | Compares alphanumeric values sensibly and accent-insensitively as C<cmp_accnum>, and ignores non-alphanumeric content like C<cmp_alnum_only> | ||
| 229 | |||
| 230 | =head3 cmp_ver | ||
| 231 | |||
| 232 | Compares version numbers sensibly, even if they are of the form "v1.0028_01a". | ||
| 233 | |||
| 234 | =head3 cmp_dict | ||
| 235 | |||
| 236 | This uses a C<complex_sort>, the first stage being C<cmp_accnum_only>. Strings which are still equal are progressively sorted with tie-breakers so that order is reliable. Strings beginning "the " are sorted identically, except at the end, when strings without "the " have preference. | ||
| 237 | |||
| 238 | =over | ||
| 239 | |||
| 240 | =item * | ||
| 241 | |||
| 242 | Case - uppercase comes after lowercase. | ||
| 243 | |||
| 244 | =item * | ||
| 245 | |||
| 246 | Accents - uppercase comes after lowercase. | ||
| 247 | |||
| 248 | =item * | ||
| 249 | |||
| 250 | Non-alphanumeric characters - these are sorted, ignoring other intervening characters. | ||
| 251 | |||
| 252 | =item * | ||
| 253 | |||
| 254 | Definite article - if the strings are otherwise identical, a string beginning "the " comes after a string not beginning "the " | ||
| 255 | |||
| 256 | =back | ||
| 257 | |||
| 258 | =head1 BUGS | ||
| 259 | |||
| 260 | Please use the Github issues tracker. | ||
| 261 | |||
| 262 | =head1 LICENSE | ||
| 263 | |||
| 264 | Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 265 | |||
| 266 | =cut | ||