| 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 | ▶ | 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.2.2'; | |||||||||||
| 37 | |||||||||||||
| 38 | 1 | my $unknown = "\001"; | |||||||||||
| 39 | 1 | 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 | 0 | ▶ | sub create_dict { | ||||||||||
|
Subroutine
create_dict: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 64 | 0 | my ($class, $wordlist, $options, $mode_options) = @_; | |||||||||||
| 65 | 0 | ▶ | $options ||= ""; | ||||||||||
|
Condition0%0/2
$options ||= ""
MC/DC0%0/1
$options ||= ""
$options
Truth table0%0/2
A
A= $options
| |||||||||||||
| 66 | 0 | ▶ | $mode_options ||= ""; | ||||||||||
|
Condition0%0/2
$mode_options ||= ""
MC/DC0%0/1
$mode_options ||= ""
$mode_options
Truth table0%0/2
A
A= $mode_options
| |||||||||||||
| 67 | 0 | my $command = "lexed $options build $mode_options 2>/dev/null"; | |||||||||||
| 68 | 0 | ▶ | open(LEXED, "| $command") or die "Can't run $command: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | foreach my $word (@{$wordlist}) { | |||||||||||
| 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<Dict::Lexed> 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 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 98 | 0 | my ($class, $options, $mode_options) = @_; | |||||||||||
| 99 | 0 | my $self = bless { | |||||||||||
| 100 | _in => IO::Handle->new(), | ||||||||||||
| 101 | _out => IO::Handle->new() | ||||||||||||
| 102 | }, $class; | ||||||||||||
| 103 | 0 | ▶ | $options ||= ""; | ||||||||||
|
Condition0%0/2
$options ||= ""
MC/DC0%0/1
$options ||= ""
$options
Truth table0%0/2
A
A= $options
| |||||||||||||
| 104 | 0 | ▶ | $mode_options ||= ""; | ||||||||||
|
Condition0%0/2
$mode_options ||= ""
MC/DC0%0/1
$mode_options ||= ""
$mode_options
Truth table0%0/2
A
A= $mode_options
| |||||||||||||
| 105 | 0 | my $command = "lexed $options consult -f '' '$delimiter' '\n' '$unknown' $mode_options 2>/dev/null"; | |||||||||||
| 106 | 0 | ▶ | open2($self->{_out}, $self->{_in}, "$command") or die "Can't run $command: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | return $self; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||
| 111 | 0 | my ($self) = @_; | |||||||||||
| 112 | # close external process handles | ||||||||||||
| 113 | 0 | ▶ | $self->{_in}->close() if $self->{_in}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | ▶ | $self->{_out}->close() if $self->{_out}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | sub check { | ||||||||||
|
Subroutine
check: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 128 | 0 | my ($self, $word) = @_; | |||||||||||
| 129 | |||||||||||||
| 130 | 0 | my @query = $self->query($word); | |||||||||||
| 131 | return (@query) ? | ||||||||||||
| 132 | 0 | ▶ | grep { /^\Q$word\E$/ } @query : | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | sub suggest { | ||||||||||
|
Subroutine
suggest: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 145 | 0 | my ($self, $word) = @_; | |||||||||||
| 146 | |||||||||||||
| 147 | 0 | my @query = $self->query($word); | |||||||||||
| 148 | return (@query) ? | ||||||||||||
| 149 | 0 | ▶ | grep { ! /^$word$/ } @query : | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | sub query { | ||||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 161 | 0 | 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 | ▶ | return $line eq $unknown ? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 <grousse@cpan.org> | ||||||||||||
| 182 | |||||||||||||
| 183 | =cut | ||||||||||||
| 184 | |||||||||||||
| 185 | 1 | 1; | |||||||||||