Data/Password/zxcvbn/AuthorTools/BuildRankedDictionaries.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 120 CC 6 SCAR 24.2
CC
6
Cov
0%
_filter_frequency_lists
30.0
_parse_frequency_lists
6.0
_is_rare_and_short
2.0
CRAP 11.2
1 package Data::Password::zxcvbn::AuthorTools::BuildRankedDictionaries;
2 use v5.26;
3 use Types::Path::Tiny qw(Dir);
4 use Types::Common::Numeric qw(PositiveOrZeroInt);
5 use Types::Standard qw(HashRef);
6 use Moo;
7 use namespace::clean;
8 our $VERSION = '1.0.2'; # VERSION
9 # ABSTRACT: class to generate C<Data::Password::zxcvbn::*::RankedDictionaries>
10  
11 with 'Data::Password::zxcvbn::AuthorTools::PackageWriter';
12  
13  
14 has dictionaries_word_count => (
15 is => 'ro',
16 required => 1,
17 isa => HashRef[PositiveOrZeroInt],
18 );
19  
20  
21 has data_dir => (
22 is => 'ro',
23 default => 'data/',
24 isa => Dir,
25 coerce => 1,
26 );
27  
28  
29 has '+package_abstract' => (
30 default => 'ranked dictionaries for common words',
31 );
32  
33 sub _parse_frequency_lists {
34 my ($self) = @_;
35  
36 my %freq_lists;
37  
38 for my $file ($self->data_dir->children(qr{\.txt\z})) {
39 my $basename = $file->basename('.txt');
40 unless (exists $self->dictionaries_word_count->{$basename}) {
41 next;
42 }
43  
44 my %token_to_rank;
45 my $fh = $file->openr_utf8;
46 while (my $line = <$fh>) {
47 my ($token) = $line =~ m{^\s*(\S+)};
48 $token_to_rank{$token} //= $.;
49 }
50  
51 $freq_lists{$basename} = \%token_to_rank;
52 }
53  
54 warn "The $_ dictionary was not found, ignoring\n"
55 for grep { !exists $freq_lists{$_} } sort keys %{$self->dictionaries_word_count};
56  
57 return \%freq_lists;
58 }
59  
60 sub _is_rare_and_short {
61 my ($token, $rank) = @_;
62 return $rank >= 10**length($token);
63 }
64  
65 # filters frequency data according to:
66 # - filter out short tokens if they are too rare.
67 # - filter out tokens if they already appear in another dict
68 # at lower rank.
69 # - cut off final freq_list at limits set in
70 # `dictionaries_word_count`, if any.
71 #
72 # The result of this function is a bit different from the JS and
73 # Python ones: those produce a hash $list_name => [ $token, ...]
74 # sorted by ascending rank, and then build a $list_name => { $token =>
75 # $rank, ... } on load; here we produce the latter data structure
76 # directly
77 sub _filter_frequency_lists {
78 my ($self, $freq_lists) = @_;
79 # $freq_lists = { $list_name => { $token => $rank, ... } }
80  
81 # $token => $min_rank_across_all_lists
82 my %minimum_rank;
83 # $token => $list_name_with_lowest_rank_for_this_token
84 my %minimum_name;
85  
86 # find out in which list each token appears nearest the top of the
87 # ranking
88 for my $list_name (sort keys %{$freq_lists}) {
89 my $token_to_rank = $freq_lists->{$list_name};
90 for my $token (keys %{$token_to_rank}) {
91 my $rank = $token_to_rank->{$token};
92 if ( !$minimum_rank{$token} || $minimum_rank{$token} >= $rank ) {
93 $minimum_rank{$token} = $rank;
94 $minimum_name{$token} = $list_name;
95 }
96 #warn "$list_name $token $rank $minimum_rank{$token} $minimum_name{$token}\n";
97 }
98 }
99  
100 # $list_name => [ [ $token, $rank ], ... ]
101 my %filtered_token_and_rank;
102  
103 for my $list_name (sort keys %{$freq_lists}) {
104 my $token_to_rank = $freq_lists->{$list_name};
105 for my $token (keys %{$token_to_rank}) {
106 # only consider a token if this is the list where it
107 # appears nearest the top of the ranking
108 next unless $minimum_name{$token} eq $list_name;
109  
110 my $rank = $token_to_rank->{$token};
111 next if _is_rare_and_short($token,$rank);
112  
113 push @{$filtered_token_and_rank{$list_name}},
114 [ $token, $rank ];
115 }
116 }
117  
118 # $list_name => { $token => $rank } # filtered & compacted rank
119 my %result;
120 for my $list_name (sort keys %filtered_token_and_rank) {
121 my @sorted_tokens = sort { $a->[1] <=> $b->[1] }
122 @{ $filtered_token_and_rank{$list_name} };
123 if (my $cutoff = $self->dictionaries_word_count->{$list_name}) {
124 splice @sorted_tokens,$cutoff;
125 }
126 my $idx = 1;
127 $result{$list_name} = {
128 map { $_->[0] => $idx++ } @sorted_tokens
129 };
130 }
131  
132 return \%result;
133 }
134  
135  
136 sub generate {
137 my ($self) = @_;
138  
139 $self->write_out(
140 $self->_filter_frequency_lists(
141 $self->_parse_frequency_lists()
142 )
143 );
144 }
145  
146  
147 sub hash_variable_name { 'ranked_dictionaries' }
148  
149 1;
150  
151 __END__