blib/lib/Lingua/Ogmios/LearningDataSet/Data.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 220 SCAR 68.4
CC
30
Cov
0%
getSVM
72.0
getARFF
56.0
new
20.0
CRAP 930.0
1 package Lingua::Ogmios::LearningDataSet::Data;
2  
3  
4 use strict;
5 use warnings;
6  
7 our $VERSION='0.1';
8  
9 sub new {
10  
11 my $class = shift;
12 my $fields = shift;
13  
14  
15 my $data = {
16 'values' => $fields->{values},
17 'type' => $fields->{type},
18 'weight' => $fields->{weight},
19 'countVal' => 0,
20 'class' => $fields->{class},
21 'comment' => $fields->{comment},
22 'qid' => $fields->{qid},
23 };
24  
25 bless($data, $class);
26  
27 if (!defined $data->type) {
28 $data->type('normal');
29 }
30  
31 if (defined $data->values) {
32 $data->countVal(scalar(@{$data->values}));
33 } else {
34 $data->{values} = [];
35 }
36  
37 if (defined $fields->{"countVal"}) {
38 $data->countVal($fields->{"countVal"});
39 }
40  
41 return($data);
42 }
43  
44 sub weight {
45 my $self = shift;
46  
47 if (@_) {
48 $self->{'weight'} = shift;
49 }
50 return($self->{'weight'});
51 }
52  
53 sub comment {
54 my $self = shift;
55  
56 if (@_) {
57 $self->{'comment'} = shift;
58 }
59 return($self->{'comment'});
60 }
61  
62 sub qid {
63 my $self = shift;
64  
65 if (@_) {
66 $self->{'qid'} = shift;
67 }
68 return($self->{'qid'});
69 }
70  
71 sub class {
72 my $self = shift;
73  
74 if (@_) {
75 $self->{'class'} = shift;
76 }
77 return($self->{'class'});
78 }
79  
80 sub isNormal {
81 my ($self) = @_;
82  
83 if ($self->type eq "normal") {
84 return(1);
85 } else {
86 return(0);
87 }
88 }
89  
90 sub isSparse {
91 my ($self) = @_;
92  
93 if ($self->type eq "sparse") {
94 return(1);
95 } else {
96 return(0);
97 }
98 }
99  
100  
101 sub type {
102 my $self = shift;
103  
104 if (@_) {
105 $self->{'type'} = shift;
106 }
107 return($self->{'type'});
108 }
109  
110 sub countVal {
111 my $self = shift;
112  
113 if (@_) {
114 $self->{'countVal'} = shift;
115 }
116 return($self->{'countVal'});
117 }
118  
119 sub values {
120 my $self = shift;
121  
122 if (@_) {
123 my $values_ref = shift;
124 push @{$self->{'values'}}, @$values_ref;
125 if (defined $self->values) {
126 $self->countVal(scalar(@{$self->values}));
127 }
128  
129 }
130 return($self->{'values'});
131 }
132  
133  
134  
135 sub incr_value {
136 my $self = shift;
137 my $index = shift;
138  
139 if ($index >= $self->countVal) {
140 warn "\nindex ($index) out of range (countVal=" . $self->countVal . " - a)\n";
141 }
142  
143 $self->values->[$index]++;
144 return($self->values->[$index]);
145 }
146  
147  
148 sub value {
149 my $self = shift;
150 my $index = shift;
151  
152 if ($index >= $self->countVal) {
153 warn "\nindex ($index) out of range (countVal=" . $self->countVal . " - b)\n";
154 }
155  
156 if (@_) {
157 $self->values->[$index] = shift;
158 }
159 return($self->values->[$index]);
160 }
161  
162  
163 sub getARFF {
164 my ($self) = @_;
165  
166 my $i;
167 my $arffString = "";
168  
169 if (defined $self->comment) {
170 $arffString .= "% " . $self->comment . "\n";
171 }
172  
173 if ($self->isNormal) {
174 $arffString .= join(',',@{$self->values}) . ",";
175 if (defined $self->class) {
176 $arffString .= $self->class;
177 } else {
178 $arffString .= "?"
179 }
180 }
181  
182 if ($self->isSparse) {
183 $arffString .= '{';
184 for($i=0;$i < $self->countVal;$i++) {
185 if (defined $self->values->[$i]) {
186 # warn $self->values->[$i] . "($i)\n";
187 $arffString .= "$i " . $self->values->[$i] . ", ";
188 }
189 }
190 $arffString .= $self->countVal . " ";
191 if (defined $self->class) {
192 $arffString .= $self->class;
193 } else {
194 $arffString .= "?"
195 }
196 $arffString .= '}';
197 }
198  
199  
200 return($arffString);
201 }
202  
203 sub getSVM {
204 my $self = shift;
205 my $classes = shift;
206 my $printComments = shift;
207 my $svmString;
208 my $i;
209  
210 if (((!defined $printComments) || ($printComments == 1)) && (defined $self->comment)) {
211 $svmString = $self->comment . "\n";
212 }
213 if (defined $self->class) {
214 # warn "====> " . $self->class . ";\n";
215 if (scalar(keys %$classes) == 1) {
216 if (exists $classes->{$self->class}) {
217 $svmString .= "+1 ";
218 } else {
219 $svmString .= "-1 ";
220 }
221 } else {
222 # warn "==> $classes\n";
223 # for my $c (keys(%{$classes})) {
224 # warn "$c : " . $classes->{$c} . "\n";
225 # }
226 # warn $self->class . "\n";
227 if (exists $classes->{$self->class}) {
228 $svmString .= $classes->{$self->class} . " ";
229 }
230 }
231 } else {
232 $svmString .= "0 ";
233 }
234 # warn "$svmString\n";
235 if (defined $self->qid) {
236 $svmString .= "qid:" . $self->qid . " ";
237 }
238  
239 for($i=0;$i < $self->countVal;$i++) {
240 if (defined $self->values->[$i]) {
241 $svmString .= ($i+1) . ":" . $self->values->[$i] . " ";
242 }
243 }
244 return($svmString);
245 }
246  
247  
248 =head1 NAME
249  
250 Lingua::Ogmios::LearningDataSet::Data - Perl extension for ???.
251  
252 =head1 SYNOPSIS
253  
254 use Lingua::Ogmios::LearningDataSet::Data;
255  
256 my %config = Lingua::Ogmios::LearningDataSet::Data::load_config($rcfile);
257  
258 $yatea = Lingua::Ogmios::LearningDataSet::Data->new($config{"OPTIONS"}, \%config);
259  
260 $yatea->termExtraction($corpus);
261  
262  
263 =head1 DESCRIPTION
264  
265  
266 =head1 METHODS
267  
268 =head2 function()
269  
270 function($rcfile);
271  
272 =head1 SEE ALSO
273  
274  
275 =head1 AUTHORS
276  
277 Thierry Hamon <thierry.hamon@limsi.fr>
278  
279 =head1 LICENSE
280  
281 Copyright (C) 2013 by Thierry Hamon
282  
283 This library is free software; you can redistribute it and/or modify
284 it under the same terms as Perl itself, either Perl version 5.8.6 or,
285 at your option, any later version of Perl 5 you may have available.
286  
287  
288 =cut
289  
290  
291 1;