| 1 | package Text::Affixes; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 4 | require Exporter; | |||||||||||
| 8 | |||||||||||||
| 9 | 4 | our @ISA = qw(Exporter); | |||||||||||
| 10 | |||||||||||||
| 11 | 4 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||
| 12 | |||||||||||||
| 13 | ) ] ); | ||||||||||||
| 14 | |||||||||||||
| 15 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||
| 16 | |||||||||||||
| 17 | 4 | our @EXPORT = qw( | |||||||||||
| 18 | get_prefixes | ||||||||||||
| 19 | get_suffixes | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | 4 | our $VERSION = '0.09'; | |||||||||||
| 23 | |||||||||||||
| 24 | =head1 NAME | ||||||||||||
| 25 | |||||||||||||
| 26 | Text::Affixes - Prefixes and suffixes analysis of text | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||
| 29 | |||||||||||||
| 30 | use Text::Affixes; | ||||||||||||
| 31 | my $text = "Hello, world. Hello, big world."; | ||||||||||||
| 32 | my $prefixes = get_prefixes($text); | ||||||||||||
| 33 | |||||||||||||
| 34 | # $prefixes now holds | ||||||||||||
| 35 | # { | ||||||||||||
| 36 | # 3 => { | ||||||||||||
| 37 | # 'Hel' => 2, | ||||||||||||
| 38 | # 'wor' => 2, | ||||||||||||
| 39 | # } | ||||||||||||
| 40 | # } | ||||||||||||
| 41 | |||||||||||||
| 42 | # or | ||||||||||||
| 43 | |||||||||||||
| 44 | $prefixes = get_prefixes({min => 1, max => 2},$text); | ||||||||||||
| 45 | |||||||||||||
| 46 | # $prefixes now holds | ||||||||||||
| 47 | # { | ||||||||||||
| 48 | # 1 => { | ||||||||||||
| 49 | # 'H' => 2, | ||||||||||||
| 50 | # 'w' => 2, | ||||||||||||
| 51 | # 'b' => 1, | ||||||||||||
| 52 | # }, | ||||||||||||
| 53 | # 2 => { | ||||||||||||
| 54 | # 'He' => 2, | ||||||||||||
| 55 | # 'wo' => 2, | ||||||||||||
| 56 | # 'bi' => 1, | ||||||||||||
| 57 | # } | ||||||||||||
| 58 | # } | ||||||||||||
| 59 | |||||||||||||
| 60 | # the use for get_suffixes is similar | ||||||||||||
| 61 | |||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||
| 63 | |||||||||||||
| 64 | Provides methods for prefix and suffix analysis of text. | ||||||||||||
| 65 | |||||||||||||
| 66 | =head1 METHODS | ||||||||||||
| 67 | |||||||||||||
| 68 | =head2 get_prefixes | ||||||||||||
| 69 | |||||||||||||
| 70 | Extracts prefixes from text. You can specify the minimum and maximum | ||||||||||||
| 71 | number of characters of prefixes you want. | ||||||||||||
| 72 | |||||||||||||
| 73 | Returns a reference to a hash, where the specified limits are mapped | ||||||||||||
| 74 | in hashes; each of those hashes maps every prefix in the text into the | ||||||||||||
| 75 | number of times it was found. | ||||||||||||
| 76 | |||||||||||||
| 77 | By default, both minimum and maximum limits are 3. If the minimum | ||||||||||||
| 78 | limit is greater than the lower one, an empty hash is returned. | ||||||||||||
| 79 | |||||||||||||
| 80 | A prefix is considered to be a sequence of word characters (\w) in | ||||||||||||
| 81 | the beginning of a word (that is, after a word boundary) that does not | ||||||||||||
| 82 | reach the end of the word ("regular expressionly", a prefix is the $1 | ||||||||||||
| 83 | of /\b(\w+)\w/). | ||||||||||||
| 84 | |||||||||||||
| 85 | # extracting prefixes of size 3 | ||||||||||||
| 86 | $prefixes = get_prefixes( $text ); | ||||||||||||
| 87 | |||||||||||||
| 88 | # extracting prefixes of sizes 2 and 3 | ||||||||||||
| 89 | $prefixes = get_prefixes( {min => 2}, $text ); | ||||||||||||
| 90 | |||||||||||||
| 91 | # extracting prefixes of sizes 3 and 4 | ||||||||||||
| 92 | $prefixes = get_prefixes( {max => 4}, $text ); | ||||||||||||
| 93 | |||||||||||||
| 94 | # extracting prefixes of sizes 2, 3 and 4 | ||||||||||||
| 95 | $prefixes = get_prefixes( {min => 2, max=> 4}, $text); | ||||||||||||
| 96 | |||||||||||||
| 97 | =cut | ||||||||||||
| 98 | |||||||||||||
| 99 | 11 | ▶ | sub get_prefixes { | ||||||||||
|
Subroutine
get_prefixes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 100 | 11 | return _get_elements(1,@_); | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | =head2 get_suffixes | ||||||||||||
| 104 | |||||||||||||
| 105 | The get_suffixes function is similar to the get_prefixes one. You | ||||||||||||
| 106 | should read the documentation for that one and than come back to this | ||||||||||||
| 107 | point. | ||||||||||||
| 108 | |||||||||||||
| 109 | A suffix is considered to be a sequence of word characters (\w) in | ||||||||||||
| 110 | the end of a word (that is, before a word boundary) that does not start | ||||||||||||
| 111 | at the beginning of the word ("regular expressionly" speaking, a | ||||||||||||
| 112 | suffix is the $1 of /\w(\w+)\b/). | ||||||||||||
| 113 | |||||||||||||
| 114 | # extracting suffixes of size 3 | ||||||||||||
| 115 | $suffixes = get_suffixes( $text ); | ||||||||||||
| 116 | |||||||||||||
| 117 | # extracting suffixes of sizes 2 and 3 | ||||||||||||
| 118 | $suffixes = get_suffixes( {min => 2}, $text ); | ||||||||||||
| 119 | |||||||||||||
| 120 | # extracting suffixes of sizes 3 and 4 | ||||||||||||
| 121 | $suffixes = get_suffixes( {max => 4}, $text ); | ||||||||||||
| 122 | |||||||||||||
| 123 | # extracting suffixes of sizes 2, 3 and 4 | ||||||||||||
| 124 | $suffixes = get_suffixes( {min => 2, max=> 4}, $text); | ||||||||||||
| 125 | |||||||||||||
| 126 | =cut | ||||||||||||
| 127 | |||||||||||||
| 128 | 7 | ▶ | sub get_suffixes { | ||||||||||
|
Subroutine
get_suffixes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 129 | 7 | return _get_elements(0,@_); | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 18 | ▶ | sub _get_elements { | ||||||||||
|
Subroutine
_get_elements: called CC=16 SCAR=27.7
| |||||||||||||
| 133 | 18 | my $task = shift; | |||||||||||
| 134 | |||||||||||||
| 135 | =head1 OPTIONS | ||||||||||||
| 136 | |||||||||||||
| 137 | Apart from deciding on a minimum and maximum size for prefixes or suffixes, you | ||||||||||||
| 138 | can also decide on some configuration options. | ||||||||||||
| 139 | |||||||||||||
| 140 | =cut | ||||||||||||
| 141 | |||||||||||||
| 142 | # configuration | ||||||||||||
| 143 | 18 | my %conf = ( min => 3, | |||||||||||
| 144 | max => 3, | ||||||||||||
| 145 | exclude_numbers => 1, | ||||||||||||
| 146 | lowercase => 0, | ||||||||||||
| 147 | ); | ||||||||||||
| 148 | 18 | ▶ | if (ref $_[0] eq 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 15 | %conf = (%conf, %{+shift}); | |||||||||||
| 150 | } | ||||||||||||
| 151 | 18 | ▶ | return {} if $conf{max} < $conf{min}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | |||||||||||||
| 153 | # get the elements | ||||||||||||
| 154 | 17 | my %elements; | |||||||||||
| 155 | 17 | ▶ | my $text = shift || return undef; | ||||||||||
|
Condition100%2/2
shift() || (return undef)
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 156 | 16 | ▶ | $conf{min} = 1 if $conf{min} < 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 16 | for ($conf{min} .. $conf{max}) { | |||||||||||
| 158 | |||||||||||||
| 159 | 17 | ▶ | my $regex = $task ? qr/\b(\w{$_})\w/ : # prefixes | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | qr/\w(\w{$_})\b/ ; # suffixes | ||||||||||||
| 161 | |||||||||||||
| 162 | 17 | ▶ | while ($text =~ /$regex/g) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 52 | $elements{$_}{$1}++; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | =head2 exclude_numbers | ||||||||||||
| 169 | |||||||||||||
| 170 | Set to 0 if you consider numbers as part of words. Default value is 1. | ||||||||||||
| 171 | |||||||||||||
| 172 | # this | ||||||||||||
| 173 | get_suffixes( {min => 1, max => 1, exclude_numbers => 0}, "Hello, but w8" ); | ||||||||||||
| 174 | |||||||||||||
| 175 | # returns this: | ||||||||||||
| 176 | { | ||||||||||||
| 177 | 1 => { | ||||||||||||
| 178 | 'o' => 1, | ||||||||||||
| 179 | 't' => 1, | ||||||||||||
| 180 | '8' => 1 | ||||||||||||
| 181 | } | ||||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | =cut | ||||||||||||
| 185 | |||||||||||||
| 186 | # exclude elements containing numbers | ||||||||||||
| 187 | 16 | ▶ | if ($conf{exclude_numbers}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 14 | for my $s (keys %elements) { | |||||||||||
| 189 | 15 | for (keys %{$elements{$s}}) { | |||||||||||
| 190 | 34 | ▶ | delete ${$elements{$s}}{$_} if /\d/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | } | ||||||||||||
| 192 | } | ||||||||||||
| 193 | } | ||||||||||||
| 194 | |||||||||||||
| 195 | =head2 lowercase | ||||||||||||
| 196 | |||||||||||||
| 197 | Set to 1 to extract all prefixes in lowercase mode. Default value is 0. | ||||||||||||
| 198 | |||||||||||||
| 199 | ATTENTION: This does not mean that prefixes with uppercased characters won't be | ||||||||||||
| 200 | extracted. It means they will be extracted after being lowercased. | ||||||||||||
| 201 | |||||||||||||
| 202 | # this... | ||||||||||||
| 203 | get_prefixes( {min => 2, max => 2, lowercase => 1}, "Hello, hello"); | ||||||||||||
| 204 | |||||||||||||
| 205 | # returns this: | ||||||||||||
| 206 | { | ||||||||||||
| 207 | 2 => { | ||||||||||||
| 208 | 'he' => 2 | ||||||||||||
| 209 | } | ||||||||||||
| 210 | } | ||||||||||||
| 211 | |||||||||||||
| 212 | =cut | ||||||||||||
| 213 | |||||||||||||
| 214 | # elements containing uppercased characters become lowercased ones | ||||||||||||
| 215 | 16 | ▶ | if ($conf{lowercase}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 216 | 4 | for my $s (keys %elements) { | |||||||||||
| 217 | 4 | for (keys %{$elements{$s}}) { | |||||||||||
| 218 | 6 | ▶ | if (/[[:upper:]]/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 219 | 4 | ${$elements{$s}}{lc $_} += | |||||||||||
| 220 | 4 | delete ${$elements{$s}}{$_}; | |||||||||||
| 221 | } | ||||||||||||
| 222 | } | ||||||||||||
| 223 | } | ||||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | 16 | return \%elements; | |||||||||||
| 227 | } | ||||||||||||
| 228 | |||||||||||||
| 229 | 4 | 1; | |||||||||||
| 230 | __END__ | ||||||||||||