| 1 | package Thesaurus; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use vars qw[$VERSION]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | $VERSION = '0.23'; | |||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use Params::Validate qw( validate_with BOOLEAN ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | sub new | ||||||||||
| 12 | 6 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 13 | 6 | my $class = shift; | |||||||||
| 14 | 6 | my %p = validate_with( params => \@_, | |||||||||
| 15 | spec => { ignore_case => { type => BOOLEAN, default => 0 }, | ||||||||||
| 16 | }, | ||||||||||
| 17 | allow_extra => 1, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | my $self = bless { params => { ignore_case => delete $p{ignore_case}, | ||||||||||
| 21 | } | ||||||||||
| 22 | 6 | }, $class; | |||||||||
| 23 | |||||||||||
| 24 | 6 | $self->_init(%p); | |||||||||
| 25 | |||||||||||
| 26 | 6 | return bless $self, $class; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub _init | ||||||||||
| 30 | 2 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | 2 | $self->{data} = {}; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub add | ||||||||||
| 37 | 9 | ▶ | { | ||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 38 | 9 | my $self = shift; | |||||||||
| 39 | |||||||||||
| 40 | 9 | foreach my $list (@_) | |||||||||
| 41 | { | ||||||||||
| 42 | 16 | $self->_add_list($list); | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub _add_list | ||||||||||
| 47 | 16 | ▶ | { | ||||||||
|
Subroutine
_add_list: called CC=2 SCAR=6.9
| |||||||||||
| 48 | 16 | my $self = shift; | |||||||||
| 49 | 16 | my $list = shift; | |||||||||
| 50 | |||||||||||
| 51 | 16 | my %items = $self->_hash_from_list($list); | |||||||||
| 52 | |||||||||||
| 53 | 16 | ▶ | while ( my ($k, $v) = each %items ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | { | ||||||||||
| 55 | 72 | $self->{data}{$k} = $v; | |||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | sub _hash_from_list | ||||||||||
| 60 | 16 | ▶ | { | ||||||||
|
Subroutine
_hash_from_list: called CC=2 SCAR=6.9
| |||||||||||
| 61 | 16 | my $self = shift; | |||||||||
| 62 | 16 | my $list = shift; | |||||||||
| 63 | |||||||||||
| 64 | # if we have any of these already, we need to create new lists for | ||||||||||
| 65 | # them | ||||||||||
| 66 | my @new = | ||||||||||
| 67 | 16 | map { $self->find($_) } $self->_normalize_list($list); | |||||||||
| 68 | |||||||||||
| 69 | # return a unique list | ||||||||||
| 70 | 16 | my @unique = keys %{ { map { $_ => 1 } @new, @$list } }; | |||||||||
| 71 | |||||||||||
| 72 | # a hash of keys to values | ||||||||||
| 73 | 16 | ▶ | return map { ( $self->{params}{ignore_case} ? lc $_ : $_ ) => \@unique } @unique; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub find | ||||||||||
| 77 | 79 | ▶ | { | ||||||||
|
Subroutine
find: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||
| 78 | 79 | my $self = shift; | |||||||||
| 79 | |||||||||||
| 80 | 79 | my $lists = $self->_find(@_); | |||||||||
| 81 | |||||||||||
| 82 | 79 | ▶ | if (@_ > 1) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | { | ||||||||||
| 84 | 0 | ▶ | return unless %$lists; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | return %$lists; | |||||||||
| 86 | } | ||||||||||
| 87 | else | ||||||||||
| 88 | { | ||||||||||
| 89 | 79 | ▶ | return unless exists $lists->{ $_[0] }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 79 | return @{ $lists->{ $_[0] } }; | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | sub _find | ||||||||||
| 95 | 79 | ▶ | { | ||||||||
|
Subroutine
_find: called CC=5 SCAR=16.1
| |||||||||||
| 96 | 79 | my $self = shift; | |||||||||
| 97 | |||||||||||
| 98 | 79 | my %lists; | |||||||||
| 99 | 79 | foreach my $key (@_) | |||||||||
| 100 | { | ||||||||||
| 101 | 79 | ▶ | my $search_key = $self->{params}{ignore_case} ? lc $key : $key; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | # ignore duplicates | ||||||||||
| 104 | 79 | ▶ | next if exists $lists{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | # Anonymize to keep people away from our lists! | ||||||||||
| 107 | $lists{$key} = | ||||||||||
| 108 | 79 | ▶ | exists $self->{data}{$search_key} ? [ @{ $self->{data}{$search_key} } ] : []; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 79 | return \%lists; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | sub delete | ||||||||||
| 115 | 2 | ▶ | { | ||||||||
|
Subroutine
delete: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 116 | 2 | my $self = shift; | |||||||||
| 117 | |||||||||||
| 118 | 2 | foreach my $item ( $self->_normalize_list(\@_) ) | |||||||||
| 119 | { | ||||||||||
| 120 | 2 | ▶ | next unless exists $self->{data}{$item}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | 2 | foreach my $key ( @{ $self->{data}{$item} } ) | |||||||||
| 123 | { | ||||||||||
| 124 | 12 | delete $self->{data}{$key}; | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | sub all | ||||||||||
| 130 | 1 | ▶ | { | ||||||||
|
Subroutine
all: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 131 | 1 | my $self = shift; | |||||||||
| 132 | |||||||||||
| 133 | 1 | my (%done, @data); | |||||||||
| 134 | 1 | foreach my $key ( keys %{ $self->{data} } ) | |||||||||
| 135 | { | ||||||||||
| 136 | 6 | ▶ | next if exists $done{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | |||||||||||
| 138 | 2 | @done{ @{ $self->{data}{$key} } } = (); | |||||||||
| 139 | |||||||||||
| 140 | 2 | push @data, $self->{data}{$key}; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | return @data | ||||||||||
| 144 | 1 | } | |||||||||
| 145 | |||||||||||
| 146 | sub _normalize_list | ||||||||||
| 147 | 18 | ▶ | { | ||||||||
|
Subroutine
_normalize_list: called CC=2 SCAR=6.9
| |||||||||||
| 148 | 18 | my $self = shift; | |||||||||
| 149 | 18 | my $list = shift; | |||||||||
| 150 | |||||||||||
| 151 | 18 | ▶ | return map { lc } @$list if $self->{params}{ignore_case}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 10 | return @$list; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 3 | 1; | |||||||||
| 156 | |||||||||||
| 157 | __END__ | ||||||||||