Freq/Node.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 87.5% 7 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod 100% 13 / 13 totaltotal 88.8% 80 / 90 CC 7 SCAR 6.6
CC
7
Cov
91%
new
4.0
add_subnode
3.0
average
2.1
CRAP 1.9
« Freq/Record.pm ↑ Summary
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Freq::Node;
6  
7 =head1 NAME
8  
9 Data::Freq::Node - Represents a node of the result tree constructed by Data::Freq
10  
11 =cut
12  
13 3 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 METHODS
16  
17 =head2 new
18  
19 Usage:
20  
21 my $root_node = Data::Freq::Node->new();
22  
23 Constructs a node object in the L<Data::Freq/frequency tree>.
24  
25 =cut
26  
27 81 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
28 81 my ($class, $value, $parent) = @_;
29
30 81 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 68 13 81
31 68 ▶ $parent ||= $class;
Condition33%1/3
$parent ||= $class
l!l&&r!l&&!r
6800
MC/DC0%0/2
$parent ||= $class
$parent $class
Truth table33%1/3
A ||= B
A= $parentB= $class
ABresult
1X1
011
000
32 68 $class = ref $class;
33 }
34
35 81 my $depth = 0;
36
37 81 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 68 13 81
38 68 $depth = $parent->depth + 1;
39 68 $parent->{unique}++;
40 }
41
42 81 return bless {
43 # For this node's own
44 count => 0,
45 value => $value,
46
47 # Parent & children
48 parent => $parent,
49 children => {},
50 first => undef,
51 last => undef,
52 unique => 0,
53
54 # Depth from root
55 depth => $depth,
56 }, $class;
57 }
58  
59 =head2 add_subnode
60  
61 Usage:
62  
63 my $child_node = $parent_node->add_subnode('normalized text');
64  
65 Adds a normalized value and returns the corresponding subnode.
66  
67 If the normalized text appears for the first time under the parent node,
68 a new node is created. Otherwise, the existing node is returned with its count
69 incremented by 1.
70  
71 =cut
72  
73 233 ▶sub add_subnode {
Subroutine add_subnode: called CC=3 SCAR=11.0
Pod documented
74 233 my ($self, $value) = @_;
75 233 ▶ my $child = ($self->children->{$value} ||= $self->new($value, $self));
Condition66%2/3
$self->children->{$value} ||= $self->new($value, $self)
l!l&&r!l&&!r
165680
MC/DC0%0/2
$self->children->{$value} ||= $self->new($value, $self)
$self->children->{$value} $self->new($value, $self)
Truth table66%2/3
A ||= B
A= $self->children->{$value}B= $self->new($value, $self)
ABresult
1X1
011
000
76
77 233 ▶ $child->{first} = $self->count if $child->count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $child->count == 0 68 165 233
78 233 $child->{last} = $self->count;
79
80 233 $child->{count}++;
81
82 233 return $child;
83 }
84  
85 =head2 count
86  
87 Retrieves the count for the normalized text.
88  
89 =head2 value
90  
91 Retrieves the normalized value.
92  
93 =head2 parent
94  
95 Retrieves the parent node in the L<Data::Freq/frequency tree>.
96  
97 For the root node, C<undef> is returned.
98  
99 =head2 children
100  
101 Retrieves a hash ref of the raw counting results under this node,
102 where the key is the normalized text and the value is the corresponding subnode.
103  
104 =head2 unique
105  
106 Retrieves the number of the child nodes.
107  
108 =head2 max
109  
110 Calculates the maximum count of the child nodes.
111  
112 =head2 min
113  
114 Calculates the minimum count of the child nodes.
115  
116 =head2 average
117  
118 Calculates the average count of the child nodes.
119  
120 =head2 first
121  
122 Retrieves the first occurrence index of this node under its parent node.
123  
124 The index is the count of the parent node at the time this child node is created.
125  
126 =head2 last
127  
128 Retrieves the last occurrence index of this node under its parent node.
129  
130 The index is the count of the parent node at the last time this child node is added or created.
131  
132 =head2 depth
133  
134 Retrieves the depth in the L<Data::Freq/frequency tree>.
135  
136 The depth of the root node is 0.
137  
138 =cut
139  
140 604 ▶sub count {$_[0]->{count }}
Subroutine count: called CC=1 SCAR=0.0
Pod documented
141 140 ▶sub value {$_[0]->{value }}
Subroutine value: called CC=1 SCAR=0.0
Pod documented
142 0 ▶sub parent {$_[0]->{parent }}
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
143 260 ▶sub children {$_[0]->{children}}
Subroutine children: called CC=1 SCAR=0.0
Pod documented
144 18 ▶sub unique {$_[0]->{unique }}
Subroutine unique: called CC=1 SCAR=0.0
Pod documented
145 6 ▶sub max {List::Util::max(map {$_->count} values %{$_[0]->children})}
Subroutine max: called CC=1 SCAR=0.0
Pod documented
146 6 ▶sub min {List::Util::min(map {$_->count} values %{$_[0]->children})}
Subroutine min: called CC=1 SCAR=0.0
Pod documented
147 6 ▶sub average {$_[0]->unique > 0 ? ($_[0]->count / $_[0]->unique) : undef};
Subroutine average: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->unique > 0 ? : 6 0 6
148 174 ▶sub first {$_[0]->{first }}
Subroutine first: called CC=1 SCAR=0.0
Pod documented
149 6 ▶sub last {$_[0]->{last }}
Subroutine last: called CC=1 SCAR=0.0
Pod documented
150 140 ▶sub depth {$_[0]->{depth }}
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
151  
152 =head1 AUTHOR
153  
154 Mahiro Ando, C<< <mahiro at cpan.org> >>
155  
156 =head1 LICENSE AND COPYRIGHT
157  
158 Copyright 2012 Mahiro Ando.
159  
160 This program is free software; you can redistribute it and/or modify it
161 under the terms of either: the GNU General Public License as published
162 by the Free Software Foundation; or the Artistic License.
163  
164 See http://dev.perl.org/licenses/ for more information.
165  
166 =cut
167  
168 3 1;
« Freq/Record.pm ↑ Summary