| 1 | package Pod::Wordlist; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '1.27'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Lingua::EN::Inflect 'PL'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | _MAXWORDLENGTH => 50, | |||||||||||||||||||||||||||
| 12 | 7 | }; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Class::Tiny { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | wordlist => \&_copy_wordlist, | |||||||||||||||||||||||||||
| 16 | _is_debug => 0, | ||||||||||||||||||||||||||||
| 17 | no_wide_chars => 0, | ||||||||||||||||||||||||||||
| 18 | 7 | }; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 7 | our %Wordlist; ## no critic ( Variables::ProhibitPackageVars ) | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 18 | ▶ | sub _copy_wordlist { | ||||||||||||||||||||||||||
|
Subroutine
_copy_wordlist: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 18 | my %copy; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # %Wordlist can be accessed externally, and users will often add terms in | ||||||||||||||||||||||||||||
| 26 | # encoded form | ||||||||||||||||||||||||||||
| 27 | 18 | for my $word ( keys %Wordlist ) { | |||||||||||||||||||||||||||
| 28 | 43758 | my $decoded_word = $word; | |||||||||||||||||||||||||||
| 29 | # if it was already decoded, this should do nothing | ||||||||||||||||||||||||||||
| 30 | 43758 | utf8::decode($decoded_word); | |||||||||||||||||||||||||||
| 31 | 43758 | $copy{$decoded_word} = 1; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 18 | return \%copy; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 38 | 7 | my $file; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # try to find wordlist in non-installed dist | ||||||||||||||||||||||||||||
| 41 | 7 | my ($d, $p) = File::Spec->splitpath(__FILE__); | |||||||||||||||||||||||||||
| 42 | 7 | $p = File::Spec->catdir($p, (File::Spec->updir) x 2, 'share'); | |||||||||||||||||||||||||||
| 43 | 7 | my $full_path = File::Spec->catpath($d, $p, 'wordlist'); | |||||||||||||||||||||||||||
| 44 | 7 | ▶ | if ($full_path && -e $full_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$full_path and -e $full_path
MC/DC0%0/2
$full_path and -e $full_path
$full_path
-e $full_path
Truth table33%1/3
A and B
A= $full_pathB= -e $full_path
| |||||||||||||||||||||||||||||
| 45 | 0 | $file = $full_path; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | if ( not defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 7 | require File::ShareDir; | |||||||||||||||||||||||||||
| 50 | 7 | $file = File::ShareDir::dist_file('Pod-Spell', 'wordlist'); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | open my $fh, '<:encoding(UTF-8)', $file | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | or die "Cannot read $file: $!"; ## no critic (ErrorHandling::RequireCarping) | ||||||||||||||||||||||||||||
| 55 | 7 | ▶ | while ( defined( my $line = readline $fh ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 8540 | chomp $line; | |||||||||||||||||||||||||||
| 57 | 8540 | $Wordlist{$line} = 1; | |||||||||||||||||||||||||||
| 58 | 8540 | $Wordlist{PL($line)} = 1; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 7 | close $fh; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 21 | ▶ | sub learn_stopwords { | ||||||||||||||||||||||||||
|
Subroutine
learn_stopwords: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 21 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 65 | 21 | my $stopwords = $self->wordlist; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 21 | ▶ | while ( $text =~ m<(\S+)>g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 45 | my $word = $1; | |||||||||||||||||||||||||||
| 69 | 45 | utf8::decode($word); | |||||||||||||||||||||||||||
| 70 | 45 | ▶ | if ( $word =~ m/^!(.+)/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | # "!word" deletes from the stopword list | ||||||||||||||||||||||||||||
| 72 | 2 | my $negation = $1; | |||||||||||||||||||||||||||
| 73 | # different $1 from above | ||||||||||||||||||||||||||||
| 74 | 2 | delete $stopwords->{$negation}; | |||||||||||||||||||||||||||
| 75 | 2 | delete $stopwords->{PL($negation)}; | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | print "Unlearning stopword <$negation>\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 43 | $word =~ s{'s$}{}; # we strip 's when checking so strip here, too | |||||||||||||||||||||||||||
| 80 | 43 | $stopwords->{$word} = 1; | |||||||||||||||||||||||||||
| 81 | 43 | $stopwords->{PL($word)} = 1; | |||||||||||||||||||||||||||
| 82 | 43 | ▶ | print "Learning stopword <$word>\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 21 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 161 | ▶ | sub is_stopword { | ||||||||||||||||||||||||||
|
Subroutine
is_stopword: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 161 | my ($self, $word) = @_; | |||||||||||||||||||||||||||
| 90 | 161 | my $stopwords = $self->wordlist; | |||||||||||||||||||||||||||
| 91 | 161 | ▶ | if ( exists $stopwords->{$word} or exists $stopwords->{ lc $word } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $stopwords->{$word} or exists $$stopwords{lc $word}
MC/DC100%2/2
exists $stopwords->{$word} or exists $$stopwords{lc $word}
exists $stopwords->{$word}
exists $$stopwords{lc $word}
Truth table100%3/3
A or B
A= exists $stopwords->{$word}B= exists $$stopwords{lc $word}
| |||||||||||||||||||||||||||||
| 92 | 71 | ▶ | print " Rejecting <$word>\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 71 | return 1; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 90 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 38 | ▶ | sub strip_stopwords { | ||||||||||||||||||||||||||
|
Subroutine
strip_stopwords: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 38 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Count the things in $text | ||||||||||||||||||||||||||||
| 102 | 38 | ▶ | print "Content: <", $text, ">\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 38 | my @words = grep { length($_) < _MAXWORDLENGTH } split " ", $text; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 38 | for ( @words ) { | |||||||||||||||||||||||||||
| 107 | 211 | ▶ | print "Parsing word: <$_>\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # some spellcheckers can't cope with anything but Latin1 | ||||||||||||||||||||||||||||
| 109 | 211 | ▶ | $_ = '' if $self->no_wide_chars && /[^\x00-\xFF]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->no_wide_chars and /[^\x00-\xFF]/
MC/DC100%2/2
$self->no_wide_chars and /[^\x00-\xFF]/
$self->no_wide_chars
/[^\x00-\xFF]/
Truth table100%3/3
A and B
A= $self->no_wide_charsB= /[^\x00-\xFF]/
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # strip leading punctuation | ||||||||||||||||||||||||||||
| 112 | 211 | s/^[\(\[\{\'\"\:\;\,\?\!\.]+//; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # keep everything up to trailing punctuation, not counting | ||||||||||||||||||||||||||||
| 115 | # periods (for abbreviations like "Ph.D."), single-quotes | ||||||||||||||||||||||||||||
| 116 | # (for contractions like "don't") or colons (for package | ||||||||||||||||||||||||||||
| 117 | # names like "Foo::Bar") | ||||||||||||||||||||||||||||
| 118 | 211 | s/^([^\)\]\}\"\;\,\?\!]+).*$/$1/; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # strip trailing single-quote, periods or colons; after this | ||||||||||||||||||||||||||||
| 121 | # we have a word that could have internal periods or quotes | ||||||||||||||||||||||||||||
| 122 | 211 | s/[\.\'\:]+$//; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # strip possessive | ||||||||||||||||||||||||||||
| 125 | 211 | s/'s$//i; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # zero out variable names or things with internal symbols, | ||||||||||||||||||||||||||||
| 128 | # since those are probably code expressions outside a C<> | ||||||||||||||||||||||||||||
| 129 | 211 | my $is_sigil = /^[\&\%\$\@\:\<\*\\\_]/; | |||||||||||||||||||||||||||
| 130 | 211 | my $is_strange = /[\%\^\&\#\$\@\_\<\>\(\)\[\]\{\}\\\*\:\+\/\=\|\`\~]/; | |||||||||||||||||||||||||||
| 131 | 211 | ▶ | $_ = '' if $is_sigil || $is_strange; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_sigil or $is_strange
MC/DC100%2/2
$is_sigil or $is_strange
$is_sigil
$is_strange
Truth table100%3/3
A or B
A= $is_sigilB= $is_strange
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # stop if there are no "word" characters left; if it's just | ||||||||||||||||||||||||||||
| 134 | # punctuation that we didn't happen to strip or it's weird glyphs, | ||||||||||||||||||||||||||||
| 135 | # the spellchecker won't do any good anyway | ||||||||||||||||||||||||||||
| 136 | 211 | ▶ | next unless /\w/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 156 | ▶ | print " Checking as <$_>\n" if $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # replace it with any stopword or stopword parts stripped | ||||||||||||||||||||||||||||
| 141 | 156 | $_ = $self->_strip_a_word($_); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 156 | ▶ | print " Keeping as <$_>\n" if $_ && $self->_is_debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_ and $self->_is_debug
MC/DC100%2/2
$_ and $self->_is_debug
$_
$self->_is_debug
Truth table100%3/3
A and B
A= $_B= $self->_is_debug
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 38 | ▶ | return join(" ", grep { defined && length } @words ); | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && length $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 156 | ▶ | sub _strip_a_word { | ||||||||||||||||||||||||||
|
Subroutine
_strip_a_word: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 150 | 156 | my ($self, $word) = @_; | |||||||||||||||||||||||||||
| 151 | 156 | my $remainder; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # try word as-is, including possible hyphenation vs stoplist | ||||||||||||||||||||||||||||
| 154 | 156 | ▶ | if ($self->is_stopword($word) ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 155 | 69 | $remainder = ''; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | # internal period could be abbreviations, so check with | ||||||||||||||||||||||||||||
| 158 | # trailing period restored and drop or keep on that basis | ||||||||||||||||||||||||||||
| 159 | elsif ( index($word, '.') >= 0 ) { | ||||||||||||||||||||||||||||
| 160 | 2 | my $abbr = "$word."; | |||||||||||||||||||||||||||
| 161 | 2 | ▶ | $remainder = $self->is_stopword($abbr) ? '' : $abbr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | # check individual parts of hyphenated word, keep whatever isn't a | ||||||||||||||||||||||||||||
| 164 | # stopword as individual words | ||||||||||||||||||||||||||||
| 165 | elsif ( index($word, '-') >= 0 ) { | ||||||||||||||||||||||||||||
| 166 | 1 | my @keep; | |||||||||||||||||||||||||||
| 167 | 1 | for my $part ( split /-/, $word ) { | |||||||||||||||||||||||||||
| 168 | 3 | ▶ | push @keep, $part if ! $self->is_stopword( $part ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | $remainder = join(" ", @keep) if @keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | # otherwise, we just keep it | ||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||
| 174 | 84 | $remainder = $word; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 156 | return $remainder; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 7 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||