| 1 | package Search::Tools::TokenListUtils; | ||||||||||||||||||||||||||||
| 2 | 32 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 32 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 32 | our $VERSION = '1.007'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Search::Tools::TokenListUtils - mixin methods for TokenList and TokenListPP | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my $tokens = $tokenizer->tokenize( $string ); | ||||||||||||||||||||||||||||
| 14 | if ( $tokens->str eq $string) { | ||||||||||||||||||||||||||||
| 15 | print "string is same, before and after tokenize()\n"; | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | else { | ||||||||||||||||||||||||||||
| 18 | warn "I'm filing a bug report against Search::Tools right away!\n"; | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my ($start_pos, $end_pos) = $tokens->get_window( 5, 20 ); | ||||||||||||||||||||||||||||
| 22 | # $start_pos probably == 0 | ||||||||||||||||||||||||||||
| 23 | # $end_pos probably == 25 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $slice = $tokens->get_window_pos( 5, 20 ); | ||||||||||||||||||||||||||||
| 26 | for my $token (@$slice) { | ||||||||||||||||||||||||||||
| 27 | print "token = $token\n"; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Search::Tools::TokenListUtils contains pure-Perl methods inhertited | ||||||||||||||||||||||||||||
| 33 | by both Search::Tools::TokenList and Search::Tools::TokenListPP. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 str | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Returns a serialized version of the TokenList. If you haven't | ||||||||||||||||||||||||||||
| 40 | altered the TokenList since you got it from tokenize(), | ||||||||||||||||||||||||||||
| 41 | then str() returns a scalar string identical to (but not the same as) | ||||||||||||||||||||||||||||
| 42 | the string you passed to tokenize(). | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Both Search::Tools::TokenList and TokenListPP are overloaded | ||||||||||||||||||||||||||||
| 45 | to stringify to the str() value. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | sub str { | ||||||||||||||||||||||||||
|
Subroutine
str: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 12 | my $joiner = shift(@_); | |||||||||||||||||||||||||||
| 52 | 12 | ▶ | if ( !defined $joiner ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 12 | $joiner = ''; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 12 | return join( $joiner, map {"$_"} @{ $self->as_array } ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 get_window( I<pos> [, I<size>, I<as_sentence>] ) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Returns array with two values: I<start> and I<end> positions | ||||||||||||||||||||||||||||
| 61 | for the array of length I<size> on either side of I<pos>. | ||||||||||||||||||||||||||||
| 62 | Like taking a slice of the TokenList. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Note that I<size> is the number of B<tokens> not B<matches>. | ||||||||||||||||||||||||||||
| 65 | So if you're looking for the number of "words", think about | ||||||||||||||||||||||||||||
| 66 | I<size>*2. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Note too that I<size> is the number of B<tokens> on B<one> | ||||||||||||||||||||||||||||
| 69 | side of I<pos>. So the entire window width (length of the returned | ||||||||||||||||||||||||||||
| 70 | slice) is I<size>*2 +/-1. The window is guaranteed to be bounded | ||||||||||||||||||||||||||||
| 71 | by B<matches>. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | If I<as_sentence> is true, the window is shifted to try and match | ||||||||||||||||||||||||||||
| 74 | the first token prior to I<pos> that returns true for is_sentence_start(). | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 77 | ▶ | sub get_window { | ||||||||||||||||||||||||||
|
Subroutine
get_window: called CC=17 SCAR=37.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 77 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 77 | my $pos = shift; | |||||||||||||||||||||||||||
| 81 | 77 | ▶ | if ( !defined $pos ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | croak "pos required"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 77 | ▶ | my $size = int(shift) || 20; | ||||||||||||||||||||||||||
|
Condition50%1/2
int shift() || 20
MC/DC0%0/1
int shift() || 20
int shift()
Truth table50%1/2
A
A= int shift()
| |||||||||||||||||||||||||||||
| 86 | 77 | ▶ | my $as_sentence = shift || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 87 | 77 | my $max_index = $self->len - 1; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 77 | ▶ | if ( $pos > $max_index or $pos < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$pos > $max_index or $pos < 0
MC/DC0%0/2
$pos > $max_index or $pos < 0
$pos > $max_index
$pos < 0
Truth table33%1/3
A or B
A= $pos > $max_indexB= $pos < 0
| |||||||||||||||||||||||||||||
| 90 | 0 | croak "illegal pos value: no such index in TokenList"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #warn "window size $size for pos $pos"; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # get the $size tokens on either side of $tok | ||||||||||||||||||||||||||||
| 96 | 77 | my ( $start, $end ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # is token too close to the top of the stack? | ||||||||||||||||||||||||||||
| 99 | 77 | ▶ | if ( $pos > $size ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 62 | $start = $pos - $size; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # is token too close to the bottom of the stack? | ||||||||||||||||||||||||||||
| 104 | 77 | ▶ | if ( $pos < ( $max_index - $size ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 76 | $end = $pos + $size; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 77 | ▶ | $start ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$start ||= 0
MC/DC100%1/1
$start ||= 0
$start
Truth table100%2/2
A
A= $start
| |||||||||||||||||||||||||||||
| 108 | 77 | ▶ | $end ||= $max_index; | ||||||||||||||||||||||||||
|
Condition66%2/3
$end ||= $max_index
MC/DC0%0/2
$end ||= $max_index
$end
$max_index
Truth table66%2/3
A ||= B
A= $endB= $max_index
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 77 | ▶ | if ($as_sentence) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | my $sentence_starts = $self->get_sentence_starts; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # default to what we have. | ||||||||||||||||||||||||||||
| 114 | 0 | my $start_for_pos = $start; | |||||||||||||||||||||||||||
| 115 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | #warn "looking for sentence_start for start = $start end = $end\n"; | ||||||||||||||||||||||||||||
| 118 | 0 | for (@$sentence_starts) { | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | #warn " $_ [$i]\n"; | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( $_ >= $pos ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $start_for_pos = $sentence_starts->[$i]; | |||||||||||||||||||||||||||
| 123 | 0 | last; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | $i++; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #warn "found $start_for_pos (start = $start end = $end)\n"; | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $start_for_pos != $start ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ( $start_for_pos < $start ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $end -= ( $start - $start_for_pos ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 0 | $end += ( $start_for_pos - $start ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | $start = $start_for_pos; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | #warn "now $start_for_pos (start = $start end = $end)\n"; | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # make sure window starts and ends with is_match | ||||||||||||||||||||||||||||
| 144 | 77 | ▶ | while ( !$self->get_token($start)->is_match ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 31 | $start++; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 77 | ▶ | while ( !$self->get_token($end)->is_match ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 42 | $end--; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | #warn "return $start .. $end"; | ||||||||||||||||||||||||||||
| 153 | #warn "$size ~~ " . ( $end - $start ); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 77 | return ( $start, $end ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 get_window_tokens( I<pos> [, I<size>] ) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Like get_window() but returns an array ref of a slice | ||||||||||||||||||||||||||||
| 161 | of the TokenList containing Tokens. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub get_window_tokens { | ||||||||||||||||||||||||||
|
Subroutine
get_window_tokens: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 0 | my ( $start, $end ) = $self->get_window(@_); | |||||||||||||||||||||||||||
| 168 | 0 | my @slice = (); | |||||||||||||||||||||||||||
| 169 | 0 | for ( $start .. $end ) { | |||||||||||||||||||||||||||
| 170 | 0 | push( @slice, $self->get_token($_) ); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | return \@slice; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 as_sentences([I<stringified>]) | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Returns a reference to an array of arrays, | ||||||||||||||||||||||||||||
| 178 | where each child array is a "sentence" worth of Token objects. | ||||||||||||||||||||||||||||
| 179 | You can stringify each sentence array like: | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | my $sentences = $tokenlist->as_sentences; | ||||||||||||||||||||||||||||
| 182 | for my $s (@$sentences) { | ||||||||||||||||||||||||||||
| 183 | printf("sentence: %s\n", join("", map {"$_"} @$s)); | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | If you pass a single true value to as_sentences(), | ||||||||||||||||||||||||||||
| 187 | then the array returned will consist of plain scalar strings | ||||||||||||||||||||||||||||
| 188 | with whitespace normalized. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | ▶ | sub as_sentences { | ||||||||||||||||||||||||||
|
Subroutine
as_sentences: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | 2 | ▶ | my $stringed = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 195 | 2 | my @sents; | |||||||||||||||||||||||||||
| 196 | my @s; | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # use array method since we do not know the iterator position | ||||||||||||||||||||||||||||
| 199 | 2 | for my $t ( @{ $self->as_array } ) { | |||||||||||||||||||||||||||
| 200 | 144 | ▶ | if ( $t->is_sentence_start ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # if has any, add anonymous copy to master | ||||||||||||||||||||||||||||
| 203 | 8 | ▶ | if (@s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 6 | push @sents, [@s]; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # reset | ||||||||||||||||||||||||||||
| 208 | 8 | @s = (); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # add | ||||||||||||||||||||||||||||
| 212 | 144 | push @s, $t; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 2 | ▶ | if (@s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 2 | push @sents, [@s]; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 2 | ▶ | if ($stringed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 1 | my @stringed; | |||||||||||||||||||||||||||
| 219 | 1 | for my $s (@sents) { | |||||||||||||||||||||||||||
| 220 | 4 | my $str = join( "", map {"$_"} @$s ); | |||||||||||||||||||||||||||
| 221 | 4 | $str =~ s/\s\s+/\ /g; | |||||||||||||||||||||||||||
| 222 | 4 | $str =~ s/\s+$//; | |||||||||||||||||||||||||||
| 223 | 4 | push @stringed, $str; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 1 | return \@stringed; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | return \@sents; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 32 | 1; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||||||||||