File Coverage

blib/lib/Dict/Lexed.pm
Criterion Covered Total %
statement 12 45 26.6
branch 0 14 0.0
condition 0 8 0.0
subroutine 4 10 40.0
pod 5 5 100.0
total 21 82 25.6


line stmt bran cond sub pod time code
1             # $Id: Lexed.pm,v 1.14 2006/08/22 13:09:14 rousse Exp $
2              
3             package Dict::Lexed;
4              
5             =head1 NAME
6              
7             Dict::Lexed - Lexed wrapper
8              
9             =head1 VERSION
10              
11             Version 0.2.2
12              
13             =head1 DESCRIPTION
14              
15             This module is a perl wrapper around Lexed, a lexicalizer developed at INRIA
16             (http://www.lionel-clement.net/lexed)
17              
18             =head1 SYNOPSIS
19              
20             use Dict::Lexed;
21              
22             Dict::Lexed->create_dict($wordlist);
23              
24             my $dict = Dict::Lexed->new();
25              
26             $dict->check('foo');
27             $dict->suggest('foo');
28              
29             =cut
30              
31 1     1   68675 use IPC::Open2;
  1         4838  
  1         155  
32 1     1   11 use IO::Handle;
  1         2  
  1         42  
33 1     1   7 use strict;
  1         2  
  1         32  
34 1     1   6 use warnings;
  1         2  
  1         729  
35              
36             our $VERSION = '0.2.2';
37              
38             my $unknown = "\001";
39             my $delimiter = "\002";
40              
41             =head1 Class methods
42              
43             =head2 Dict::Lexed->create_dict(I<$wordlist>, I<$options>, I<$mode_options>)
44              
45             Creates a dictionnary from I<$wordlist> suitable for use with lexed.
46              
47             Optional parameters:
48              
49             =over
50              
51             =item I<$options>
52              
53             general options passed to lexed
54              
55             =item I<$mode_options>
56              
57             specific build options passed to lexed
58              
59             =back
60              
61             =cut
62              
63             sub create_dict {
64 0     0 1   my ($class, $wordlist, $options, $mode_options) = @_;
65 0   0       $options ||= "";
66 0   0       $mode_options ||= "";
67 0           my $command = "lexed $options build $mode_options 2>/dev/null";
68 0 0         open(LEXED, "| $command") or die "Can't run $command: $!";
69 0           foreach my $word (@{$wordlist}) {
  0            
70 0           print LEXED $word . "\t" . $word . "\n";
71             }
72 0           close(LEXED);
73             }
74              
75             =head1 Constructor
76              
77             =head2 Dict::Lexed->new(I<$options>, I<$mode_options>)
78              
79             Creates and returns a new C object.
80              
81             Optional parameters:
82              
83             =over
84              
85             =item I<$options>
86              
87             general options passed to lexed
88              
89             =item I<$mode_options>
90              
91             specific consultation options passed to lexed
92              
93             =back
94              
95             =cut
96              
97             sub new {
98 0     0 1   my ($class, $options, $mode_options) = @_;
99 0           my $self = bless {
100             _in => IO::Handle->new(),
101             _out => IO::Handle->new()
102             }, $class;
103 0   0       $options ||= "";
104 0   0       $mode_options ||= "";
105 0           my $command = "lexed $options consult -f '' '$delimiter' '\n' '$unknown' $mode_options 2>/dev/null";
106 0 0         open2($self->{_out}, $self->{_in}, "$command") or die "Can't run $command: $!";
107 0           return $self;
108             }
109              
110             sub DESTROY {
111 0     0     my ($self) = @_;
112             # close external process handles
113 0 0         $self->{_in}->close() if $self->{_in};
114 0 0         $self->{_out}->close() if $self->{_out};
115             }
116              
117             =head1 Methods
118              
119              
120             =head2 $dict->check(I<$word>)
121              
122             Check the dictionnary for exact match of word I<$word>.
123             Returns a true value if word is present in the dictionnary, false otherwise.
124              
125             =cut
126              
127             sub check {
128 0     0 1   my ($self, $word) = @_;
129              
130 0           my @query = $self->query($word);
131 0           return (@query) ?
132 0 0         grep { /^\Q$word\E$/ } @query :
133             0;
134             }
135              
136             =head2 $dict->suggest(I<$word>)
137              
138             Check the dictionnary for approximate match of word I<$word>.
139             Returns a list of approximated words from the dictionnary, according to
140             parameters passed when creating the object.
141              
142             =cut
143              
144             sub suggest {
145 0     0 1   my ($self, $word) = @_;
146              
147 0           my @query = $self->query($word);
148 0           return (@query) ?
149 0 0         grep { ! /^$word$/ } @query :
150             ();
151             }
152              
153             =head2 $dict->query(I<$word>)
154              
155             Query the dictionnary for word I<$word>.
156             Returns the raw result of the query, as a list of words.
157              
158             =cut
159              
160             sub query {
161 0     0 1   my ($self, $word) = @_;
162              
163 0           my ($in, $out) = ($self->{_in}, $self->{_out});
164 0           print $in $word . "\n";
165 0           my $line = <$out>;
166 0           chomp $line;
167              
168 0 0         return $line eq $unknown ?
169             () :
170             split(/$delimiter/, $line);
171             }
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             Copyright (C) 2004, INRIA.
176              
177             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
178              
179             =head1 AUTHOR
180              
181             Guillaume Rousse
182              
183             =cut
184              
185             1;