File Coverage

blib/lib/Lingua/ZH/CCDICT/Storage/InMemory.pm
Criterion Covered Total %
statement 52 59 88.1
branch 6 8 75.0
condition 3 3 100.0
subroutine 10 12 83.3
pod 4 4 100.0
total 75 86 87.2


line stmt bran cond sub pod time code
1             package Lingua::ZH::CCDICT::Storage::InMemory;
2              
3 1     1   27242 use strict;
  1         4  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         31  
5              
6 1     1   6 use base 'Lingua::ZH::CCDICT';
  1         6  
  1         654  
7              
8 1     1   7 use Params::Validate qw( validate SCALAR );
  1         2  
  1         70  
9              
10 1     1   992 use Lingua::ZH::CCDICT::ResultItem;
  1         3  
  1         34  
11 1     1   887 use Lingua::ZH::CCDICT::ResultSet::Array;
  1         3  
  1         878  
12              
13              
14             sub new
15             {
16 1     1 1 3 my $class = shift;
17 1         18 my %p = validate( @_,
18             { file => { type => SCALAR, optional => 1 },
19             },
20             );
21              
22 1         6 my $self = bless {}, $class;
23              
24 1         11 $self->parse_source_file( $p{file} );
25              
26 1         12 return $self;
27             }
28              
29             sub _real_add_entry
30             {
31 27484     27484   33091 my $self = shift;
32 27484         30567 my $unicode = shift;
33 27484         29705 my $entry = shift;
34              
35 27484         131034 foreach my $key ( keys %$entry )
36             {
37 252231 100 100     1104354 next if $key eq 'english' || $key eq 'unicode';
38              
39 203223 100       285068 foreach my $val ( eval { @{ $entry->{$key} } } ? @{ $entry->{$key} } : $entry->{$key} )
  203223         236148  
  203223         2880129  
  68968         144612  
40             {
41             # intentionally stringify to take advantage of
42             # stringification overloading for romanizations
43 262395         321080 push @{ $self->{$key}{"$val"} }, $unicode;
  262395         1164886  
44             }
45             }
46              
47 27484         142912 $self->{unicode}{$unicode} = $entry;
48 27484         127069 $self->{unicode}{$unicode}{unicode} = $unicode;
49             }
50              
51             sub all_characters
52             {
53 0     0 1 0 my $self = shift;
54              
55             return
56 0         0 Lingua::ZH::CCDICT::ResultSet::Array->new
57             ( results =>
58 0         0 [ map { Lingua::ZH::CCDICT::ResultItem->new( %{ $self->{unicode}{$_} } ) }
  0         0  
59 0         0 sort keys %{ $self->{unicode} } ]
60             );
61             }
62              
63             sub match_unicode
64             {
65 9     9 1 1642 my $self = shift;
66              
67 9         11 my %seen;
68 9 50       26 my @chars = map { $seen{$_}++ ? () : $_ } sort @_;
  32         108  
69              
70 9         13 my @results;
71 9         14 foreach my $char (@chars)
72             {
73 32 50       127 if ( exists $self->{unicode}{$char} )
74             {
75 32         191 push @results,
76 32         30 Lingua::ZH::CCDICT::ResultItem->new( %{ $self->{unicode}{$char} } );
77             }
78             }
79              
80 9         41 return Lingua::ZH::CCDICT::ResultSet::Array->new( array => \@results );
81             }
82              
83             sub _match
84             {
85 3     3   5 my $self = shift;
86 3         5 my $type = shift;
87              
88             return
89 4         4 $self->match_unicode( map { @{ $self->{$type}{$_} } }
  4         35  
  4         19  
90 3         5 grep { exists $self->{$type}{$_} }
91             @_
92             );
93             }
94              
95             sub entry_count
96             {
97 0     0 1   return scalar keys %{ $_[0]->{unicode} };
  0            
98             }
99              
100              
101             1;
102              
103             __END__