| 1 | package Tie::Hash::Abbrev::BibRefs; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Tie::Hash::Abbrev::BibRefs - match bibliographic references to the original titles | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Tie::Hash::Abbrev::BibRefs; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | tie my %hash, 'Tie::Hash::Abbrev::BibRefs', | ||||||||||||||||||||||||||||
| 12 | preprocess => sub { s/\s+[[:upper:]]:.*// }, | ||||||||||||||||||||||||||||
| 13 | stopwords => [ qw( a and de del der des di | ||||||||||||||||||||||||||||
| 14 | et for für i if in la las | ||||||||||||||||||||||||||||
| 15 | of on part Part Pt. Sect. | ||||||||||||||||||||||||||||
| 16 | the to und ) ], | ||||||||||||||||||||||||||||
| 17 | exceptions => { jpn => 'japan', | ||||||||||||||||||||||||||||
| 18 | natl => 'national' }; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $hash{'Physical Review B'} = '0163-1829'; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | print $hash{'Phys. Rev. B: Condens. Matter Mater. Phys.'}; | ||||||||||||||||||||||||||||
| 23 | # will print '0163-1829' | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This module is an attempt to ease the mapping of often abbreviated | ||||||||||||||||||||||||||||
| 28 | bibliographical references to the original titles. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | To achieve this, it simplyfies the title according to parameterizable rules and | ||||||||||||||||||||||||||||
| 31 | stores it as a I<normalized key>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | When accessing the hash, the key given is also L<normalized|/"KEY NORMALIZATION"> | ||||||||||||||||||||||||||||
| 34 | and compared to the normalized version of the original title. | ||||||||||||||||||||||||||||
| 35 | In addition, each word (words are separated by whitespace) may be abbreviated by | ||||||||||||||||||||||||||||
| 36 | specifying only the first few letters. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | If more than one matching hash entry is found, the values of all matching | ||||||||||||||||||||||||||||
| 39 | entries are compared; as long as they are all | ||||||||||||||||||||||||||||
| 40 | L<eq|perlop/"Equality Operators">ual (or all L<undef|perlfunc/undef>), the | ||||||||||||||||||||||||||||
| 41 | lookup is still considered to be successful. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 KEY NORMALIZATION | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | The process of normalization is implemented as follows: | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =over 4 | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item 1. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | execute any preprocessing code (see L<example above/SYNOPSIS>), which is | ||||||||||||||||||||||||||||
| 52 | expected to operate on C<$_>. | ||||||||||||||||||||||||||||
| 53 | You can use subroutine references or strings here; strings will be | ||||||||||||||||||||||||||||
| 54 | L<eval()uated|perlfunc/eval>. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item 2. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | split the key into parts (at whitespace). | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item 3. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | remove any parts contained in the list of stopwords | ||||||||||||||||||||||||||||
| 63 | (see L<example above|/SYNOPSIS>). | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item 4. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | replace any parts contained in the list of exceptions | ||||||||||||||||||||||||||||
| 68 | by their corresponding value. | ||||||||||||||||||||||||||||
| 69 | If the value is L<undef|perlfunc/undef>, the entire part will be removed. | ||||||||||||||||||||||||||||
| 70 | (In the L<example above|/SYNOPSIS>, "Jpn" would be replaced by "japan".) | ||||||||||||||||||||||||||||
| 71 | This lookup is done case-insensitively. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item 5. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | remove any non-word characters at the end of each part or followed by a dash | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =back | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | $VERSION = 0.02; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | use constant DATA => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | use constant I => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | use constant PREPROCESS => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | use constant STOPWORDS => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | use constant EXCEPTIONS => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | use constant DEBUG => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=9 SCAR=35.0
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | croak 'Odd number of arguments.' unless @_ & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | my $package = shift; | |||||||||||||||||||||||||||
| 98 | 1 | ▶ | $package = ref $package if length ref $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | my $self = bless [], $package; | |||||||||||||||||||||||||||
| 100 | 1 | $self->[DATA] = []; | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my ( $option, $argument ) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $option eq 'debug' ) { $self->debug($argument) } | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 104 | 0 | elsif ( $option =~ /^exceptions?\z/ ) { $self->exceptions($argument) } | |||||||||||||||||||||||||||
| 105 | 0 | elsif ( $option eq 'preprocess' ) { $self->preprocess($argument) } | |||||||||||||||||||||||||||
| 106 | elsif ( $option =~ /^stopwords?\z/ ) { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $self->stopwords( ref $argument ? @$argument : $argument ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | else { croak qq(Unknown TIEHASH option "$option"!) } | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 1 | $self; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 115 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( defined( my $found = $self->find($key) ) ) { $self->[DATA][$found] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | else { undef } | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 121 | 0 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | defined $self->exact( | ||||||||||||||||||||||||||||
| 124 | $key, my $pos = $self->pos( my $normkey = $self->normalize($key) ) | ||||||||||||||||||||||||||||
| 125 | ) | ||||||||||||||||||||||||||||
| 126 | ) | ||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 0 | $self->[DATA][ $pos + 1 ] = $value; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | else { splice @{ $self->[DATA] }, $pos, 0, $normkey, $value, $key } | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 134 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ( defined $self->find($key) ) { 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | else { '' } | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 140 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | my $pos = $self->pos( my $normkey = $self->normalize($key) ); | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( defined $self->exact( $key, $pos ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ( undef, my $value ) = splice @{ $self->[DATA] }, $pos, 3; | |||||||||||||||||||||||||||
| 144 | 0 | $self->startover; | |||||||||||||||||||||||||||
| 145 | 0 | $value; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | else { undef } | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 151 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 152 | 0 | $self->startover; | |||||||||||||||||||||||||||
| 153 | 0 | @{ $self->[DATA] } = (); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 157 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | return undef unless @{ $self->[DATA] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | $self->[ $self->[I] = 2 ]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 163 | 0 | my ( $self, $lastkey ) = @_; | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ( ( my $i = $self->[I] += 3 ) <= $#{ $self->[DATA] } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $self->[DATA][$i]; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||
| 168 | 0 | $self->startover; | |||||||||||||||||||||||||||
| 169 | 0 | undef; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub UNTIE { } | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | ▶ | sub DESTROY { shift->startover } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 ADDITIONAL METHODS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 debug | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | turn debug mode on (when given a true value as argument) or off | ||||||||||||||||||||||||||||
| 182 | (when given a false value). | ||||||||||||||||||||||||||||
| 183 | Returns the (possibly new) value. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | In debug mode, the L</find> method will print debug messages to STDERR. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | $self->[DEBUG] = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | $self->[DEBUG]; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 delete_abbrev | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | my @deleted = tied(%hash)->delete_abbrev('foo','bar'); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Will delete all elements on the basis of all unambiguous abbreviations given as | ||||||||||||||||||||||||||||
| 200 | arguments and return a (possibly empty) list of all deleted values. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub delete_abbrev { | ||||||||||||||||||||||||||
|
Subroutine
delete_abbrev: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 205 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 206 | 0 | my @deleted; | |||||||||||||||||||||||||||
| 207 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 208 | next | ||||||||||||||||||||||||||||
| 209 | unless | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | defined( my $pos1 = $self->valid( $_, my $pos = $self->pos($_) ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 212 | 0 | push @deleted, grep $i++ & 1, splice @{ $self->[DATA] }, $pos, | |||||||||||||||||||||||||||
| 213 | 3 + $pos1 - $pos; | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | $self->startover if @deleted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | @deleted; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head2 exceptions | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | get or set the exceptions table for the hash. | ||||||||||||||||||||||||||||
| 222 | Expects hash references or L<undef|perlfunc/undef>, which clears the table. | ||||||||||||||||||||||||||||
| 223 | Returns a reference to the new exception table. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =cut | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub exceptions { | ||||||||||||||||||||||||||
|
Subroutine
exceptions: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 229 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | if (defined) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | while ( my ( $k, $v ) = each %$_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $self->[EXCEPTIONS]{ lc $k } = lc $v; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | 0 | else { $self->[EXCEPTIONS] = {} } | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | $self->[EXCEPTIONS] || {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head2 preprocess | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | set up the preprocessing code chain for the hash. | ||||||||||||||||||||||||||||
| 243 | Any code references or strings will be added to the chain, | ||||||||||||||||||||||||||||
| 244 | an L<undef|perlfunc/undef> will clear the chain. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | ▶ | sub preprocess { | ||||||||||||||||||||||||||
|
Subroutine
preprocess: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 250 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | if (defined) { push @{ $self->[PREPROCESS] }, $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | else { @{ $self->[PREPROCESS] } = [] } | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | @{ $self->[PREPROCESS] || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->[2] || []
MC/DC0%0/1
$self->[2] || []
$self->[2]
Truth table0%0/2
A
A= $self->[2]
| |||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head2 stopwords | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | get or set the /stopwords for the hash. | ||||||||||||||||||||||||||||
| 260 | Any arguments given will be added to the list of stopwords. | ||||||||||||||||||||||||||||
| 261 | An L<C<undef>> as argument will clear the list of stopwords. | ||||||||||||||||||||||||||||
| 262 | The method returns the new list of stopwords (in an unsorted manner). | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | sub stopwords { | ||||||||||||||||||||||||||
|
Subroutine
stopwords: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 268 | 0 | for (@_) { | |||||||||||||||||||||||||||
| 269 | 0 | ▶ | if (defined) { $self->[STOPWORDS]{$_} = undef } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | else { $self->[STOPWORDS] = {} } | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 0 | ▶ | keys %{ $self->[STOPWORDS] || {} }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->[3] || {}
MC/DC0%0/1
$self->[3] || {}
$self->[3]
Truth table0%0/2
A
A= $self->[3]
| |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | The following methods should usually not be called "from the outside"; | ||||||||||||||||||||||||||||
| 278 | the main intention of ducumenting them is that the author still wants to | ||||||||||||||||||||||||||||
| 279 | understand his own module in case changes will be neccessary later. :o) | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 exact | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | expects a key as first and a L<position|/pos> as second argument. | ||||||||||||||||||||||||||||
| 284 | Returns the position if the given key equals (case-insensitively) the real key | ||||||||||||||||||||||||||||
| 285 | stored at that position or undef if not. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | ▶ | sub exact { | ||||||||||||||||||||||||||
|
Subroutine
exact: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 290 | 0 | my ( $self, $key, $pos ) = @_; | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | if ( $pos < $#{ $self->[DATA] } && lc $self->[DATA][ $pos + 2 ] eq lc $key ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pos < $#{$$self[0];} and lc $self->[0][$pos + 2] eq lc $key
MC/DC0%0/2
$pos < $#{$$self[0];} and lc $self->[0][$pos + 2] eq lc $key
$pos < $#{$$self[0];}
lc $self->[0][$pos + 2] eq lc $key
Truth table0%0/3
A and B
A= $pos < $#{$$self[0];}B= lc $self->[0][$pos + 2] eq lc $key
| |||||||||||||||||||||||||||||
| 292 | { | ||||||||||||||||||||||||||||
| 293 | 0 | $pos; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 0 | else { undef } | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head2 find | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | This is the central method for lookups, used by L<exists()|perlfunc/exists> and | ||||||||||||||||||||||||||||
| 301 | C<FETCH>. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | It expects a key as its only argument. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Upon success, the method returns an array index at which the corresponding value | ||||||||||||||||||||||||||||
| 306 | can be found, or undef otherwise. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||
| 311 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 312 | 0 | my $debug = $self->debug; | |||||||||||||||||||||||||||
| 313 | 0 | my ( $prefix, $pattern, $normkey ) = $self->normalize($key); | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | print STDERR <<_ if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | -------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 316 | Key: <$key> | ||||||||||||||||||||||||||||
| 317 | Prefix: <$prefix> | ||||||||||||||||||||||||||||
| 318 | Pattern: <$pattern> | ||||||||||||||||||||||||||||
| 319 | NormKey: <$normkey> | ||||||||||||||||||||||||||||
| 320 | _ | ||||||||||||||||||||||||||||
| 321 | 0 | ▶ | defined( my $pos = $self->pos($prefix) ) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 0 | my $data = $self->[DATA]; | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | print STDERR 'Starting search at entry #' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 324 | . ( $pos / 3 ) | ||||||||||||||||||||||||||||
| 325 | . ( | ||||||||||||||||||||||||||||
| 326 | $pos ? qq(; the key before that would be: "$data->[$pos-3]"\n) : ".\n" ) | ||||||||||||||||||||||||||||
| 327 | if $debug; | ||||||||||||||||||||||||||||
| 328 | 0 | my $found; | |||||||||||||||||||||||||||
| 329 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
($pos += 3) < $data->$#* and $prefix eq substr($data->[$pos], 0, length $prefix)
MC/DC0%0/2
($pos += 3) < $data->$#* and $prefix eq substr($data->[$pos], 0, length $prefix)
($pos += 3) < $data->$#*
$prefix eq substr($data->[$pos], 0, length $prefix)
Truth table0%0/3
A and B
A= ($pos += 3) < $data->$#*B= $prefix eq substr($data->[$pos], 0, length $prefix)
| |||||||||||||||||||||||||||||
| 330 | 0 | ▶ | print STDERR 'Examining entry #' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | . ( $pos / 3 ) | ||||||||||||||||||||||||||||
| 332 | . qq(: "$data->[$pos]"... ) | ||||||||||||||||||||||||||||
| 333 | if $debug; | ||||||||||||||||||||||||||||
| 334 | 0 | ▶ | if ( $data->[$pos] =~ $pattern ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 0 | ▶ | if ( lc $data->[ $pos + 2 ] eq lc $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | print STDERR "exact match.\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 0 | return $pos + 1; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | unless ( defined $found ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 340 | 0 | $found = $pos + 1; | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | print STDERR qq( matches, value: "$data->[$found]"\n) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | if $debug; | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | 0 | ▶ | elsif ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $$data[$pos + 1] or $$data[$pos + 1] ne $data->[$found]
MC/DC0%0/2
not defined $$data[$pos + 1] or $$data[$pos + 1] ne $data->[$found]
not defined $$data[$pos + 1]
$$data[$pos + 1] ne $data->[$found]
Truth table0%0/3
not(A) or B
A= not defined $$data[$pos + 1]B= $$data[$pos + 1] ne $data->[$found]
| |||||||||||||||||||||||||||||
| 345 | defined $data->[$found] | ||||||||||||||||||||||||||||
| 346 | ? !defined $data->[ $pos + 1 ] | ||||||||||||||||||||||||||||
| 347 | || $data->[ $pos + 1 ] ne $data->[$found] | ||||||||||||||||||||||||||||
| 348 | : defined $data->[ $pos + 1 ] | ||||||||||||||||||||||||||||
| 349 | ) | ||||||||||||||||||||||||||||
| 350 | { | ||||||||||||||||||||||||||||
| 351 | 0 | ▶ | print STDERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | qq( also matches, but has a different value: "$data->[$pos+1]"\n) | ||||||||||||||||||||||||||||
| 353 | if $debug; | ||||||||||||||||||||||||||||
| 354 | 0 | return; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 0 | ▶ | else { print STDERR "does not match.\n" if $debug } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | } while ( $pos += 3 ) < $#$data | ||||||||||||||||||||||||||||
| 359 | && $prefix eq substr $data->[$pos], 0, length $prefix; | ||||||||||||||||||||||||||||
| 360 | 0 | ▶ | print STDERR $pos > $#$data ? "Last element reached.\n" | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 361 | : qq("$data->[$pos]" has a different prefix.\n), | ||||||||||||||||||||||||||||
| 362 | defined $found ? "Search was successful.\n" | ||||||||||||||||||||||||||||
| 363 | : "Search was NOT successful.\n" | ||||||||||||||||||||||||||||
| 364 | if $debug; | ||||||||||||||||||||||||||||
| 365 | 0 | $found; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head2 normalize | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Given a key as the its only argument, | ||||||||||||||||||||||||||||
| 371 | this method will return the normalized key in scalar | ||||||||||||||||||||||||||||
| 372 | and a three element list in array context, consisting of | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =over 4 | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =item 0. | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | the L</prefix> | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =item 1. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | the L</"search pattern"> and | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =item 2. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | the L</"normalized key">. | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =back | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =cut | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 0 | ▶ | sub normalize { | ||||||||||||||||||||||||||
|
Subroutine
normalize: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 393 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 394 | 0 | my ( $exceptions, $stopwords ) = @{$self}[ EXCEPTIONS, STOPWORDS ]; | |||||||||||||||||||||||||||
| 395 | 0 | local $_ = $key; | |||||||||||||||||||||||||||
| 396 | 0 | for my $pp ( $self->preprocess ) { | |||||||||||||||||||||||||||
| 397 | 0 | ▶ | if ( ref $pp ) { &$pp } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 0 | else { eval $pp } | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | ( | ||||||||||||||||||||||||||||
| 401 | my $normkey = | ||||||||||||||||||||||||||||
| 402 | join ' ', | ||||||||||||||||||||||||||||
| 403 | map exists $exceptions->{ +lc } | ||||||||||||||||||||||||||||
| 404 | ? defined $exceptions->{ +lc } ? $exceptions->{ +lc } : () | ||||||||||||||||||||||||||||
| 405 | : lc, | ||||||||||||||||||||||||||||
| 406 | 0 | ▶ | grep !exists $stopwords->{$_}, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 407 | split /\s+|-/ | ||||||||||||||||||||||||||||
| 408 | ) =~ s/\W+(?=\s|-|$)//g; | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | return $normkey unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | 0 | my ($prefix) = $normkey =~ /^([^\s-]*)/; | |||||||||||||||||||||||||||
| 411 | 0 | my $pattern = '^' | |||||||||||||||||||||||||||
| 412 | . join ( ' ', map quotemeta() . '\S*', split /\s+|-/, $normkey ) . '$'; | ||||||||||||||||||||||||||||
| 413 | 0 | ▶ | $prefix, $] < 5.006 ? $pattern : eval 'qr/$pattern/', $normkey; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head2 pos | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | expects an (usually L<normalized|/"normalized key">) key as (its only) argument | ||||||||||||||||||||||||||||
| 419 | and returns the position at which this key is stored (if it exists) | ||||||||||||||||||||||||||||
| 420 | or should be sorted (if it does not already exist). | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =cut | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 0 | ▶ | sub pos { | ||||||||||||||||||||||||||
|
Subroutine
pos: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 425 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 426 | 0 | my $data = $self->[DATA]; | |||||||||||||||||||||||||||
| 427 | 0 | my $a = 0; | |||||||||||||||||||||||||||
| 428 | 0 | my $b = @$data; | |||||||||||||||||||||||||||
| 429 | 0 | ▶ | while ( $a < $b && $a < $#$data ) { # perform a binary search | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$a < $b and $a < $data->$#*
MC/DC0%0/2
$a < $b and $a < $data->$#*
$a < $b
$a < $data->$#*
Truth table0%0/3
A and B
A= $a < $bB= $a < $data->$#*
| |||||||||||||||||||||||||||||
| 430 | 0 | ▶ | if ( $data->[ my $c = 3 * int +( $a + $b >> 1 ) / 3 ] lt $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | 0 | $a = $c + 3; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | 0 | else { $b = $c } | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | 0 | $a; | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =head2 startover | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | expects no arguments and simply resets the iterator for the hash, | ||||||||||||||||||||||||||||
| 441 | so that the next call to L<each()|perlfunc/each> will return the first key/value | ||||||||||||||||||||||||||||
| 442 | pair again. | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | =cut | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 1 | ▶ | sub startover { | ||||||||||||||||||||||||||
|
Subroutine
startover: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 447 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 448 | 1 | $self->[I] = undef; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head1 BUGS | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | None known so far. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | Martin H. Sluka | ||||||||||||||||||||||||||||
| 458 | mailto:martin@sluka.de | ||||||||||||||||||||||||||||
| 459 | http://martin.sluka.de/ | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =head1 THANKS TO | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | Dr. Hermann Schier from the Max Planck Institute for Solid State Research | ||||||||||||||||||||||||||||
| 464 | in Stuttgart/Germany for initiating and underwriting the development of this | ||||||||||||||||||||||||||||
| 465 | module and for contribution a lot of ideas. | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 470 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 473 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | L<Tie::Hash::Array> | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =cut | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 1 | 1 | |||||||||||||||||||||||||||