File Coverage

blib/lib/Lingua/Spelling/Alternative.pm
Criterion Covered Total %
statement 65 103 63.1
branch 13 46 28.2
condition 0 4 0.0
subroutine 7 9 77.7
pod 5 6 83.3
total 90 168 53.5


line stmt bran cond sub pod time code
1             # Documentation and Copyright exist after __END__
2              
3             package Lingua::Spelling::Alternative;
4             require 5.001;
5              
6 1     1   1545 use strict;
  1         1  
  1         43  
7 1     1   6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         1  
  1         171  
8              
9 1     1   5 use Exporter;
  1         6  
  1         2112  
10             $VERSION = '0.02';
11             @ISA = ('Exporter');
12              
13             #@EXPORT = qw();
14             @EXPORT_OK = qw(
15             &alternatives
16             );
17              
18             my $debug=0;
19              
20             #
21             # make new instance of language, get args
22             #
23             sub new {
24 2     2 1 188 my $class = shift;
25 2         5 my $self = {};
26 2         5 bless($self, $class);
27 2         10 $self->{ARGS} = {@_};
28 2         5 $debug = $self->{ARGS}->{DEBUG};
29 2         3 @{$self->{affix_regexp}} = ();
  2         5  
30 2         3 @{$self->{affix_add}} = ();
  2         5  
31 2         3 @{$self->{affix_sub}} = ();
  2         3  
32 2 50       7 $self ? return $self : return undef;
33             }
34              
35              
36             #
37             # load affix file in internal structures
38             #
39              
40             sub load_affix {
41 0     0 1 0 my $self = shift;
42 0         0 my $filename = shift;
43              
44 0         0 my $suffixes=0;
45              
46 0         0 my ($regexp,$add,$sub);
47              
48 0 0       0 print STDERR "reading affix file $filename\n" if ($debug);
49              
50 0 0       0 open (A,$filename) || die "Can't open affix file $filename: $!";
51 0         0 while() {
52 0         0 chomp;
53 0 0       0 next if (/^#|^[\s\t\n\r]*$/);
54              
55 0 0       0 if (/^suffixes/i) {
56 0         0 $suffixes++;
57 0         0 next;
58             }
59              
60 0 0       0 next if (! $suffixes);
61              
62 0 0       0 if (/^flag[\s\t]+\*{0,1}(.):/i) {
63 0         0 undef $regexp;
64 0         0 undef $add;
65 0         0 undef $sub;
66 0         0 next;
67             }
68              
69 0 0       0 if (/^[\s\t]*([^>#]+)>[\s\t]+-([^\,\s\t]+),([^\s\t]+)/) {
    0          
70 0         0 $regexp = $1;
71 0         0 $add = $2;
72 0 0       0 $sub = $3 if ($3 ne "-");
73             } elsif (/^[\s\t]*([^>#]+)>[\s\t]+([^\s\t\#]+)/) {
74 0         0 $regexp = $1;
75 0         0 $sub = $2;
76             }
77              
78             sub nuke_s {
79 0     0 0 0 my $tmp = $_[0];
80 0 0       0 return if (!$tmp);
81             # $tmp=~s/^\s+//g;
82             # $tmp=~s/\s+$//g;
83 0         0 $tmp=~s/\s+//g;
84 0         0 return $tmp;
85             }
86              
87 0         0 push @{$self->{affix_regexp}},nuke_s($regexp);
  0         0  
88 0         0 push @{$self->{affix_add}},nuke_s($add);
  0         0  
89 0         0 push @{$self->{affix_sub}},nuke_s($sub);
  0         0  
90             }
91 0         0 return 1;
92             }
93              
94             #
95             # function for reading raw findaffix output
96             #
97              
98             sub load_findaffix {
99 1     1 1 6 my $self = shift;
100 1         3 my $filename = shift;
101              
102 1 50       5 print STDERR "reading findaffix output $filename\n" if ($debug);
103              
104 1 50       32 open (A,$filename) || die "Can't open findaffix output $filename: $!";
105 1         22 while() {
106 10         14 chomp;
107 10         30 my @line=split(m;/;,$_,4);
108 10 50       29 if ($#line > 2) {
109 10         9 push @{$self->{affix_regexp}},'.';
  10         17  
110 10         11 push @{$self->{affix_sub}},$line[0];
  10         22  
111 10         12 push @{$self->{affix_add}},$line[1];
  10         40  
112             }
113             }
114 1         4 return 1;
115             }
116              
117             #
118             # function which returns original word and all alternatives
119             #
120              
121             sub alternatives {
122 3     3 1 17 my $self = shift;
123 3         189 my @out;
124 3         9 foreach my $word (@_) {
125 4         5 push @out,$word; # save original word
126 4 50       11 next if (length($word) < 3); # cludge: preskoci kratke
127 4         10 for(my $i=0; $i<=$#{$self->{affix_regexp}}; $i++) {
  44         330  
128 40         56 my $regexp = $self->{affix_regexp}[$i];
129 40         51 my $add = $self->{affix_add}[$i];
130 40         207 my $sub = $self->{affix_sub}[$i];
131 40 50 0     65 print STDERR "r:'$regexp'\t-'",$sub||'',"'\t+'",$add||'',"'\n" if ($debug);
      0        
132 40 50       75 next if length($word) < length($sub);
133 40         40 my $tmp_word = $word;
134 40 100       64 if ($sub) {
135 32 50       3611 next if ($word !~ m/$sub$/i);
136 0 0       0 if ($add) {
137 0         0 $tmp_word =~ s/$sub$/$add/i;
138             } else {
139 0         0 $tmp_word =~ s/$sub$//i;
140             }
141             } else {
142 8         178 $tmp_word = $word.$add;
143             }
144 8 50       18 print STDERR "\t ?:$tmp_word\n" if ($debug);
145 8 50       28 if ($tmp_word =~ m/$regexp/ix) {
146             # print "$word -> $tmp_word\t-$sub, +$add, regexp: $regexp\n";
147 8         187 push @out,lc($tmp_word);
148             }
149             }
150             }
151 3         356 return @out;
152             }
153              
154             #
155             # function which return minimal word of all alternatives
156             #
157              
158             sub minimal {
159 1     1 1 711 my $self = shift;
160 1         166 my @out;
161 1         3 foreach my $word (@_) {
162 2         350 my @alt = $self->alternatives($word);
163 2         5 my $minimal = shift @alt;
164 2         3 foreach (@alt) {
165 4 50       12 $minimal=$_ if (length($_) < length($minimal));
166             }
167 2         11 push @out,$minimal;
168             }
169 1         22 return @out;
170             }
171              
172             ###############################################################################
173             1;
174             __END__