| 1 | package Tree::Lexicon; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | Tree::Lexicon - Object class for storing and retrieving a lexicon in a tree of affixes | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | our @EXPORT_OK = qw( cs_regexp ci_regexp ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Version 0.01 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | use Tree::Lexicon; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | my $lexicon = Tree::Lexicon->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | $lexicon->insert( 'apply', '', 'Apple', 'Windows', 'Linux', 'app', 'all day' ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # Warns of strings not matching /^\w+/ without inserting | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | if ($lexicon->contains( 'WiNdOwS' )) { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | $lexicon->remove( 'wInDoWs' ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | $lexicon->insert( 'Vista' ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | my @words = $lexicon->vocabulary; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 44 | @words = ( 'Apple', 'Linux', 'Windows', 'app', 'apply' ); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | @words = $lexicon->auto_complete( 'ap' ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 48 | @words = ( 'app', 'apply' ); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | my $regexp = $lexicon->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 52 | $regexp = qr/\b(?:Apple|Linux|Windows|app(?:ly)?)\b/; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | my $caseless->Tree::Lexicon->new( 0 )->insert( 'apply', '', 'Apple', 'Windows', 'Linux', 'app', 'all day' ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # Warns of strings not matching /^\w+/ without inserting | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | if ($caseless->contains( 'WiNdOwS' )) { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | $caseless->remove( 'wInDoWs' ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | $caseless->insert( 'Vista' ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | @words = $caseless->vocabulary; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 64 | @words = ( 'APP', 'APPLE', 'APPLY', 'LINUX', 'VISTA' ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | @words = $caseless->auto_complete( 'ap' ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 68 | @words = ( 'APP', 'APPLE', 'APPLY' ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | my $regexp = $caseless->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 72 | $regexp = qr/\b(?:[Aa][Pp[Pp](?:[Ll](?:[Ee]|[Yy]))?|[Ll][Ii][Nn][Uu][X]|[Vv][Ii][Ss][Tt][Aa])\b/; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | use Tree::Lexicon qw( cs_regexp ci_regexp ); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | my $cs_regexp = cs_regexp( @words ); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 78 | $cs_regexp = Tree::Lexicon->new()->insert( @words )->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | my $ci_regexp = ci_regexp( @words ); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $ci_regexp = Tree::Lexicon->new( 0 )->insert( @words )->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | The purpose of this module is to provide a simple and effective means to store a lexicon. It is intended to aid parsers in identifying keywords and interactive applications in identifying user-provided words. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =head1 EXPORT | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =head2 cs_regexp | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | Convenience function for generating a case sensitive regular expression from list of words. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | my $cs_regexp = cs_regexp( @words ); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | $cs_regexp = Tree::Lexicon->new( 1 )->insert( @words )->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | sub cs_regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cs_regexp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 2 | return Tree::Lexicon->new()->insert( @_ )->as_regexp(); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =head2 ci_regexp | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | Convenience function for generating a case insensitive regular expression from list of words. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | my $ci_regexp = cs_regexp( @words ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 111 | $ci_regexp = Tree::Lexicon->new( 0 )->insert( @words )->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | sub ci_regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ci_regexp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | return Tree::Lexicon->new( 0 )->insert( @_ )->as_regexp(); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | Passing a string not matching C</^\w+/> as an argument to L<C<insert>|/insert>, L<C<remove>|/remove>, L<C<contains>|/contains> or L<C<auto_complete>|/auto_complete> yields a warning to STDERR and nothing else. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | Returns a new empty C<Tree::Lexicon> object. By default, the tree's contents are case-sensitive. Passing a single I<false> argument to the constuctor makes its contents case-insensitive. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | $lexicon = Tree::Lexicon->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # Same as: | ||||||||||||||||||||||||||||||||||||||||||
| 129 | $lexicon = Tree::Lexicon->new( 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # or # | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | $lexicon = Tree::Lexicon->new( 0 ); | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 10 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 10 | my $cs = shift; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 10 | ▶ | $cs = (defined $cs) ? $cs ? 1 : '' : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 10 | return bless { CASE => $cs, NODES => [] }; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 insert | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Inserts zero or more words into the lexicon tree and returns the object. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | $lexicon->insert( 'list', 'of', 'words' ); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | If you already have an initial list of words, then you can chain this method up with the constructor. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | my $lexicon = Tree::Lexicon->new()->insert( @words ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # Insert words | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 372 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 372 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 372 | ▶ | if ($self->{CASE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 252 | foreach (@_) { _insert( $self->{NODES}, $_ ); } | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 120 | foreach (@_) { _insert( $self->{NODES}, uc( $_ ) ); } | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 372 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =head2 remove | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | Removes zero or more words from the lexicon tree and returns them (or C<undef> if not found). | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | @removed = $lexicon->remove( 'these', 'words' ); | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # Remove words | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 364 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=5 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 364 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | return (wantarray or (@_ > 1)) ? | ||||||||||||||||||||||||||||||||||||||||||
| 188 | ($self->{CASE}) ? | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | map { _remove( $self->{NODES}, $_ ) } @_ : | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | map { _remove( $self->{NODES}, uc( $_ ) ) } @_ : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | ($self->{CASE}) ? | ||||||||||||||||||||||||||||||||||||||||||
| 192 | _remove( $self->{NODES}, shift ) : | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 364 | ▶ | _remove( $self->{NODES}, uc( shift )); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
wantarray or @_ > 1
MC/DC0%0/2
wantarray or @_ > 1
wantarray
@_ > 1
Truth table33%1/3
A or B
A= wantarrayB= @_ > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =head2 contains | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Returns C<1> or C<''> for each word as to its presence or absense, respectively. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | @verify = $lexicon->contains( 'these', 'words' ); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # Verify words | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 1456 | ▶ | sub contains { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=5 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 1456 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | return (wantarray or (@_ > 1)) ? | ||||||||||||||||||||||||||||||||||||||||||
| 210 | ($self->{CASE}) ? | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | map { _contains( $self->{NODES}, $_ ) } @_ : | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | map { _contains( $self->{NODES}, uc( $_ ) ) } @_ : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | ($self->{CASE}) ? | ||||||||||||||||||||||||||||||||||||||||||
| 214 | _contains( $self->{NODES}, shift ) : | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 1456 | ▶ | _contains( $self->{NODES}, uc( shift )); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
wantarray or @_ > 1
MC/DC0%0/2
wantarray or @_ > 1
wantarray
@_ > 1
Truth table33%1/3
A or B
A= wantarrayB= @_ > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =head2 auto_complete | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | Returns all words beginning with the string passed. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | @words = $lexicon->auto_complete( 'a' ); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # Words beginning with | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 26 | ▶ | sub auto_complete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auto_complete: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 26 | my $prefix = shift; | |||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 26 | ▶ | unless ($prefix and $prefix =~ /^\w+/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$prefix and $prefix =~ /^\w+/
MC/DC0%0/2
$prefix and $prefix =~ /^\w+/
$prefix
$prefix =~ /^\w+/
Truth table33%1/3
A and B
A= $prefixB= $prefix =~ /^\w+/
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | carp "Cannot auto-complete non-word string."; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 26 | ▶ | ($self->{CASE}) or $prefix = uc $prefix; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 26 | my ($node, $root) = _ac_first( $self->{NODES}, $prefix ); | |||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 26 | ▶ | (defined $node) or return (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 21 | my @words = _vocabulary( $node->[-1], $root ); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 21 | ▶ | ($node->[1]) and unshift @words, $root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 21 | return @words; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | =head2 vocabulary | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | Returns all words in the lexicon. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | @words = $lexicon->vocabulary(); | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | # All words | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | ▶ | sub vocabulary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vocabulary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | return _vocabulary( $self->{NODES} ); | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | =head2 as_regexp | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | Returns a regular expression equivalent to the lexicon tree. The regular expression has the form C<qr/\b(?: ... )\b/>. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | $regexp = $lexicon->as_regexp(); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Lexicon as regular expression | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 736 | ▶ | sub as_regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_regexp: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 736 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 277 | my $regexp = $self->{CASE} ? | ||||||||||||||||||||||||||||||||||||||||||
| 278 | _cs_regexp( $self->{NODES} ) : | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 736 | ▶ | _ci_regexp( $self->{NODES} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 736 | return qr/\b(?:$regexp)\b/; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | ## Begin Private Functions ## | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | # Recursive backend for 'insert()' | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 4323 | ▶ | sub _insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 4323 | my ($nodes, | |||||||||||||||||||||||||||||||||||||||||
| 289 | $string) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | 4323 | ▶ | unless ($string and $string =~ /^\w+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$string and $string =~ /^\w+$/
MC/DC0%0/2
$string and $string =~ /^\w+$/
$string
$string =~ /^\w+$/
Truth table33%1/3
A and B
A= $stringB= $string =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | carp "Cannot insert non-word string into lexicon tree."; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | # Node location and possible common root | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 4323 | my ($node, | |||||||||||||||||||||||||||||||||||||||||
| 298 | $pos, | ||||||||||||||||||||||||||||||||||||||||||
| 299 | $root) = _locate( $nodes, $string ); | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # Is there a common root to node's string and passed string? | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 4323 | ▶ | if ($root) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Are they equal? | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 2594 | ▶ | if ($string eq $node->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 24 | $node->[1] = 1; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # Strip the common root from $string | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 2570 | $string =~ s/^$root//; | |||||||||||||||||||||||||||||||||||||||||
| 310 | # Is common root same as node's string? | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 2570 | ▶ | unless ($node->[0] eq $root) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | # No: split node upwards | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 629 | $node->[0] =~ s/^$root//; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 629 | $node = [ $root, !$string, [ $node ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 629 | $nodes->[$pos] = $node; | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # Recurse with what's left of $string | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 2570 | ▶ | _insert( $node->[-1], $string ) if ($string); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | # This is a node with no root in common with its neighbors | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 1729 | splice( @{$nodes}, $pos, 0, [ $string, 1, [] ] ); | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | # Backend for 'remove()' | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 364 | ▶ | sub _remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_remove: called CC=12 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 364 | my ($nodes, | |||||||||||||||||||||||||||||||||||||||||
| 330 | $sought) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 364 | my $found; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 364 | my @stack = (); | |||||||||||||||||||||||||||||||||||||||||
| 333 | 364 | my ($node, | |||||||||||||||||||||||||||||||||||||||||
| 334 | $pos, | ||||||||||||||||||||||||||||||||||||||||||
| 335 | $root); | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 364 | ▶ | unless ($sought and $sought =~ /^\w+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sought and $sought =~ /^\w+$/
MC/DC0%0/2
$sought and $sought =~ /^\w+$/
$sought
$sought =~ /^\w+$/
Truth table33%1/3
A and B
A= $soughtB= $sought =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | carp "Cannot remove non-word string from lexicon tree."; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | # Search tree, stripping sought of roots and appending to found | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 364 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$sought and $found
MC/DC50%1/2
$sought and $found
$sought
$found
Truth table66%2/3
A and B
A= $soughtB= $found
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 1055 | ($node, $pos, $root) = _locate( $nodes, $sought ); | |||||||||||||||||||||||||||||||||||||||||
| 345 | # Is there a node whose string can be stripped from string? | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 1055 | ▶ | if ($root eq $node->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | # Add to what was found | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 1055 | $found .= $root; | |||||||||||||||||||||||||||||||||||||||||
| 349 | # Strip what was found | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 1055 | $sought =~ s/^$root//; | |||||||||||||||||||||||||||||||||||||||||
| 351 | # Is there more to search? | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1055 | ▶ | if ($sought) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | # Record visit | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 691 | push @stack, [ $node, $nodes, $pos ]; | |||||||||||||||||||||||||||||||||||||||||
| 355 | # Recurse | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 691 | $nodes = $node->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # Verify that $found is a "hit" | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 364 | ▶ | ($node->[1]) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | $found = undef; | ||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $found = undef; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | } while ($sought and $found); | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 364 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 364 | $node->[1] = ''; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 364 | ▶ | until ($node->[1] || @{$node->[-1]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->[1] or @{$node->[-1];}
MC/DC100%2/2
$node->[1] or @{$node->[-1];}
$node->[1]
@{$node->[-1];}
Truth table100%3/3
A or B
A= $node->[1]B= @{$node->[-1];}
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 473 | splice( @{$nodes}, $pos, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 373 | 473 | ▶ | last unless (@stack); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 419 | ($node, $nodes, $pos) = @{pop @stack}; | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 364 | return $found; | |||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | # Backend for 'contains()' | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 1456 | ▶ | sub _contains { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_contains: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 1456 | my ($nodes, | |||||||||||||||||||||||||||||||||||||||||
| 384 | $sought) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 1456 | my ($node, | |||||||||||||||||||||||||||||||||||||||||
| 386 | $pos, | ||||||||||||||||||||||||||||||||||||||||||
| 387 | $root); | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | 1456 | ▶ | unless ($sought and $sought =~ /^\w+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sought and $sought =~ /^\w+$/
MC/DC0%0/2
$sought and $sought =~ /^\w+$/
$sought
$sought =~ /^\w+$/
Truth table33%1/3
A and B
A= $soughtB= $sought =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | carp "Cannot find non-word string in lexicon tree."; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | # Search tree, stripping string of roots | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 1456 | ▶ | while ($sought) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 3615 | ($node, $pos, $root) = _locate( $nodes, $sought ); | |||||||||||||||||||||||||||||||||||||||||
| 397 | # Is there a node whose string can be stripped from string? | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 3615 | ▶ | last unless ($node and $root eq $node->[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node and $root eq $node->[0]
MC/DC100%2/2
$node and $root eq $node->[0]
$node
$root eq $node->[0]
Truth table100%3/3
A and B
A= $nodeB= $root eq $node->[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 2920 | $sought =~ s/^$root//; | |||||||||||||||||||||||||||||||||||||||||
| 400 | # Recurse? | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 2920 | ▶ | ($sought) and $nodes = $node->[-1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 1456 | ▶ | return (not $sought and $node->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!$sought && $node->[1]
MC/DC100%2/2
!$sought && $node->[1]
!$sought
$node->[1]
Truth table100%3/3
not(A) && B
A= !$soughtB= $node->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | # Recursive backend for 'vocabulary()' | ||||||||||||||||||||||||||||||||||||||||||
| 408 | 624 | ▶ | sub _vocabulary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_vocabulary: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 624 | my $nodes = shift; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 624 | ▶ | my $root = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 624 | my @vocab; | |||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 624 | foreach my $node (@{$nodes}) { | |||||||||||||||||||||||||||||||||||||||||
| 414 | 601 | my $ext_root = $root.$node->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 601 | ▶ | ($node->[1]) and push @vocab, $ext_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 601 | push @vocab, _vocabulary( $node->[-1], $ext_root ); | |||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 624 | return @vocab; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | # Case sensitive recursive backend for 'as_regexp()' | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 31709 | ▶ | sub _cs_regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cs_regexp: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | join( '|', map { | ||||||||||||||||||||||||||||||||||||||||||
| 425 | $_->[0].( | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 88801 | ▶ | (@{$_->[-1]}) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | '(?:'._cs_regexp( $_->[-1] ).')'.( | ||||||||||||||||||||||||||||||||||||||||||
| 428 | ($_->[1]) ? '?' : '' | ||||||||||||||||||||||||||||||||||||||||||
| 429 | ) : '' | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 31709 | ) } @{$_[0]} ) | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | # Case insensitive recursive backend for 'as_regexp()' | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 7040 | ▶ | sub _ci_regexp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ci_regexp: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | join( '|', map { | ||||||||||||||||||||||||||||||||||||||||||
| 436 | _ci_seq( $_->[0] ).( | ||||||||||||||||||||||||||||||||||||||||||
| 437 | 19885 | ▶ | (@{$_->[-1]}) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | '(?:'._ci_regexp( $_->[-1] ).')'.( | ||||||||||||||||||||||||||||||||||||||||||
| 439 | ($_->[1]) ? '?' : '' | ||||||||||||||||||||||||||||||||||||||||||
| 440 | ) : '' | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 7040 | ) } @{$_[0]} ) | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | # Begin Helper Functions # | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | # Greatest common root of two strings (called by '_locate()') | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 9561 | ▶ | sub _gc_root { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gc_root: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 9561 | my ($string1, | |||||||||||||||||||||||||||||||||||||||||
| 449 | $string2) = @_; # ( $string1 le $string2 ) | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | # Does $string2 begin with $string1? | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 9561 | ▶ | ($string2 =~ /^$string1/) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | return $string1; | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | # First character of $string1 | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 3510 | my $root = substr( $string1, 0, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | # Does $string2 begin with the same character? | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 3510 | ▶ | ($string2 =~ /^$root/) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | return ''; | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | # Append characters from $string1 to root ... | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 678 | ▶ | for (my $i = 1; $i < length( $string1 ); $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 989 | $root .= substr( $string1, $i, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 465 | # ... until it no longer matches $string2. | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 989 | ▶ | unless ($string2 =~ /^$root/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 678 | $root = substr( $root, 0, $i ); | |||||||||||||||||||||||||||||||||||||||||
| 468 | 678 | last; | |||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 678 | return $root; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | # Get position within array and common root of node string and passed string | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 9019 | ▶ | sub _locate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_locate: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 9019 | my ($nodes, | |||||||||||||||||||||||||||||||||||||||||
| 478 | $sought) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 479 | my ($min, | ||||||||||||||||||||||||||||||||||||||||||
| 480 | 9019 | $pos) = ( -1, @{$nodes} - 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 481 | 9019 | my $root = ''; | |||||||||||||||||||||||||||||||||||||||||
| 482 | 9019 | my $node; | |||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | # Binary search from above | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 9019 | ▶ | while ($pos > $min and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pos > $min and $sought lt $nodes->[$pos][0]
MC/DC100%2/2
$pos > $min and $sought lt $nodes->[$pos][0]
$pos > $min
$sought lt $nodes->[$pos][0]
Truth table100%3/3
A and B
A= $pos > $minB= $sought lt $nodes->[$pos][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | $sought lt $nodes->[$pos]->[0]) { | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 16497 | my $mid = $pos + ($min - $pos) / 2; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 16497 | ▶ | if ($sought lt $nodes->[$mid]->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 9787 | $pos = $mid - 1; | |||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 491 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 6710 | $min = $mid; | |||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | # Value of $pos is position of greatest | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # less-than-or-equal node, possibly -1 | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | # Is there a less-than-or-equal node with a common root? | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 9019 | ▶ | unless ($pos >= 0 and $node = $nodes->[$pos] and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$pos >= 0 and $node = $nodes->[$pos] and $root = _gc_root($node->[0], $sought)
$pos >= 0 and $node = $nodes->[$pos]
MC/DC66%2/3
$pos >= 0 and $node = $nodes->[$pos] and $root = _gc_root($node->[0], $sought)
$pos >= 0
$node = $nodes->[$pos]
$root = _gc_root($node->[0], $sought)
Truth table75%3/4
A and B and C
A= $pos >= 0B= $node = $nodes->[$pos]C= $root = _gc_root($node->[0], $sought)
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | $root = _gc_root( $node->[0], $sought )) { | ||||||||||||||||||||||||||||||||||||||||||
| 502 | # No | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 2689 | $pos++; | |||||||||||||||||||||||||||||||||||||||||
| 504 | # Value of $pos is position of least | ||||||||||||||||||||||||||||||||||||||||||
| 505 | # greater-than-or-equal node, possibly scalar( @{$nodes} ) | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | # Is there a greater-than-or-equal node? with a common root? | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 2689 | ▶ | ($pos < scalar( @{$nodes} )) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pos < scalar @{$nodes;} and $node = $nodes->[$pos]
MC/DC50%1/2
$pos < scalar @{$nodes;} and $node = $nodes->[$pos]
$pos < scalar @{$nodes;}
$node = $nodes->[$pos]
Truth table66%2/3
A and B
A= $pos < scalar @{$nodes;}B= $node = $nodes->[$pos]
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | $node = $nodes->[$pos] and | ||||||||||||||||||||||||||||||||||||||||||
| 510 | $root = _gc_root( $sought, $node->[0] ); | ||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 9019 | return ($node, $pos, $root); | |||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | # Recursive backend to seed 'auto_complete()' | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 26 | ▶ | sub _ac_first { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ac_first: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 26 | my ($nodes, | |||||||||||||||||||||||||||||||||||||||||
| 519 | $string, | ||||||||||||||||||||||||||||||||||||||||||
| 520 | $ext_root) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 521 | 26 | my ($node, | |||||||||||||||||||||||||||||||||||||||||
| 522 | $pos, | ||||||||||||||||||||||||||||||||||||||||||
| 523 | $root) = _locate( $nodes, $string ); | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 26 | ▶ | ($ext_root) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | $ext_root = ''; | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | # Is there a node in common to string? | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 26 | ▶ | if ($root) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | # Yes: extened the recorded root thus far | ||||||||||||||||||||||||||||||||||||||||||
| 530 | 21 | $ext_root .= $node->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | # Does string terminate in node's string? | ||||||||||||||||||||||||||||||||||||||||||
| 533 | 21 | ▶ | ($node->[0] =~ /^$string/) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | return ( $node, $ext_root ); | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | # Does string exceed node's string? | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | ▶ | ($string =~ s/^$node->[0]//) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | return _ac_first( $node->[-1], $string, $ext_root ); | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | # Else, fall through | ||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 5 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | # Convert string to case insensistive sequence (called by '_ci_regexp()') | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 19885 | ▶ | sub _ci_seq { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ci_seq: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 19885 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 549 | 19885 | my $lc = lc $str; | |||||||||||||||||||||||||||||||||||||||||
| 550 | 19885 | my $ci_seq; | |||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | 19885 | ▶ | ($lc eq $str) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | return $str; | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | 19689 | my @lc_chars = split( //, $lc, -1 ); | |||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | 19689 | foreach my $uc_char (split( //, $str, -1 )) { | |||||||||||||||||||||||||||||||||||||||||
| 558 | 69077 | my $lc_char = shift @lc_chars; | |||||||||||||||||||||||||||||||||||||||||
| 559 | 69077 | ▶ | $ci_seq .= ($lc_char eq $uc_char) ? $uc_char : "[$uc_char$lc_char]"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | 19689 | return $ci_seq; | |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | # End Helper Functions # | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | ## End Private Functions ## | ||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | S. Randall Sawyer, C<< <srandalls at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | Please report any bugs or feature requests to C<bug-tree-lexicon at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 576 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tree-Lexicon>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 577 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | perldoc Tree::Lexicon | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Lexicon> | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | L<http://annocpan.org/dist/Tree-Lexicon> | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | L<http://cpanratings.perl.org/d/Tree-Lexicon> | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | L<http://search.cpan.org/dist/Tree-Lexicon/> | ||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | =head1 ACKNOWLEDGMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | This module's framework generated with L<C<module-starter>|Module::Starter>. | ||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||
| 613 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | Copyright 2013 S. Randall Sawyer. | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 618 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||||||||||||||||
| 619 | copy of the full license at: | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | __END__ | ||||||||||||||||||||||||||||||||||||||||||