File Coverage

blib/lib/Data/Freq/Node.pm
Criterion Covered Total %
statement 40 41 97.5
branch 7 8 87.5
condition 3 6 50.0
subroutine 16 17 94.1
pod 13 13 100.0
total 79 85 92.9


line stmt bran cond sub pod time code
1 3     3   50 use 5.006;
  3         17  
2 3     3   12 use strict;
  3         6  
  3         50  
3 3     3   12 use warnings;
  3         4  
  3         117  
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             =head1 VERSION
12              
13             Version 0.04
14              
15             =cut
16              
17             our $VERSION = '0.04';
18              
19 3     3   15 use List::Util;
  3         6  
  3         1392  
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             Usage:
26              
27             my $root_node = Data::Freq::Node->new();
28              
29             Constructs a node object in the L.
30              
31             =cut
32              
33             sub new {
34 81     81 1 154 my ($class, $value, $parent) = @_;
35            
36 81 100       142 if (ref $class) {
37 68   33     102 $parent ||= $class;
38 68         80 $class = ref $class;
39             }
40            
41 81         91 my $depth = 0;
42            
43 81 100       125 if ($parent) {
44 68         99 $depth = $parent->depth + 1;
45 68         86 $parent->{unique}++;
46             }
47            
48 81         511 return bless {
49             # For this node's own
50             count => 0,
51             value => $value,
52            
53             # Parent & children
54             parent => $parent,
55             children => {},
56             first => undef,
57             last => undef,
58             unique => 0,
59            
60             # Depth from root
61             depth => $depth,
62             }, $class;
63             }
64              
65             =head2 add_subnode
66              
67             Usage:
68              
69             my $child_node = $parent_node->add_subnode('normalized text');
70              
71             Adds a normalized value and returns the corresponding subnode.
72              
73             If the normalized text appears for the first time under the parent node,
74             a new node is created. Otherwise, the existing node is returned with its count
75             incremented by 1.
76              
77             =cut
78              
79             sub add_subnode {
80 233     233 1 312 my ($self, $value) = @_;
81 233   66     315 my $child = ($self->children->{$value} ||= $self->new($value, $self));
82            
83 233 100       349 $child->{first} = $self->count if $child->count == 0;
84 233         318 $child->{last} = $self->count;
85            
86 233         269 $child->{count}++;
87            
88 233         551 return $child;
89             }
90              
91             =head2 count
92              
93             Retrieves the count for the normalized text.
94              
95             =head2 value
96              
97             Retrieves the normalized value.
98              
99             =head2 parent
100              
101             Retrieves the parent node in the L.
102              
103             For the root node, C is returned.
104              
105             =head2 children
106              
107             Retrieves a hash ref of the raw counting results under this node,
108             where the key is the normalized text and the value is the corresponding subnode.
109              
110             =head2 unique
111              
112             Retrieves the number of the child nodes.
113              
114             =head2 max
115              
116             Calculates the maximum count of the child nodes.
117              
118             =head2 min
119              
120             Calculates the minimum count of the child nodes.
121              
122             =head2 average
123              
124             Calculates the average count of the child nodes.
125              
126             =head2 first
127              
128             Retrieves the first occurrence index of this node under its parent node.
129              
130             The index is the count of the parent node at the time this child node is created.
131              
132             =head2 last
133              
134             Retrieves the last occurrence index of this node under its parent node.
135              
136             The index is the count of the parent node at the last time this child node is added or created.
137              
138             =head2 depth
139              
140             Retrieves the depth in the L.
141              
142             The depth of the root node is 0.
143              
144             =cut
145              
146 604     604 1 967 sub count {$_[0]->{count }}
147 140     140 1 272 sub value {$_[0]->{value }}
148 0     0 1 0 sub parent {$_[0]->{parent }}
149 260     260 1 546 sub children {$_[0]->{children}}
150 18     18 1 33 sub unique {$_[0]->{unique }}
151 6     6 1 7 sub max {List::Util::max(map {$_->count} values %{$_[0]->children})}
  12         14  
  6         9  
152 6     6 1 8 sub min {List::Util::min(map {$_->count} values %{$_[0]->children})}
  12         13  
  6         9  
153 6 50   6 1 12 sub average {$_[0]->unique > 0 ? ($_[0]->count / $_[0]->unique) : undef};
154 174     174 1 442 sub first {$_[0]->{first }}
155 6     6 1 11 sub last {$_[0]->{last }}
156 140     140 1 328 sub depth {$_[0]->{depth }}
157              
158             =head1 AUTHOR
159              
160             Mahiro Ando, C<< >>
161              
162             =head1 LICENSE AND COPYRIGHT
163              
164             Copyright 2012 Mahiro Ando.
165              
166             This program is free software; you can redistribute it and/or modify it
167             under the terms of either: the GNU General Public License as published
168             by the Free Software Foundation; or the Artistic License.
169              
170             See http://dev.perl.org/licenses/ for more information.
171              
172             =cut
173              
174             1;