| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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
|
|
|
1
|
|
83102
|
use IPC::Open2; |
|
|
1
|
|
|
|
|
4933
|
|
|
|
1
|
|
|
|
|
80
|
|
|
32
|
1
|
|
|
1
|
|
10
|
use IO::Handle; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
33
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
31
|
|
|
34
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
621
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub create_dict { |
|
48
|
0
|
|
|
0
|
1
|
|
my ($class, $wordlist, $file) = @_; |
|
49
|
0
|
0
|
|
|
|
|
open(FSA, "| fsa_ubuild > $file") or die "Can't run fsa_ubuild: $!"; |
|
50
|
0
|
|
|
|
|
|
print FSA join("\n", @{$wordlist}); |
|
|
0
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
|
|
sub new { |
|
77
|
0
|
|
|
0
|
1
|
|
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}); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
open2($self->{_out}, $self->{_in}, "$command") or die "Can't run $command: $!"; |
|
84
|
0
|
|
|
|
|
|
return $self; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub DESTROY { |
|
88
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
|
89
|
|
|
|
|
|
|
# close external process handles |
|
90
|
0
|
0
|
|
|
|
|
$self->{_in}->close() if $self->{_in}; |
|
91
|
0
|
0
|
|
|
|
|
$self->{_out}->close() if $self->{_out}; |
|
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
|
|
|
|
|
|
|
sub check { |
|
104
|
0
|
|
|
0
|
1
|
|
my ($self, $word) = @_; |
|
105
|
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my @query = $self->query($word); |
|
107
|
0
|
|
|
|
|
|
return ($query[0] eq '*not found*') ? |
|
108
|
|
|
|
|
|
|
0 : |
|
109
|
0
|
0
|
|
|
|
|
grep { /^$word$/ } @query; |
|
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
|
|
|
|
|
|
|
sub suggest { |
|
121
|
0
|
|
|
0
|
1
|
|
my ($self, $word) = @_; |
|
122
|
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my @query = $self->query($word); |
|
124
|
0
|
|
|
|
|
|
return ($query[0] eq '*not found*') ? |
|
125
|
|
|
|
|
|
|
() : |
|
126
|
0
|
0
|
|
|
|
|
grep { ! /^$word$/ } @query; |
|
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
|
|
|
|
|
|
|
sub query { |
|
137
|
0
|
|
|
0
|
1
|
|
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); |
|
|
0
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |