1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
package Lingua::EN::NameCase;
=head1 NAME
Lingua::EN::NameCase - Correctly case a person's name from UPERCASE or lowcase
=cut
use warnings;
use strict;
use locale;
use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK $HEBREW $SPANISH $ROMAN $POSTNOMINAL );
=head1 VERSION
Version 1.21
=cut
our $VERSION = '1.21';
=head1 SYNOPSIS
# Working with scalars; complementing lc and uc.
use Lingua::EN::NameCase qw( nc );
$FixedCasedName = nc( $OriginalName );
$FixedCasedName = nc( \$OriginalName );
# Working with arrays or array references.
use Lingua::EN::NameCase 'NameCase';
$FixedCasedName = NameCase( $OriginalName );
@FixedCasedNames = NameCase( @OriginalNames );
$FixedCasedName = NameCase( \$OriginalName );
@FixedCasedNames = NameCase( \@OriginalNames );
NameCase( \@OriginalNames ) ; # In-place.
# NameCase will not change a scalar in-place, i.e.
NameCase( \$OriginalName ) ; # WRONG: null operation.
$Lingua::EN::NameCase::SPANISH = 1;
# Now 'El' => 'El' instead of (default) Greek 'El' => 'el'.
# Now 'La' => 'La' instead of (default) French 'La' => 'la'.
$Lingua::EN::NameCase::HEBREW = 0;
# Now 'Aharon BEN Amram Ha-Kohein' => 'Aharon Ben Amram Ha-Kohein'
# instead of (default) => 'Aharon ben Amram Ha-Kohein'.
$Lingua::EN::NameCase::ROMAN = 0;
# Now 'Li' => 'Li' instead of (default) 'Li' => 'LI'.
$Lingua::EN::NameCase::POSTNOMINAL = 0;
# Now 'PHD' => 'PhD' instead of (default) 'PHD' => 'Phd'.
=head1 DESCRIPTION
Forenames and surnames are often stored either wholly in UPPERCASE
or wholly in lowercase. This module allows you to convert names into
the correct case where possible.
Although forenames and surnames are normally stored separately if they
do appear in a single string, whitespace separated, NameCase and nc deal
correctly with them.
NameCase currently correctly name cases names which include any of the
following:
Mc, Mac, al, el, ap, da, de, delle, della, di, du, del, der,
la, le, lo, van and von.
It correctly deals with names which contain apostrophes and hyphens too.
=head2 EXAMPLE FIXES
Original Name Case
-------- ---------
KEITH Keith
LEIGH-WILLIAMS Leigh-Williams
MCCARTHY McCarthy
O'CALLAGHAN O'Callaghan
ST. JOHN St. John
plus "son (daughter) of" etc. in various languages, e.g.:
VON STREIT von Streit
VAN DYKE van Dyke
AP LLWYD DAFYDD ap Llwyd Dafydd
etc.
plus names with roman numerals (up to 89, LXXXIX), e.g.:
henry viii Henry VIII
louis xiv Louis XIV
=cut
#--------------------------------------------------------------------------
# Modules
use Carp;
use Exporter();
@ISA = qw( Exporter );
@EXPORT = qw( nc );
@EXPORT_OK = qw( NameCase nc );
#--------------------------------------------------------------------------
# Variables
$HEBREW = 1;
$SPANISH = 0;
$ROMAN = 1;
$POSTNOMINAL = 1;
my @POST_NOMINAL_INITIALS = qw(
VC GC KG LG KT LT KP GCB OM GCSI GCMG GCIE GCVO GBE CH KCB DCB KCSI KCMG
DCMG KCIE KCVO DCVO KBE DBE CB CSI CMG CIE CVO CBE DSO LVO OBE ISO MVO MBE
IOM CGC RRC DSC MC DFC AFC ARRC OBI DCM CGM GM IDSM DSM MM DFM AFM SGM IOM
CPM QGM RVM BEM QPM QFSM QAM CPM MSM ERD VD TD UD ED RD VRD AE
PC ADC QHP QHS QHDS QHNS QHC SCJ J LJ QS SL QC KC JP DL MP MSP MSYP AM AM
MLA MEP DBEnv DConstMgt DREst EdD DPhil PhD DLitt DSocSci MD EngD DD LLD
DProf MA MArch MAnth MSc MMORSE MMath MMathStat MPharm MPhil MSc MSci MSt
MRes MEng MChem MBiochem MSocSc MMus LLM BCL MPhys MComp MAcc MFin MBA MPA
MEd MEP MEnt MCGI MGeol MLitt MEarthSc MClinRes BA BSc LLB BEng MBChB FdA
FdSc FdEng PgDip PgD PgCert PgC PgCLTHE AUH AKC AUS HNC HNCert HND HNDip
DipHE Dip OND CertHE ACSM MCSM DIC AICSM ARSM ARCS LLB LLM BCL MJur DPhil
PhD LLD DipLP FCILEx GCILEx ACILEx CQSW DipSW BSW MSW FCILT CMILT MILT CPL
CTP CML PLS CTL DLP PLog EJLog ESLog EMLog JrLog Log SrLog BArch MArch ARB
RIBA RIAS RIAI RSAW MB BM BS BCh BChir MRCS FRCS MS MCh. MRCP FRCP MRCPCH
FRCPCH MRCPath MFPM FFPM BDS MRCPsych FRCPsych MRCOG FRCOG MCEM FCEM FRCA
FFPMRCA MRCGP FRCGP BSc MScChiro MChiro MSc DC LFHOM MFHOM FFHOM FADO FBDO
FCOptom MCOptom MOst DPT MCSP FCSP. SROT MSCR FSCR. CPhT RN VN RVN BVSc
BVetMed VetMB BVM&S MRCVS FRCVS FAWM PGCAP PGCHE PGCE PGDE BEd NPQH QTS
CSci CSciTeach RSci RSciTech CEng IEng EngTech ICTTech DEM MM CMarEng
CMarSci CMarTech IMarEng MarEngTech RGN SRN RMN RSCN SEN EN RNMH RN RM RN1
RNA RN2 RN3 RNMH RN4 RN5 RNLD RN6 RN8 RNC RN7 RN9 RHV RSN ROH RFHN SPAN
SPMH SPCN SPLD SPHP SCHM SCLD SPCC SPDN V100 V200 V300 LPE MSc
);
#--------------------------------------------------------------------------
# Functions
=head2 NameCase
Takes a scalar, scalarref, array or arrayref, and changes the case of the
contents, as appropriate. Essentially a wrapper around nc().
=cut
sub NameCase {
croak "Usage: \$SCALAR|\@ARRAY = NameCase [\\]\$SCALAR|\@ARRAY"
if ref $_[0] and ( ref $_[0] ne 'ARRAY' and ref $_[0] ne 'SCALAR' );
local( $_ );
if( wantarray and ( scalar @_ > 1 or ref $_[0] eq 'ARRAY' ) ) {
# We have received an array or array reference in a list context
# so we will return an array.
map { nc( $_ ) } @{ ref( $_[0] ) ? $_[0] : \@_ };
} elsif( ref $_[0] eq 'ARRAY' ) {
# We have received an array reference in a scalar or void context
# so we will work on the array in-place.
foreach ( @{ $_[0] } ) {
$_ = nc( $_ );
}
} elsif( ref $_[0] eq 'SCALAR' ) {
# We don't work on scalar references in-place; we take the value
# and return a name-cased copy.
nc( ${ $_[0] } );
} elsif( scalar @_ == 1 ) {
# We've received a scalar: we return a name-cased copy.
nc( $_[0] );
} else {
croak "NameCase only accepts a single scalar, array or array ref";
}
}
=head2 nc
Takes a scalar or scalarref, and change the case of the name in the
corresponding string appropriately.
=cut
sub nc {
croak "Usage: nc [[\\]\$SCALAR]"
if scalar @_ > 1 or ( ref $_[0] and ref $_[0] ne 'SCALAR' );
local( $_ ) = @_ if @_;
$_ = ${$_} if ref( $_ ) ; # Replace reference with value.
return $_ unless($_);
$_ = lc ; # Lowercase the lot.
s{ \b (\w) }{\u$1}gx; # Uppercase first letter of every word.
s{ (\'\w) \b }{\L$1}gx; # Lowercase 's.
# Name case Mcs and Macs - taken straight from NameParse.pm incl. comments.
# Exclude names with 1-2 letters after prefix like Mack, Macky, Mace
# Exclude names ending in a,c,i,o, or j are typically Polish or Italian
if ( /\bMac[A-Za-z]{2,}[^aciozj]\b/ or /\bMc/ ) {
s/\b(Ma?c)([A-Za-z]+)/$1\u$2/g;
# Now correct for "Mac" exceptions
s/\bMacEvicius/Macevicius/g; # Lithuanian
s/\bMacHado/Machado/g; # Portuguese
s/\bMacHar/Machar/g;
s/\bMacHin/Machin/g;
s/\bMacHlin/Machlin/g;
s/\bMacIas/Macias/g;
s/\bMacIulis/Maciulis/g;
s/\bMacKie/Mackie/g;
s/\bMacKle/Mackle/g;
s/\bMacKlin/Macklin/g;
s/\bMacQuarie/Macquarie/g;
s/\bMacOmber/Macomber/g;
s/\bMacIn/Macin/g;
s/\bMacKintosh/Mackintosh/g;
s/\bMacKen/Macken/g;
s/\bMacHen/Machen/g;
s/\bMacisaac/MacIsaac/g;
s/\bMacHiel/Machiel/g;
s/\bMacIol/Maciol/g;
s/\bMacKell/Mackell/g;
s/\bMacKlem/Macklem/g;
s/\bMacKrell/Mackrell/g;
s/\bMacLin/Maclin/g;
s/\bMacKey/Mackey/g;
s/\bMacKley/Mackley/g;
s/\bMacHell/Machell/g;
s/\bMacHon/Machon/g;
}
s/Macmurdo/MacMurdo/g;
# Fixes for "son (daughter) of" etc. in various languages.
s{ \b Al(?=\s+\w) }{al}gx; # al Arabic or forename Al.
s{ \b Ap \b }{ap}gx; # ap Welsh.
# <http://www.jewfaq.org/jnames.htm> search for: followed by ben
# without first (?<=\S\s), first name of 'ben jones' remains lowercase
s{ (?<=\S\s)\bBen(?=\s+\w) }{ben}gx if $HEBREW; # ben Hebrew or forename Ben.
s{ (?<=\S\s)\bBat(?=\s+\w) }{bat}gx if $HEBREW; # bat Hebrew or forename Bat.
s{ \b Dell([ae])\b }{dell$1}gx; # della and delle Italian.
s{ \b D([aeiu]) \b }{d$1}gx; # da, de, di Italian; du French.
s{ \b De([lr]) \b }{de$1}gx; # del Italian; der Dutch/Flemish.
s{ \b El \b }{el}gx unless $SPANISH; # el Greek or El Spanish.
s{ \b La \b }{la}gx unless $SPANISH; # la French or La Spanish.
s{ \b L([eo]) \b }{l$1}gx; # lo Italian; le French.
s{ \b Van(?=\s+\w) }{van}gx; # van German or forename Van.
s{ \b Von \b }{von}gx; # von Dutch/Flemish
if($ROMAN) {
# Fixes for roman numeral names, e.g. Henry VIII, up to 89, LXXXIX
s{ \b ( (?: [Xx]{1,3} | [Xx][Ll] | [Ll][Xx]{0,3} )?
(?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b }{\U$1}gx;
}
if($POSTNOMINAL) {
# post-nominal initials
for my $pni (@POST_NOMINAL_INITIALS) {
s{ \b ($pni) $}{$pni}ix;
}
}
$_;
}
=head1 BUGS
The module covers the rules that I know of. There are probably a lot
more rules, exceptions etc. for "Western"-style languages which could be
incorporated.
There are probably lots of exceptions and problems - but as a general
data 'cleaner' it may be all you need.
Use Kim Ryan's L<Lingua::EN::NameParse> for any really sophisticated name parsing.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Lingua::EN::NameCase
You can also look for information at:
=over 4
=item * MetaCPAN
L<https://metacpan.org/release/Lingua-EN-NameCase>
=item * RT: CPAN's request tracker
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-EN-NameCase>
=item * CPANTS
L<http://cpants.cpanauthors.org/dist/Lingua-EN-NameCase>
=item * CPAN Testers' Matrix
L<http://matrix.cpantesters.org/?dist=Lingua-EN-NameCase>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Lingua-EN-NameCase>
=item * CPAN Testers Dependencies
L<http://deps.cpantesters.org/?module=Lingua::EN::NameCase>
=back
=head1 AUTHOR
1998-2014 Mark Summerfield <summer@qtrac.eu>
2014-present Barbie <barbie@cpan.org>
2020- Maintained by Nigel Horne, C<< <njh at bandsman.co.uk> >>
=head1 ACKNOWLEDGEMENTS
Thanks to Kim Ryan <kimaryan@ozemail.com.au> for his Mc/Mac solution.
=head1 COPYRIGHT
Copyright (c) Mark Summerfield 1998-2014. All Rights Reserved.
Copyright (c) Barbie 2014-2015. All Rights Reserved.
This distribution is free software; you can redistribute it and/or
modify it under the Artistic Licence v2.
=cut
1;
|