blib/lib/Lingua/PT/Abbrev.pm

filter: Statementstmt 50.0% 39 / 78 Branchbran 29.1% 7 / 24 Conditioncond 7.4% 2 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 46.1% 6 / 13 Podpod 100% 7 / 7 totaltotal 37.2% 61 / 164 CC 24 SCAR 23.5
CC
24
Cov
37%
_load_dictionary
17.5
_exists
12.0
_expand
12.0
CRAP 10.5
↑ Summary
1 package Lingua::PT::Abbrev;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding ISO-8859-1
7  
8 =head1 NAME
9  
10 Lingua::PT::Abbrev - An abbreviations dictionary manager for NLP
11  
12 =head1 VERSION
13  
14 Version 0.10
15  
16 =cut
17  
18 3 our $VERSION = '0.10';
19  
20 # dic => system dict
21 # cdic => custom dict
22 # sdic => session dict
23  
24 =head1 SYNOPSIS
25  
26 use Lingua::PT::Abbrev;
27  
28 my $dic = Lingua::PT::Abbrev->new;
29  
30 my $exp = $dic -> expand("sr");
31  
32 $text = $dic -> text_expand($text);
33  
34 =head1 ABSTRACT
35  
36 This module handles a built-in abbreviations dictionary, and a user
37 customized abbreviations dictionary. It provides handy functions for
38 NLP processing.
39  
40 =head1 FUNCTIONS
41  
42 =head2 new
43  
44 This is the Lingua::PT::Abbrev dictionaries constructor. You don't
45 need to pass it any parameter, unless you want to maintain a personal
46 dictionary. In that case, pass the path to your personal dictionary
47 file.
48  
49 The dictionary file is a text file, one abbreviation by line, as:
50  
51 sr senhor
52 sra senhora
53 dr doutor
54  
55 =cut
56  
57 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
58 2 my $class = shift;
59 2 ▶ my $custom = shift || undef;
Condition50%1/2
shift() || undef
l!l
02
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
60 2 my $self = bless { custom => $custom }, $class;
61  
62 2 $self->_load_dictionary;
63 2 ▶ $self->_load_dictionary($custom) if ($custom);
Branch: 1 branch
BranchTrueFalseTotal
if $custom 0 2 2
64  
65 2 return $self;
66 }
67  
68 2 ▶sub _load_dictionary {
Subroutine _load_dictionary: called CC=9 SCAR=28.6
69 2 my $self = shift;
70 2 ▶ my $file = shift || undef;
Condition50%1/2
shift() || undef
l!l
02
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
71  
72 2 local $_;
73  
74 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 2 2
75 0 ▶ open C, $file or die;
Branch: 1 branch
BranchTrueFalseTotal
open C, $file or die 0 0 0
76 0 ▶ while(<C>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline C)) 0 0 0
77 0 chomp;
78 0 ▶ next if m!^\s*$!;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
79 0 ($a,$b) = split /\s+/, lc;
80 0 $self->{cdic}{$a} = $b;
81 }
82 0 close C;
83 } else {
84 2 my $f = _find_file();
85 2 ▶ open D, $f or die "Cannot open file $f: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open D, $f or die "Cannot open file ${f}: $!\n" 2 0 2
86 2 ▶ while(<D>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline D)) 216 2 218
87 216 chomp;
88 216 s/\s*#.*//; # delete comments
89 216 ▶ next if m!^\s*$!;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 216 216
90 216 ($a,$b) = split /\s+/, lc;
91 216 $self->{dic}{$a} = $b;
92 }
93 2 close D;
94 }
95 }
96  
97 =head2 expand
98  
99 Given an abbreviation, this method expands it. For expanding
100 abbreviations in a text use C<<text_expand>>, a lot faster.
101  
102 Returns undef if the abbreviation is not known.
103  
104 =cut
105  
106 0 ▶sub expand {
Subroutine expand: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108 0 my $abbrev = lc(shift);
109 0 $abbrev =~ s!\.$!!;
110 0 ▶ return $self->_expand($abbrev) || undef;
Condition0%0/2
$self->_expand($abbrev) || undef
l!l
00
MC/DC0%0/1
$self->_expand($abbrev) || undef
$self->_expand($abbrev)
Truth table0%0/2
A
A= $self->_expand($abbrev)
Aresult
11
00
111 }
112  
113 0 ▶sub _exists {
Subroutine _exists: not called CC=3 SCAR=24.8
114 0 my $self = shift;
115 0 my $word = shift;
116 return exists($self->{dic}{$word}) ||
117 exists($self->{cdic}{$word}) ||
118 0 ▶ exists($self->{sdic}{$word})
Condition0%0/6
exists $self->{'dic'}{$word} || exists $self->{'cdic'}{$word} || exists $self->{'sdic'}{$word}
l!l&&r!l&&!r
000
exists $self->{"dic"}{$word} or exists $self->{"cdic"}{$word}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $self->{'dic'}{$word} || exists $self->{'cdic'}{$word} || exists $self->{'sdic'}{$word}
exists $self->{"dic"}{$word} exists $self->{"cdic"}{$word} exists $self->{'sdic'}{$word}
Truth table0%0/4
A or B || C
A= exists $self->{"dic"}{$word}B= exists $self->{"cdic"}{$word}C= exists $self->{'sdic'}{$word}
ABCresult
1XX1
01X1
0011
0000
119 }
120  
121 0 ▶sub _expand {
Subroutine _expand: not called CC=3 SCAR=24.8
122 0 my $self = shift;
123 0 my $word = shift;
124 return $self->{sdic}{$word} ||
125 $self->{cdic}{$word} ||
126 0 ▶ $self->{dic}{$word};
Condition0%0/6
$self->{'sdic'}{$word} || $self->{'cdic'}{$word} || $self->{'dic'}{$word}
l!l&&r!l&&!r
000
$self->{"sdic"}{$word} or $self->{"cdic"}{$word}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'sdic'}{$word} || $self->{'cdic'}{$word} || $self->{'dic'}{$word}
$self->{"sdic"}{$word} $self->{"cdic"}{$word} $self->{'dic'}{$word}
Truth table0%0/4
A or B || C
A= $self->{"sdic"}{$word}B= $self->{"cdic"}{$word}C= $self->{'dic'}{$word}
ABCresult
1XX1
01X1
0011
0000
127 }
128  
129 =head2 text_expand
130  
131 Given a text, this method expands all known abbreviations
132  
133 =cut
134  
135 0 ▶sub text_expand {
Subroutine text_expand: not called CC=2 SCAR=17.9
Pod documented
136 0 my $self = shift;
137 0 my $text = shift;
138  
139 0 $text =~ s{((\w+)\.)}{
140 0 ▶ $self->_expand(lc($2))||$1
Condition0%0/3
$self->_expand(lc $2) || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_expand(lc $2) || $1
$self->_expand(lc $2) $1
Truth table0%0/3
A || B
A= $self->_expand(lc $2)B= $1
ABresult
1X1
011
000
141 }eg;
142  
143 0 return $text;
144 }
145  
146 =head2 add
147  
148 Use this method to add an abbreviation to your current dictionary.
149  
150 $abrev -> add( "dr" => "Doutor" );
151  
152 =cut
153  
154 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod documented
155 0 my ($self,$abr,$exp) = @_;
156 0 ▶ return undef unless $abr and $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless $abr and $exp 0 0 0
Condition0%0/3
$abr and $exp
!ll&&!rl&&r
000
MC/DC0%0/2
$abr and $exp
$abr $exp
Truth table0%0/3
A and B
A= $abrB= $exp
ABresult
0X0
100
111
157 0 $self->{cdic}{lc($abr)} = lc($exp);
158 }
159  
160 =head2 session_add
161  
162 Use this method to add an abbreviation to your session dictionary.
163  
164 $abrev -> session_add( "dr" => "Doutor" );
165  
166 =cut
167  
168 0 ▶sub session_add {
Subroutine session_add: not called CC=3 SCAR=24.8
Pod documented
169 0 my ($self,$abr,$exp) = @_;
170 0 ▶ return undef unless $abr and $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless $abr and $exp 0 0 0
Condition0%0/3
$abr and $exp
!ll&&!rl&&r
000
MC/DC0%0/2
$abr and $exp
$abr $exp
Truth table0%0/3
A and B
A= $abrB= $exp
ABresult
0X0
100
111
171 0 $self->{sdic}{lc($abr)} = lc($exp);
172 }
173  
174 =head2 save
175  
176 This method saves the custom dictionary
177  
178 =cut
179  
180 0 ▶sub save {
Subroutine save: not called CC=3 SCAR=24.8
Pod documented
181 0 my $self = shift;
182 0 ▶ open DIC, ">$self->{custom}" or die;
Branch: 1 branch
BranchTrueFalseTotal
open DIC, qq[>$self->{"custom"}] or die 0 0 0
183 0 for (keys %{$self->{cdic}}) {
184 0 print DIC "$_ $self->{cdic}{$_}\n";
185 }
186 0 close DIC;
187 }
188  
189  
190 =head2 regexp
191  
192 This method returns a regular expression matching all abbreviations.
193 Pass as option an hash table for configuration.
194  
195 The key C<<nodot>> is used to define a regular expression not
196 containing the final dot.
197  
198 =cut
199  
200 1 ▶sub regexp {
Subroutine regexp: called CC=2 SCAR=8.1
Pod documented
201 1 my $self = shift;
202 1 my %conf = @_;
203 1 ▶ if ($conf{nodot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{'nodot'}) { } 0 1 1
204 0 my $re = "(?:".join("|",keys %{$self->{dic}}, keys %{$self->{cdic}}, keys %{$self->{sdic}}).")";
205 0 return qr/$re/i;
206 } else {
207 1 my $re = "(?:".join("|",keys %{$self->{dic}}, keys %{$self->{cdic}}, keys %{$self->{sdic}}).")\\.";
208 1 return qr/$re/i;
209 }
210 }
211  
212  
213 2 ▶sub _find_file {
Subroutine _find_file: called CC=1 SCAR=0.0
214 2 my @files = grep { -e $_ } map { "$_/Lingua/PT/Abbrev/abbrev.dat" } @INC;
215 2 return $files[0];
216 }
217  
218 =head1 AUTHOR
219  
220 Alberto Simões, C<< <ambs@cpan.org> >>
221  
222 =head1 BUGS
223  
224 Please report any bugs or feature requests to
225 C<bug-lingua-pt-abbrev@rt.cpan.org>, or through the web interface at
226 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
227 be notified of progress on your bug as I make changes.
228  
229 =head1 COPYRIGHT & LICENSE
230  
231 Copyright 2004-2005 Alberto Simões, All Rights Reserved.
232  
233 This program is free software; you can redistribute it and/or modify it
234 under the same terms as Perl itself.
235  
236 =cut
237  
238 3 1; # End of Lingua::PT::Abbrev
239  
↑ Summary