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

filter: Statementstmt 0.0% 0 / 253 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 378 SCAR 67.7
CC
29
Cov
0%
parseData
56.0
classes
30.0
parseARFF
30.0
CRAP 870.0
1 package Lingua::Ogmios::LearningDataSet;
2  
3  
4 use strict;
5 use warnings;
6  
7 our $VERSION='0.1';
8  
9 sub new {
10  
11 my $class = shift;
12  
13 my $learningDataSet = {
14 'relation' => undef,
15 'attributes' => [],
16 'attribute_index' => {},
17 'dataset' => [],
18 'comments' => [],
19 'classes' => {},
20
21 };
22  
23 bless $learningDataSet, $class;
24  
25 return($learningDataSet);
26 }
27  
28 sub relation {
29 my $self = shift;
30  
31 if (@_) {
32 $self->{relation} = shift;
33 }
34 return($self->{relation});
35 }
36  
37 sub attributes {
38 my $self = shift;
39  
40 if (@_) {
41 $self->{attributes} = shift;
42 }
43 return($self->{attributes});
44 }
45  
46 sub countValAttr {
47 my $self = shift;
48  
49 return(scalar(@{$self->{attributes}}));
50 }
51  
52 sub attribute_index {
53 my $self = shift;
54  
55 if (@_) {
56 $self->{attribute_index} = shift;
57 }
58 return($self->{attribute_index});
59 }
60  
61 sub classes {
62 my $self = shift;
63  
64 if (@_) {
65 my $classes = shift;
66 if (ref($classes) eq "HASH") {
67 $self->{classes} = {};
68 %{$self->{classes}} = %$classes;
69 } elsif (ref($classes) eq "ARRAY"){
70
71 my $i = 0;
72 $self->{classes} = {};
73 for($i=0; $i < scalar(@$classes); $i++) {
74 $self->{classes}->{$classes->[$i]} = $i + 1;
75 }
76 } elsif (ref($classes) eq "") {
77 # TODO
78 }
79 }
80 return($self->{classes});
81 }
82  
83 sub firstClass {
84 my $self = shift;
85  
86 # if (@_) {
87 # $self->{'fistClass'} = shift;
88 # }
89  
90 my @tmp = sort { $self->classes->{$a} <=> $self->classes->{$b} } keys %{$self->classes};
91  
92 return($tmp[0]);
93 }
94  
95 sub existsInClasses {
96 my ($self, $class) = @_;
97  
98 # warn $self->classes . "\n";
99 return(exists($self->classes->{$class}));
100 }
101  
102 sub dataset {
103 my $self = shift;
104  
105 if (@_) {
106 $self->{dataset} = shift;
107 }
108 return($self->{dataset});
109 }
110  
111 sub comments {
112 my $self = shift;
113  
114 if (@_) {
115 $self->{comments} = shift;
116 }
117 return($self->{comments});
118 }
119  
120 sub addAttribute {
121 my ($self, $attr) = @_;
122  
123 push @{$self->attributes}, $attr;
124 $self->attribute_index->{$attr->name} = scalar(@{$self->attributes}) - 1;
125 $attr->comment(scalar(@{$self->attributes}) - 1);
126 return($self->attribute_index->{$attr->name});
127 }
128  
129 sub delAttribute {
130 my ($self, $attr_name) = @_;
131
132 splice @{$self->attributes}, $self->attribute_index->{$attr_name}, 1;
133 delete $self->attribute_index->{$attr_name};
134
135 }
136  
137 sub getAttributeIndex {
138 my ($self, $attr_name) = @_;
139  
140 return($self->attribute_index->{$attr_name});
141
142 }
143  
144  
145 sub existsAttribute {
146 my ($self, $attr_name) = @_;
147  
148 return(exists $self->attribute_index->{$attr_name});
149  
150 }
151  
152 sub addData {
153 my ($self, $data) = @_;
154  
155 push @{$self->dataset}, $data;
156  
157 }
158  
159 sub addComment {
160 my ($self, $comment) = @_;
161  
162 push @{$self->comments}, $comment;
163 }
164  
165 sub getARFF {
166 my $self = shift;
167 my $printheader = shift;
168  
169 my $arffString;
170  
171 if ((!defined $printheader) || ($printheader == 1)) {
172 $arffString .= $self->getARFFHeader;
173 }
174  
175 $arffString .= '@DATA' . "\n";
176 $arffString .= $self->getARFFData;
177 return($arffString);
178 }
179  
180 sub getARFFData {
181 my $self = shift;
182  
183 my $arffString;
184 my $data;
185  
186 foreach $data (@{$self->dataset}) {
187 $arffString .= $data->getARFF . "\n";
188 }
189 return($arffString);
190 }
191 sub getARFFHeader {
192 my $self = shift;
193  
194 my $arffString;
195 my $comment;
196 my $attribute;
197 my $data;
198  
199 foreach $comment (@{$self->comments}) {
200 $arffString .= "% $comment\n";
201 }
202 $arffString .= '@RELATION ' . $self->relation . "\n\n";
203
204 foreach $attribute (@{$self->attributes}) {
205 $arffString .= $attribute->getARFF . "\n";
206 }
207 $arffString .= '@ATTRIBUTE class {' . join (",", sort({$self->classes->{$a} <=> $self->classes->{$b}} keys(%{$self->classes}))) . "}\n";
208 $arffString .= "\n";
209  
210 return($arffString);
211 }
212  
213 sub getSVM {
214 my $self = shift;
215 my $printComments = shift;
216 my $SVMString;
217 my $comment;
218 my $data;
219  
220 if ((!defined $printComments) || ($printComments == 1)) {
221 foreach $comment (@{$self->comments}) {
222 $SVMString .= "# $comment\n";
223 }
224 }
225  
226 foreach $data (@{$self->dataset}) {
227 $SVMString .= $data->getSVM($self->classes, $printComments) . "\n";
228 }
229 return($SVMString);
230
231 }
232  
233 sub loadAttributesFromFile {
234 my ($self, $filename, $type, $prefix) = @_;
235 my $line;
236 my $attr;
237  
238 warn "openning $filename\n";
239 open FILE, $filename or die "No such file $filename\n";
240 binmode(FILE, ":utf8");
241  
242 while($line = <FILE>) {
243 chomp $line;
244 if ($line !~ /^\s*#/o) {
245 $attr = Lingua::Ogmios::LearningDataSet::Attribute->new(
246 {"name" => "$prefix". "_" . $line,
247 "type" => uc($type),
248 "prefix" => $prefix,
249 "value" => $line,
250 }
251 );
252 $self->addAttribute($attr);
253 }
254 }
255  
256 close FILE;
257 }
258  
259 sub parseARFF {
260 my ($self, $filename) = @_;
261  
262 my $line;
263 my $relation;
264 my $attribute;
265 my $name;
266 my $type;
267 my $data;
268 my $info_attr;
269 my $prefix;
270 my @classes;
271 my @tmp ;
272  
273 open FILE, $filename or die "No such file $filename\n";
274 binmode(FILE, ":utf8");
275 while($line = <FILE>) {
276 chomp $line;
277  
278 # Relation
279 if ($line =~ /\@relation\s+/io) {
280 $relation = $'; # '
281 $relation =~ s/^['"]//o;
282 $relation =~ s/['"]$//o;
283 $self->relation($relation);
284 }
285  
286 # Attribute
287 if ($line =~ /\@attribute\s+/io) {
288 $info_attr = $'; # '
289 ($name, $type) = split / /, $info_attr;
290 if ($name eq "class") {
291 $type =~ s/^{//o;
292 $type =~ s/}$//o;
293 @classes = split /\s*,\s*/, $type;
294 $self->classes(\@classes);
295 } else {
296 @tmp = split /_/, $name;
297 $prefix = shift @tmp;
298
299 $attribute = Lingua::Ogmios::LearningDataSet::Attribute->new(
300 {"name" => $name,
301 "type" => $type,
302 "prefix" => $prefix,
303 "value" => join('_', @tmp),
304 }
305 );
306 $self->addAttribute($attribute);
307 }
308 }
309
310 # Data
311 if ($line =~ /\@data\s*/io) {
312 # warn "$line\n";
313 # while($line = <FILE>) {
314 # chomp $line;
315 $self->parseDataset(\*FILE);
316 # }
317 }
318  
319 }
320  
321 close FILE;
322 return(1);
323 }
324  
325 sub parseData {
326 my ($self, $fh, $line) = @_;
327  
328 my $addr_val;
329 my $addr;
330 my $val;
331 my $data;
332 my @values;
333 my $index;
334 # warn "line: $line\n";
335 if ($line =~ /{/o) {
336 $data = Lingua::Ogmios::LearningDataSet::Data->new({type => "sparse", 'countVal' => $self->countValAttr});
337 $line =~ s/^{\s*//o;
338 $line =~ s/\s*}$//o;
339 } else {
340 $data = Lingua::Ogmios::LearningDataSet::Data->new({type => "normal", 'countVal' => $self->countValAttr});
341 }
342 @values = split /\s*,\s*/, $line;
343 $addr_val = pop @values;
344 if (defined $addr_val) {
345 if ($data->isSparse) {
346 ($addr, $val) = split /\s+/, $addr_val;
347 } else {
348 $val = $addr_val;
349 }
350 } else {
351 $val = undef;
352 }
353 # warn "line: $line\n$addr\n$val\n";
354
355 if ((defined $val) && ($self->existsInClasses($val))) {
356 # warn "\n\n==>$val\n";exit;
357 } else {
358 $val = $self->firstClass;
359 if (defined $addr_val) {
360 push @values, $addr_val;
361 }
362 }
363 # warn "val: $val\n";
364 $data->class($val);
365  
366 # warn "\n\n====>" . $data->class . "\n";exit;
367  
368 # $data->countVal($val);
369 $index = 0;
370 foreach $addr_val (@values) {
371 if ($data->isSparse) {
372 ($addr, $val) = split /\s+/, $addr_val;
373 } else {
374 $val = $addr_val;
375 $addr = $index;
376 }
377 $data->value($addr, $val);
378 $index++;
379 }
380  
381 $self->addData($data);
382 }
383  
384 sub parseDataset {
385 my ($self, $fh) = @_;
386  
387 my $line;
388 while ($line = <$fh>) {
389 chomp $line;
390 if ($line !~ /^\s*$/o) {
391 $self->parseData($fh, $line);
392 }
393 }
394 }
395 sub printAttributes {
396 my ($self, $corresp, $fh) = @_;
397 my $name;
398 my $attribute;
399  
400 if (!defined $fh) {
401 $fh = \*STDOUT;
402 }
403 binmode($fh, ":utf8");
404  
405 if (!defined $corresp) {
406 $corresp = 0;
407 }
408 foreach $attribute (@{$self->attributes}) {
409 $name = $attribute->name;
410 $name =~ s/^[^_]+_//;
411 print $fh "$name";
412 if ($corresp == 1) {
413 print $fh "\t" . $attribute->name;
414 print $fh "\t" . $attribute->type;
415 }
416 print $fh "\n";
417 }
418  
419 }
420  
421  
422 sub printDataset {
423 my ($self, $fh) = @_;
424  
425 my $data;
426 if (!defined $fh) {
427 $fh = \*STDOUT;
428 }
429 binmode($fh, ":utf8");
430  
431 foreach $data (@{$self->dataset}) {
432 print $fh $data->getARFF . "\n";
433 print $fh $data->getSVM($self->classes) . "\n";
434 }
435 }
436 sub printRelation {
437 my ($self, $fh) = @_;
438  
439 if (!defined $fh) {
440 $fh = \*STDOUT;
441 }
442 binmode($fh, ":utf8");
443  
444 print $fh $self->relation . "\n";
445
446 }
447  
448 1;
449  
450 __END__