| 1 | # $Header$ | ||||||||||||||||||||
| 2 | # $Log$ | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | package Lingua::EN::NameLookup; | ||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | $VERSION = '0.01'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | ▶ | use Text::Soundex; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 6 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 15 | 6 | my $this = shift; | |||||||||||||||||||
| 16 | 6 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 17 | 6 | my $self = {}; | |||||||||||||||||||
| 18 | 6 | bless $self, $class; | |||||||||||||||||||
| 19 | 6 | return $self; | |||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 9 | ▶ | sub lookup { | ||||||||||||||||||
|
Subroutine
lookup: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 23 | 9 | my ($self,$name) = @_; | |||||||||||||||||||
| 24 | 9 | my $code = soundex($name); | |||||||||||||||||||
| 25 | 9 | ▶ | return 0 if (!$code); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 8 | ▶ | return 0 if (!exists($self->{$code})); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 5 | foreach my $item (sort @{ $self->{$code}}) { | |||||||||||||||||||
| 28 | 22 | ▶ | return 0 if ($item gt $name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 22 | ▶ | return 1 if ($item eq $name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | 1 | return 0; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 6 | ▶ | sub ilookup { | ||||||||||||||||||
|
Subroutine
ilookup: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 35 | 6 | my ($self,$name) = @_; | |||||||||||||||||||
| 36 | 6 | my $code = soundex($name); | |||||||||||||||||||
| 37 | 6 | ▶ | return 0 if (!$code); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 5 | ▶ | return 0 if (!exists($self->{$code})); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 4 | foreach my $item (sort @{ $self->{$code}}) { | |||||||||||||||||||
| 40 | 8 | ▶ | return 1 if ($item =~ /$name/i); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 0 | return 0; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 2 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||
| 46 | 2 | my ($self,$name) = @_; | |||||||||||||||||||
| 47 | 2 | my $code = soundex($name); | |||||||||||||||||||
| 48 | 2 | ▶ | if (!exists($self->{$code})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 2 | $self->{$code} = [ $name ]; | |||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||
| 51 | 0 | my @array = @{ $self->{$code}}; | |||||||||||||||||||
| 52 | 0 | push @array,$name; | |||||||||||||||||||
| 53 | 0 | $self->{$code} = [ sort(@array) ]; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | sub dump { | ||||||||||||||||||
|
Subroutine
dump: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 58 | 2 | my ($self,$filename) = @_; | |||||||||||||||||||
| 59 | 2 | ▶ | unless (open(_DICT,">$filename")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 1 | warn "Can't open $filename for dump $!"; | |||||||||||||||||||
| 61 | 1 | return 0; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 1 | foreach my $family ( sort keys %$self ) { | |||||||||||||||||||
| 64 | 1988 | print _DICT "$family ",join(":",sort(@{ $self->{$family} })),"\n"; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | 1 | close(_DICT); | |||||||||||||||||||
| 67 | 1 | return 1; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 7 | ▶ | sub load { | ||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 71 | 7 | my ($self,$filename) = @_; | |||||||||||||||||||
| 72 | 7 | %$self = (); | |||||||||||||||||||
| 73 | 7 | ▶ | unless (open(_DICT,"$filename")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 1 | warn "Can't open $filename for load $!"; | |||||||||||||||||||
| 75 | 1 | return 0; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 6 | ▶ | while (<_DICT>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 11928 | chomp; | |||||||||||||||||||
| 79 | 11928 | ▶ | next unless s/^([A-Z]\d{3})\s*//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 11928 | $self->{$1} = [ split(/:/) ]; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | 6 | close(_DICT); | |||||||||||||||||||
| 83 | 6 | return 1; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 87 | 2 | my ($self,$filename) = @_; | |||||||||||||||||||
| 88 | 2 | %$self = (); | |||||||||||||||||||
| 89 | 2 | ▶ | unless (open(_DICT,"$filename")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 1 | warn "Can't open $filename for init $!"; | |||||||||||||||||||
| 91 | 1 | return 0; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 1 | ▶ | while (<_DICT>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 11761 | chomp; | |||||||||||||||||||
| 95 | 11761 | my $code = soundex($_); | |||||||||||||||||||
| 96 | 11761 | push @{ $self->{"$code"} }, "$_"; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 1 | close(_DICT); | |||||||||||||||||||
| 99 | 1 | return 1; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | ▶ | sub print { | ||||||||||||||||||
|
Subroutine
print: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 103 | 0 | my ($self) = @_; | |||||||||||||||||||
| 104 | 0 | foreach my $family ( sort keys %$self ) { | |||||||||||||||||||
| 105 | 0 | print "$family: ",join(" ",sort(@{ $self->{$family} })),"\n" | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 1 | ▶ | sub report { | ||||||||||||||||||
|
Subroutine
report: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 110 | 1 | my ($self) = @_; | |||||||||||||||||||
| 111 | 1 | my $key_count = 0; | |||||||||||||||||||
| 112 | 1 | my $longest_array = 0; | |||||||||||||||||||
| 113 | 1 | my $name_count = 0; | |||||||||||||||||||
| 114 | 1 | foreach my $key (keys %$self) { | |||||||||||||||||||
| 115 | 1988 | $key_count++; | |||||||||||||||||||
| 116 | 1988 | my @array = @{$self->{$key}}; | |||||||||||||||||||
| 117 | 1988 | my $array_length = $#array; | |||||||||||||||||||
| 118 | 1988 | $name_count += $array_length; | |||||||||||||||||||
| 119 | 1988 | ▶ | $longest_array = $array_length if ($array_length > $longest_array); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | 1 | $longest_array++; | |||||||||||||||||||
| 122 | 1 | return ($key_count, $name_count, $longest_array); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 6 | 1; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =pod | ||||||||||||||||||||
| 128 | =head1 Name | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | Lingua::EN::NameLookup - a simple dictionary search and manipulation class. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head1 Synopsis | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | use Lingua::EN::NameLookup; | ||||||||||||||||||||
| 135 | $dict = new Lingua::EN::NameLookup; | ||||||||||||||||||||
| 136 | $dict->load("mydict.dat"); | ||||||||||||||||||||
| 137 | $res = $dict->lookup("FOO"); | ||||||||||||||||||||
| 138 | $res = $dict->ilookup("Foo"); | ||||||||||||||||||||
| 139 | $dict->add("Bar"); | ||||||||||||||||||||
| 140 | $dict->dump("mynewdict.dat"); | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head1 Description | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This class provides the ability to search and manipulate a simple dictionary. | ||||||||||||||||||||
| 145 | It was originally designed for checking surnames encountered during the | ||||||||||||||||||||
| 146 | preparation of census indices. It works best with small data sets and where the | ||||||||||||||||||||
| 147 | names in the data set generate many distinct soundex values. The dictionary is | ||||||||||||||||||||
| 148 | maintained in memory and hence the memory usage depends on the number of names. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 Technique | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Here's how data is stored in the dictionary: | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Firstly the soundex value of the name is calculated. If there is no key in the | ||||||||||||||||||||
| 155 | hash with the soundex then the name is stored as a one element array. If there | ||||||||||||||||||||
| 156 | already is a key in the hash with the soundex the name is added to the end of | ||||||||||||||||||||
| 157 | the existing array. Then the array is sorted and stored back in the hash. Hence | ||||||||||||||||||||
| 158 | for a name such as BARLOW we might have the following in the hash: | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | B640 => (BARIL, BARLEY, BARLOW, BERLE,...) | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Here's how we look up a name: | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | First the soundex of the name is calculated. If there is no key in the hash | ||||||||||||||||||||
| 165 | with that soundex then the name is not in the dictionary. If there is a key in | ||||||||||||||||||||
| 166 | the hash with that soundex then the array is retrieved and searched for the | ||||||||||||||||||||
| 167 | name. Since we know that the array is sorted then the search can terminate as | ||||||||||||||||||||
| 168 | soon as an array element greater than the name being searched for is found as | ||||||||||||||||||||
| 169 | we then know that it cannot be in the array. This speeds things up when the | ||||||||||||||||||||
| 170 | individual arrays are large. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 Methods | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 new | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Creates a dictionary object and initialises it (to be empty). Options are | ||||||||||||||||||||
| 177 | passed as keyword value pairs. Recognised options are: | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head2 lookup($name) | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Looks up the name in the dictionary, returns true if it is found or false if it | ||||||||||||||||||||
| 182 | is not found. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head2 ilookup($name) | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Looks up the name in the dictionary but with a case insensitive match, returns | ||||||||||||||||||||
| 187 | true if it is found or false if it is not found. Not as efficient as lookup. | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | =head2 add($name) | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Add one name to the dictionary. Probably called after B<lookup> q.v. has failed | ||||||||||||||||||||
| 192 | to find a name. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head2 dump($file) | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Dumps the dictionary to a file suitable for subsequent reading by B<load> q.v. | ||||||||||||||||||||
| 197 | Each line of the file looks like: | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | soundex name1:name2:name3... | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | If the file cannot be opened for writing then this method will croak. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head2 load($file) | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | Load the dictionary from a file produced by B<dump> q.v. This is more efficient | ||||||||||||||||||||
| 206 | than using the B<init> method as it saves having to calculate the soundex for | ||||||||||||||||||||
| 207 | each name. Each line of the file looks like: | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | soundex name1:name2:name3... | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | If the file cannot be opened for reading then this method will croak. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =head2 init($file) | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | Initialise the dictionary from a file containing one name on each line. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | If the file cannot be opened for reading then this method will croak. | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head2 print | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | Produce a human readable form of the dictionary on standard output. This method | ||||||||||||||||||||
| 222 | was originally designed for debugging but may have other uses. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =head2 report | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | Returns a list containing the number of keys in the hash, the number of names | ||||||||||||||||||||
| 227 | in the hash and the length of the longest has entry. This method was originally | ||||||||||||||||||||
| 228 | designed for performance testing but may have other uses. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =head1 Copyright | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | Copyright (c) 2002 Pete Barlow <pbarlow@cpan.org>. All rights reserved. | ||||||||||||||||||||
| 233 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 234 | the same terms as Perl itself. | ||||||||||||||||||||