blib/lib/Dict/FSA.pm

filter: Statementstmt 29.7% 14 / 47 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 4 / 10 Podpod 100% 5 / 5 totaltotal 31.0% 23 / 74 CC 7 SCAR 17.3
CC
7
Cov
24%
DESTROY
12.0
create_dict
6.0
new
6.0
CRAP 5.6
↑ Summary
1 # $Id: FSA.pm,v 1.8 2006/08/22 13:11:43 rousse Exp $
2  
3 package Dict::FSA;
4  
5 =head1 NAME
6  
7 Dict::FSA - FSA wrapper
8  
9 =head1 VERSION
10  
11 Version 0.1.2
12  
13 =head1 DESCRIPTION
14  
15 This module is a perl wrapper around fsa, a set of tools based on finite
16 state automata (http://www.eti.pg.gda.pl/~jandac/fsa.html).
17  
18 =head1 SYNOPSIS
19  
20 use Dict::FSA;
21  
22 Dict::FSA->create_dict($wordlist, $file);
23  
24 my $dict = Dict::FSA->new();
25  
26 $dict->check('foo');
27 $dict->suggest('foo');
28  
29 =cut
30  
31 1 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 our $VERSION = '0.1.2';
37  
38 =head1 Class methods
39  
40 =head2 Dict::FSA->create_dict(I<$wordlist>, I<$file>)
41  
42 Creates a dictionnary from I<$wordlist> suitable for use with fsa, and save
43 it in file I<$file>.
44  
45 =cut
46  
47 0 ▶sub create_dict {
Subroutine create_dict: not called CC=2 SCAR=17.9
Pod documented
48 0 my ($class, $wordlist, $file) = @_;
49 0 ▶ open(FSA, "| fsa_ubuild > $file") or die "Can't run fsa_ubuild: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FSA, "| fsa_ubuild > $file" or die "Can't run fsa_ubuild: $!" 0 0 0
50 0 print FSA join("\n", @{$wordlist});
51 0 close(FSA);
52 }
53  
54 =head1 Constructor
55  
56 =head2 Dict::FSALexed->new(I<$distance>, I<$wordfiles>)
57  
58 Creates and returns a new C<Dict::FSA> object.
59  
60 Optional parameters:
61  
62 =over
63  
64 =item I<$distance>
65  
66 maximum distance for approximated matches
67  
68 =item I<$wordfiles>
69  
70 an hashref of word file to use
71  
72 =back
73  
74 =cut
75  
76 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
77 0 my ($class, $distance, $wordfiles) = @_;
78 0 my $self = bless {
79 _in => IO::Handle->new(),
80 _out => IO::Handle->new()
81 }, $class;
82 0 my $command = "fsa_spell -f -e $distance " . join(" ", map { "-d $_" } @{$wordfiles});
83 0 ▶ open2($self->{_out}, $self->{_in}, "$command") or die "Can't run $command: $!";
Branch: 1 branch
BranchTrueFalseTotal
open2($self->{"_out"}, $self->{"_in"}, "$command") or die "Can't run ${command}: $!" 0 0 0
84 0 return $self;
85 }
86  
87 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
88 0 my ($self) = @_;
89 # close external process handles
90 0 ▶ $self->{_in}->close() if $self->{_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_in"} 0 0 0
91 0 ▶ $self->{_out}->close() if $self->{_out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_out"} 0 0 0
92 }
93  
94 =head1 Methods
95  
96 =head2 $dict->check(I<$word>)
97  
98 Check the dictionnary for exact match of word I<$word>.
99 Returns a true value if word is present in the dictionnary, false otherwise.
100  
101 =cut
102  
103 0 ▶sub check {
Subroutine check: not called CC=2 SCAR=17.9
Pod documented
104 0 my ($self, $word) = @_;
105  
106 0 my @query = $self->query($word);
107 return ($query[0] eq '*not found*') ?
108 0 :
109 0 ▶ grep { /^$word$/ } @query;
Branch: 1 branch
BranchTrueFalseTotal
$query[0] eq '*not found*' ? : 0 0 0
110 }
111  
112 =head2 $dict->suggest(I<$word>)
113  
114 Check the dictionnary for approximate match of word I<$word>.
115 Returns a list of approximated words from the dictionnary, according to
116 parameters passed when creating the object.
117  
118 =cut
119  
120 0 ▶sub suggest {
Subroutine suggest: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $word) = @_;
122  
123 0 my @query = $self->query($word);
124 return ($query[0] eq '*not found*') ?
125 () :
126 0 ▶ grep { ! /^$word$/ } @query;
Branch: 1 branch
BranchTrueFalseTotal
$query[0] eq '*not found*' ? : 0 0 0
127 }
128  
129 =head2 $dict->query(I<$word>)
130  
131 Query the dictionnary for word I<$word>.
132 Returns the raw result of the query, as a list of words.
133  
134 =cut
135  
136 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
137 0 my ($self, $word) = @_;
138  
139 0 my ($in, $out) = ($self->{_in}, $self->{_out});
140 0 print $in $word . "\n";
141 0 my $line = <$out>;
142 0 chomp $line;
143  
144 0 $line =~ s/^$word: //;
145 0 $line =~ tr/^/ /;
146 0 my %seen;
147 0 return grep { ! $seen{$_}++ } split(/, /, $line);
148 }
149  
150 1 1;
↑ Summary