File Coverage

blib/lib/Text/TransMetaphone.pm
Criterion Covered Total %
statement 51 57 89.4
branch 18 40 45.0
condition n/a
subroutine 9 10 90.0
pod 2 4 50.0
total 80 111 72.0


line stmt bran cond sub pod time code
1             package Text::TransMetaphone;
2 1     1   139258 use base qw(Exporter);
  1         2  
  1         167  
3 1     1   7 use utf8;
  1         3  
  1         13  
4              
5             BEGIN
6             {
7 1     1   44 use strict;
  1         1  
  1         41  
8 1     1   5 use warnings;
  1         2  
  1         69  
9 1     1   5 use vars qw($VERSION @EXPORT_OK %LocaleRanges);
  1         2  
  1         200  
10              
11 1     1   4 $VERSION = "0.10";
12              
13 1         3 @EXPORT_OK = qw( trans_metaphone reverse_key );
14              
15 1         1035 %LocaleRanges = ();
16             }
17              
18              
19             sub guess_locale
20             {
21 1     1 0 7 my ($word) = @_;
22              
23 1         3 my $locale;
24 1         93 $word =~ /(\p{IsLetter})/;
25 1         5 my $char = $1; # grab first letter
26              
27             #
28             # check ranges of registered locales
29             #
30 1         6 foreach (keys %LocaleRanges) {
31 0 0       0 $locale = $_ if ( $char =~ /$LocaleRanges{$_}/ );
32             }
33 1 50       6 unless ( $locale ) {
34 1 50       35 $locale = "am" if ( $char =~ /\p{InEthiopic}/ );
35 1 50       41 $locale = "ar" if ( $char =~ /\p{InArabic}/ );
36 1 50       24 $locale = "ch" if ( $char =~ /\p{InCherokee}/ );
37 1 50       17 $locale = "el" if ( $char =~ /\p{InGreekAndCoptic}/ );
38 1 50       8 $locale = "en_US" if ( $char =~ /\p{InBasicLatin}/ );
39 1 50       8 $locale = "gu" if ( $char =~ /\p{InGujarati}/ );
40 1 50       8 $locale = "he" if ( $char =~ /\p{InHebrew}/ );
41 1 50       7 $locale = "ja_hiragana" if ( $char =~ /\p{InHiragana}/ );
42 1 50       6 $locale = "ja_katakana" if ( $char =~ /\p{InKatakana}/ );
43 1 50       9 $locale = "ru" if ( $char =~ /\p{InCyrillic}/ );
44             }
45              
46 1         5 $locale;
47              
48             }
49              
50              
51             sub list_locales
52             {
53 0     0 0 0 keys %LocaleRanges;
54             }
55              
56              
57             sub trans_metaphone
58             {
59 1     1 1 189273 my $word = shift;
60 1 50       7 my $locale = (@_) ? shift : guess_locale ( $word );
61              
62              
63 1 50       4 die "No locale identified for $word. Locale must be specified." unless ( $locale );
64              
65 1 50       5 unless ( exists($LocaleRanges{$locale}) ) {
66 1         3 my $module = "Text::TransMetaphone::$locale";
67 1 50       103 eval "require $module;"
68             || die "Unable to load Text::TransMetaphone::$locale : $@";
69              
70 1         6 $LocaleRanges{$locale} = ${"Text::TransMetaphone::${locale}::LocaleRange"};
  1         9  
71             }
72              
73 1         2 my @keys = &{"Text::TransMetaphone::${locale}::trans_metaphone"} ( $word );
  1         6  
74              
75 1 50       8 ( wantarray ) ? @keys : $kesy[0];
76             }
77              
78              
79             sub reverse_key
80             {
81 1     1 1 1141 my ($word, $locale) = @_;
82              
83 1 50       8 die "No locale identified for $word. Locale must be specified." unless ( $locale );
84              
85 1 50       5 unless ( exists($LocaleRanges{$locale}) ) {
86 0         0 my $module = "Text::TransMetaphone::$locale";
87 0 0       0 eval "require $module;"
88             || die "Unable to load Text::TransMetaphone::$locale : $@";
89              
90 0         0 $LocaleRanges{$locale} = ${"Text::TransMetaphone::${locale}::LocaleRange"};
  0         0  
91             }
92              
93 1         2 &{"Text::TransMetaphone::${locale}::reverse_key"} ( $word );
  1         8  
94              
95             }
96              
97              
98              
99             #########################################################
100             # Do not change this, Do not put anything below this.
101             # File must return "true" value at termination
102             1;
103             ##########################################################
104              
105             __END__