Lingua-EN-NameParse
view release on metacpan or search on metacpan
lib/Lingua/EN/NameParse.pm view on Meta::CPAN
The program changes the order of the name back to the non reversed format, and
then performs the normal parsing. Note that if the name can be parsed, the fact
that it's order was originally reversed, is not recorded as a property of the
name object.
=item joint_names
When this option is set to a positive value, joint names are accounted for:
Mr_A_Smith_&Ms_B_Jones
Mr_&Ms_A_&B_Smith
Mr_A_&Ms_B_Smith
Mr_&Ms_A_Smith
Mr_A_&B_Smith
Note that if this option is not specified, than by default joint names are
ignored. Disabling joint names speeds up the processing a lot.
=item extended_titles
When this option is set to a positive value, all combinations of titles,
such as Colonel, Mother Superior are used. If this value is not set, only
the following titles are accounted for:
Mr
Ms
M/s
Mrs
Miss
Dr
Sir
Dame
Note that if this option is not specified, than by default extended titles
are ignored. Disabling extended titles speeds up the parsing.
=back
=head2 parse
$error = $name->parse("MR AC DE SILVA");
The C<parse> method takes a single parameter of a text string containing a
name. It attempts to parse the name and break it down into the components
Returns an error flag. If the name was parsed successfully, it's value is 0,
otherwise a 1. This step is a prerequisite for the following methods.
=head2 case_all
$correct_casing = $name->case_all;
The C<case_all> method converts the first letter of each component to
capitals and the remainder to lower case, with the following exceptions-
initials remain capitalised
surname spelling such as MacNay-Smith, O'Brien and Van Der Heiden are preserved
- see C<surname_prefs.txt> for user defined exceptions
A complete definition of the capitalising rules can be found by studying
the case_surname function.
The method returns the entire cased name as text.
=head2 case_all_reversed
$correct_casing = $name->case_all_reversed;
The C<case_all_reversed> method applies the same type of casing as
C<case_all>. However, the name is returned as surname followed by a comma
and the rest of the name, which can be any of the combinations allowed
for a name, except the title. Some examples are: "Smith, John", "De Silva, A.B."
This is useful for sorting names alphabetically by surname.
The method returns the entire reverse order cased name as text.
=head2 components
%my_name = $name->components;
$cased_surname = $my_name{surname_1};
The C<components> method does the same thing as the C<case_all> method,
but returns the name cased components in a hash. The following keys are used
for each component:
precursor
title_1
title_2
given_name_1
given_name_2
initials_1
initials_2
middle_name
conjunction_1
conjunction_2
surname_1
surname_2
suffix
If a component has no matching data for a given name, it will not appear in the hash
If the name could not be parsed, this method returns null. If you assign the return
value to a hash, you should check the error status returned by the C<parse> method first.
Ohterwise, you will get an odd number of values assigned to the hash.
=head2 case_surname
$correct_casing = case_surname("DE SILVA-MACNAY" [,$lc_prefix]);
C<case_surname> is a stand alone function that does not require a name
object. The input is a text string. An optional input argument controls the
casing rules for prefix portions of a surname, as described above in the
C<lc_prefix> section.
The output is a string converted to the correct casing for surnames.
See C<surname_prefs.txt> for user defined exceptions
This function is useful when you know you are only dealing with names that
do not have initials like "Mr John Jones". It is much faster than the case_all
method, but does not understand context, and cannot detect errors on strings
that are not personal names.
=head2 surname_prefs.txt
Some surnames can have more than one form of valid capitalisation, such as
MacQuarie or Macquarie. Where the user wants to specify one form as the default,
a text file called surname_prefs.txt should be created and placed in the same
location as the NameParse module. The text file should contain one surname per
line, in the capitalised form you want, such as
Macquarie
MacHado
NameParse will still operate if the file does not exist
=head2 salutation
$salutation = $name->salutation(salutation => 'Dear',sal_default => 'Friend',sal_type => 'given_name'));
The C<salutation> method converts a name into a personal greeting,
such as "Dear Mr & Mrs O'Brien" or "Dear Sue and John"
Optional parameters may be specided in a hash as follows:
salutation:
The greeting word such as 'Dear' or 'Greetings'. If not spefied than 'Dear' is used
sal_default:
The default word used when a personalised salution cannot be generated. If not
specified, than 'Friend' is used.
sal_type:
Can be either 'given_name' such as 'Dear Sue' or 'title_plus_name' such as 'Dear Ms Smith'
If not specified, than 'given_name' is used.
If an error is detected during parsing, such as with the name "AB Smith & Associates",
then the value of sal_default is used instead of a given name, or a title and surname.
If the input string contains a conjunction, an 's' is added to the value of sal_default.
If the name contains a precursor, a default salutation is produced.
=head2 clean
$good_name = clean("Bad Na9me");
C<clean> is a stand alone function that does not require a name object.
The input is a text string and the output is the string with:
all repeating spaces removed
all characters not in the set (A-Z a-z - ' , . &) removed
=head2 properties
The C<properties> method returns all the properties of the name,
non_matching, number and type, as a hash.
=over 4
=item type
The type of format a name is in, as one of the following strings:
lib/Lingua/EN/NameParse.pm view on Meta::CPAN
# approximation, but still fail on initials of more than 1 letter
push(@cased_name,case_surname($name->{comps}{non_matching},$name->{lc_prefix}));
}
return(join(' ',@cased_name));
}
#-------------------------------------------------------------------------------
=head1 case_all_reversed
Apply correct capitalisation to a person's entire name and reverse the order
so that surname is first, followed by the other components, such as: Smith, Mr John A
Useful for creating a list of names that can be sorted by surname.
If name type is unknown , returns null
If the name type has a joint name, such as 'Mr_A_Smith_Ms_B_Jones', return null,
as it is ambiguous which surname to place at the start of the string
Else, returns a string of all cased components in correct reversed order
=cut
sub case_all_reversed
{
my $name = shift;
my @cased_name_reversed;
unless ( $name->{properties}{type} eq 'unknown' )
{
unless ( $reverse_component_order{$name->{properties}{type} } )
{
# this type of name should not be reversed, such as two surnames
return;
}
my %component_vals = $name->components;
my @reverse_order = @{ $reverse_component_order{$name->{properties}{type} } };
foreach my $component_key ( @reverse_order )
{
# As some components such as precursors are optional, they will appear
# in the order array but may or may not have have a value, so only
# process defined values
my $component_value = $component_vals{$component_key};
if ( $component_value )
{
if ($component_key eq 'surname_1')
{
$component_value .= ',';
}
push(@cased_name_reversed,$component_value);
}
}
}
return(join(' ',@cased_name_reversed));
}
#-------------------------------------------------------------------------------
# The user may specify their own preferred spelling for surnames.
# These should be placed in a text file called surname_prefs.txt
# in the same location as the module itself.
BEGIN
{
# Obtain the full path to NameParse module, defined in the %INC hash.
my $prefs_file_location = $INC{"Lingua/EN/NameParse.pm"};
# Now substitute the name of the preferences file
$prefs_file_location =~ s/NameParse\.pm$/surname_prefs.txt/;
if ( open(PREFERENCES_FH,"<$prefs_file_location") )
{
my @surnames = <PREFERENCES_FH>;
foreach my $name ( @surnames )
{
chomp($name);
# Build hash, lower case name is key for case insensitive
# comparison, while value holds the actual capitalisation
$Lingua::EN::surname_preferences{lc($name)} = $name;
}
close(PREFERENCES_FH);
}
}
#-------------------------------------------------------------------------------
# Apply correct capitalisation to a person's surname. Can be called as a
# stand alone function.
sub case_surname
{
my ($surname,$lc_prefix) = @_;
unless ($surname)
{
return('');
}
# If the user has specified a preferred capitalisation for this
# surname in the surname_prefs.txt, it should be returned now.
if ($Lingua::EN::surname_preferences{lc($surname)} )
{
return($Lingua::EN::surname_preferences{lc($surname)});
}
# Lowercase everything
$surname = lc($surname);
# Now uppercase first letter of every word. By checking on word boundaries,
# we will account for apostrophes (D'Angelo) and hyphenated names
$surname =~ s/\b(\w)/\u$1/g;
# Name case Macs and Mcs
# Exclude names with 1-2 letters after prefix like Mack, Macky, Mace
# Exclude names ending in a,c,i,o,z or j, typically Polish or Italian
if ( $surname =~ /\bMac[a-z]{2,}[^a|c|i|o|z|j]\b/i )
{
$surname =~ s/\b(Mac)([a-z]+)/$1\u$2/ig;
# Now correct for "Mac" exceptions
$surname =~ s/MacHin/Machin/;
$surname =~ s/MacHlin/Machlin/;
$surname =~ s/MacHar/Machar/;
$surname =~ s/MacKle/Mackle/;
$surname =~ s/MacKlin/Macklin/;
$surname =~ s/MacKie/Mackie/;
# Portuguese
$surname =~ s/MacHado/Machado/;
# Lithuanian
$surname =~ s/MacEvicius/Macevicius/;
$surname =~ s/MacIulis/Maciulis/;
$surname =~ s/MacIas/Macias/;
}
elsif ( $surname =~ /\bMc/i )
{
$surname =~ s/\b(Mc)([a-z]+)/$1\u$2/ig;
}
# Exceptions (only 'Mac' name ending in 'o' ?)
$surname =~ s/Macmurdo/MacMurdo/;
if ( $lc_prefix )
{
# Lowercase first letter of every word in prefix. The trailing space
# prevents the surname from being altered. Note that spellings like
# d'Angelo are not accounted for.
$surname =~ s/\b(\w+ )/\l$1/g;
}
# Correct for possessives such as "John's" or "Australia's". Although this
# should not occur in a person's name, they are valid for proper names.
# As this subroutine may be used to capitalise words other than names,
# we may need to account for this case. Note that the 's' must be at the
# end of the string
$surname =~ s/(\w+)'S(\s+)/$1's$2/;
$surname =~ s/(\w+)'S$/$1's/;
( run in 3.630 seconds using v1.01-cache-2.11-cpan-364913b4093 )