File Coverage

blib/lib/String/Normal/Type/Address.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 12 66.6
condition 3 8 37.5
subroutine 6 6 100.0
pod 2 2 100.0
total 49 58 84.4


line stmt bran cond sub pod time code
1             package String::Normal::Type::Address;
2 9     9   32 use strict;
  9         9  
  9         209  
3 9     9   30 use warnings;
  9         9  
  9         172  
4 9     9   28 use String::Normal::Type;
  9         8  
  9         118  
5 9     9   31 use String::Normal::Config;
  9         9  
  9         2348  
6              
7             our $address_stem;
8             our $address_stop;
9              
10             sub transform {
11 2     2 1 3 my ($self,$value) = @_;
12              
13 2 50       10 $value =~ s/\([^)]*\)/ /g if $value =~ /^[^(]|[^)]$/;
14              
15 2         7 $value = String::Normal::Type::_scrub_value( $value );
16              
17             # tokenize and stem
18 2         4 my @tokens = ();
19 2         5 for my $token (split ' ', $value) {
20 7 100       12 $token = defined( $address_stem->{$token} ) ? $address_stem->{$token} : $token;
21             # TODO: this form of stop wording will need to be further addressed
22 7 100       10 if (@tokens > 2) {
23 1 50 33     8 last if $token eq 'apt' or $token eq 'ste';
24             }
25 6         8 push @tokens, $token;
26             }
27              
28             # remove all middle stop words
29             my @filtered = map {
30 2   50     3 my $count = $address_stop->{middle}{$_} || '';
  6         17  
31 6 50 33     15 (length $count and @tokens >= $count) ? () : $_;
32             } @tokens;
33              
34             # revert if we filtered words down to less than 2 tokens
35 2 50       5 @filtered = @tokens if @filtered < 2;
36              
37 2         11 return join ' ', @filtered;
38             }
39              
40             sub new {
41 1     1 1 2 my $self = shift;
42 1         4 $address_stem = String::Normal::Config::AddressStem::_data( @_ );
43 1         6 $address_stop = String::Normal::Config::AddressStop::_data( @_ );
44 1         5 return bless {@_}, $self;
45             }
46              
47             1;
48              
49             =head1 NAME
50              
51             String::Normal::Type::Address;
52              
53             =head1 DESCRIPTION
54              
55             This package defines substitutions to be performed on the address
56             and city types of a record.
57              
58             =head1 METHODS
59              
60             =over 4
61              
62             =item C
63              
64             my $address = String::Normal::Type::Address->new;
65              
66             Creates an Address type. Accepts the following named parameters:
67              
68             =back
69              
70             =over 8
71              
72             =item * C
73              
74             Path to text file to override default address stemming.
75              
76             =item * C
77              
78             Path to text file to override default address stop words.
79              
80             =back
81              
82             =over 4
83              
84             =item C
85              
86             my $new_value = $address->transform( $value );
87              
88             Transforms a value according to the rules of a Address type.
89              
90             =back
91              
92             =head1 AUTHOR
93              
94             Jeff Anderson, C<< >>
95              
96             =head1 LICENSE AND COPYRIGHT
97              
98             Copyright 2016 Jeff Anderson.
99              
100             This program is free software; you can redistribute it and/or modify it
101             under the terms of the the Artistic License (2.0). You may obtain a
102             copy of the full license at:
103              
104             L
105              
106             Any use, modification, and distribution of the Standard or Modified
107             Versions is governed by this Artistic License. By using, modifying or
108             distributing the Package, you accept this license. Do not use, modify,
109             or distribute the Package, if you do not accept this license.
110              
111             If your Modified Version has been derived from a Modified Version made
112             by someone other than you, you are nevertheless required to ensure that
113             your Modified Version complies with the requirements of this license.
114              
115             This license does not grant you the right to use any trademark, service
116             mark, tradename, or logo of the Copyright Holder.
117              
118             This license includes the non-exclusive, worldwide, free-of-charge
119             patent license to make, have made, use, offer to sell, sell, import and
120             otherwise transfer the Package with respect to any patent claims
121             licensable by the Copyright Holder that are necessarily infringed by the
122             Package. If you institute patent litigation (including a cross-claim or
123             counterclaim) against any party alleging that the Package constitutes
124             direct or contributory patent infringement, then this Artistic License
125             to you shall terminate on the date that such litigation is filed.
126              
127             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
128             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
129             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
130             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
131             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
132             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
133             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
134             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135              
136             =cut