File Coverage

blib/lib/Lingua/JA/Gal.pm
Criterion Covered Total %
statement 34 34 100.0
branch 10 10 100.0
condition 6 7 85.7
subroutine 8 8 100.0
pod 1 1 100.0
total 59 60 98.3


line stmt bran cond sub pod time code
1             package Lingua::JA::Gal;
2 4     4   52887 use strict;
  4         5  
  4         99  
3 4     4   12 use warnings;
  4         5  
  4         71  
4 4     4   498 use utf8;
  4         14  
  4         12  
5             our $VERSION = '0.03';
6              
7 4     4   135 use Exporter 'import';
  4         5  
  4         118  
8 4     4   1600 use File::ShareDir 'dist_file';
  4         16133  
  4         228  
9 4     4   7328 use Unicode::Japanese;
  4         43423  
  4         16  
10              
11             our @EXPORT_OK = qw/gal/;
12              
13             our $Lexicon ||= do {
14             my $file = dist_file('Lingua-JA-Gal', 'lexicon.pl');
15             do $file;
16             };
17              
18             sub gal {
19 6 100   6 1 45 my $class = shift if $_[0] eq __PACKAGE__; ## no critic
20 6   50     34 my $text = shift || "";
21 6   100     19 my $options = shift || {};
22            
23 6 100       17 $options->{rate} = 100 if not defined $options->{rate};
24            
25 6         25 $text =~ s{(.)}{ _gal_char($1, $options) }ge;
  21         102  
26 6         30 $text;
27             }
28              
29             sub _gal_char {
30 21     21   29 my ($char, $options) = @_;
31            
32 21         17 my $suggestions = do {
33 21         54 my $normalized = Unicode::Japanese->new($char)->z2h->h2zKana->getu;
34 21 100       34605 $Lexicon->{ $normalized } || [];
35             };
36            
37 21 100       42 if (my $callback = $options->{callback}) {
38 7         13 return $callback->($char, $suggestions, $options);
39             }
40              
41 14 100 100     89 if (@$suggestions && int(rand 100) < $options->{rate}) {
42 8         27 return $suggestions->[ int(rand @$suggestions) ];
43             } else {
44 6         17 return $char;
45             }
46             }
47              
48             1;
49             __END__