blib/lib/Alvis/Wikipedia/CatGraph.pm untested

filter: Statementstmt 0.0% 0 / 240 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 397 CC 37 SCAR 33.5
CC
37
Cov
0%
get_relative_scores
72.0
build_path_length_map
42.0
get_two_top_levels_cats
42.0
CRAP 28.6
1 package Alvis::Wikipedia::CatGraph;
2  
3 use warnings;
4 use strict;
5  
6 use Storable;
7  
8 $Alvis::Wikipedia::CatGraph::VERSION = '0.1';
9  
10 #############################################################################
11 #
12 # Exported graph types
13 #
14 #############################################################################
15  
16 our ($TWO_TOP_LEVELS, # top 2 levels of the graph
17 $GIVEN_LIST # a given list of categories
18 )=(0..1);
19  
20 #############################################################################
21 #
22 # Global variables & constants
23 #
24 ##############################################################################
25  
26 my $DEF_ROOT='fundamental';
27 my $DEBUG=0;
28  
29 my $DEF_METHOD=$TWO_TOP_LEVELS;
30  
31 #############################################################################
32 #
33 # Error message stuff
34 #
35 #############################################################################
36  
37 my ($ERR_OK,
38 $ERR_LOAD,
39 $ERR_LOAD_UNDEF,
40 $ERR_NO_ROOT_CAT,
41 $ERR_UNDEF_LIST,
42 $ERR_NONEXIST_CAT,
43 $ERR_UNK_CAT_METHOD,
44 $ERR_NO_GRAPH,
45 $ERR_DUMP
46 )=(0..8);
47 my %ErrMsgs=($ERR_OK=>"",
48 $ERR_LOAD=>"Loading a graph dump failed.",
49 $ERR_LOAD_UNDEF=>"Loaded an undefined graph.",
50 $ERR_NO_ROOT_CAT=>"Root category not found in the graph.",
51 $ERR_UNDEF_LIST=>"Undefined category list.",
52 $ERR_NONEXIST_CAT=>"Category in the category list does not " .
53 "appear in the given graph.",
54 $ERR_UNK_CAT_METHOD=>"Unrecognized categorization method.",
55 $ERR_NO_GRAPH=>"No graph.",
56 $ERR_DUMP=>"Dumping the graph failed."
57 );
58  
59  
60 sub _set_err_state
61 {
62 my $self=shift;
63 my $errcode=shift;
64 my $errmsg=shift;
65  
66 if (!defined($errcode))
67 {
68 confess("set_err_state() called with an undefined argument.");
69 }
70  
71 if (exists($ErrMsgs{$errcode}))
72 {
73 if ($errcode==$ERR_OK)
74 {
75 $self->{errstr}="";
76 }
77 else
78 {
79 $self->{errstr}.=" " . $ErrMsgs{$errcode};
80 if (defined($errmsg))
81 {
82 $self->{errstr}.=" " . $errmsg;
83 }
84  
85 }
86 }
87 else
88 {
89 confess("Internal error: set_err_state() called with an " .
90 "unrecognized argument ($errcode).")
91 }
92 }
93  
94 sub clearerr
95 {
96 my $self=shift;
97
98 $self->{errstr}="";
99 }
100  
101 sub errmsg
102 {
103 my $self=shift;
104
105 return $self->{errstr};
106 }
107  
108 #############################################################################
109 #
110 # Public methods
111 #
112 ##############################################################################
113
114 sub new
115 {
116 my $proto=shift;
117  
118 my $class=ref($proto)||$proto;
119 my $parent=ref($proto)&&$proto;
120 my $self={};
121 bless($self,$class);
122  
123  
124 $self->_init(@_);
125  
126 $self->_set_err_state($ERR_OK);
127  
128 return $self;
129 }
130  
131 sub _init
132 {
133 my $self=shift;
134  
135 $self->{root}=$DEF_ROOT;
136 $self->{method}=$DEF_METHOD;
137  
138 if (defined(@_))
139 {
140 my %args=@_;
141 @$self{ keys %args }=values(%args);
142 }
143 }
144  
145 sub dump_graph
146 {
147 my $self=shift;
148 my $f=shift;
149  
150 eval
151 {
152 store($self->{G},$f);
153 };
154 if ($@)
155 {
156 $self->_set_err_state($ERR_DUMP,"file: $f");
157 return 0;
158 }
159  
160 return 1;
161 }
162  
163 sub load_graph
164 {
165 my $self=shift;
166 my $f=shift;
167  
168 eval
169 {
170 $self->{G}=retrieve($f);
171 };
172 if ($@)
173 {
174 $self->_set_err_state($ERR_LOAD,"file: $f");
175 return 0;
176 }
177 if (!defined($self->{G}))
178 {
179 $self->_set_err_state($ERR_LOAD_UNDEF,"file: $f");
180 return 0;
181 }
182
183 return 1;
184 }
185  
186 sub load_path_length_map
187 {
188 my $self=shift;
189 my $f=shift;
190  
191 eval
192 {
193 $self->{pathLengthMap}=retrieve($f);
194 };
195 if ($@)
196 {
197 $self->_set_err_state($ERR_LOAD,"file: $f");
198 return 0;
199 }
200 if (!defined($self->{pathLengthMap}))
201 {
202 $self->_set_err_state($ERR_LOAD_UNDEF,"file: $f");
203 return 0;
204 }
205
206 return 1;
207 }
208  
209 sub dump_path_length_map
210 {
211 my $self=shift;
212 my $f=shift;
213  
214 eval
215 {
216 store($self->{pathLengthMap},$f);
217 };
218 if ($@)
219 {
220 $self->_set_err_state($ERR_LOAD,"file: $f");
221 return 0;
222 }
223
224 return 1;
225 }
226  
227 sub build_path_length_map
228 {
229 my $self=shift;
230 my $list=shift; # list of category candidates
231  
232 $self->{pathLengthMap}=();
233  
234 my $L1=0; my $L2=1;
235  
236 if ($self->{method} eq $TWO_TOP_LEVELS)
237 {
238 if (!defined($self->{root}) ||
239 !exists($self->{G}{$self->{root}}))
240 {
241 $self->_set_err_state($ERR_NO_ROOT_CAT);
242 return 0;
243 }
244 my %visited=($self->{root}=>1);
245 $self->{pathLengthMap}{$self->{root}}{$self->{root}}=0;
246  
247 for my $c (@{$self->{G}{$self->{root}}})
248 {
249 $visited{$c}=1;
250 $self->{pathLengthMap}{$c}{$c}=0;
251  
252 for my $c2 (@{$self->{G}{$c}})
253 {
254 $visited{$c2}=1;
255 $self->{pathLengthMap}{$c2}{$c2}=0;
256  
257 for my $c3 (@{$self->{G}{$c2}})
258 {
259 my %c3_visited=%visited;
260 $self->_link_descendants($c3,$c2,\%c3_visited);
261 undef %c3_visited;
262 }
263 }
264 }
265
266 }
267 elsif ($self->{method} eq $GIVEN_LIST)
268 {
269 if (!defined($list))
270 {
271 $self->_set_err_state($ERR_UNDEF_LIST);
272 return 0;
273 }
274  
275 my %visited=();
276  
277 for my $c (@$list)
278 {
279 $visited{$c}=1;
280 $self->{pathLengthMap}{$c}{$c}=0;
281  
282 for my $c2 (@{$self->{G}{$c}})
283 {
284 if (!exists($visited{$c2}))
285 {
286 %visited=();
287 $visited{$c}=1;
288 $self->_link_descendants($c2,$c,\%visited);
289 }
290 }
291 }
292 }
293 else
294 {
295 $self->_set_err_state($ERR_UNK_CAT_METHOD);
296 return 0;
297 }
298  
299 return 1;
300 }
301  
302 sub _link_descendants
303 {
304 my $self=shift;
305 my $node=shift;
306 my $ancestor=shift;
307 my $visited=shift;
308  
309 my @stack=([$ancestor,$node]);
310 while (scalar(@stack))
311 {
312 my $p=pop(@stack);
313 my $n=pop(@$p);
314  
315 my $length=scalar(@$p) - 1;
316 if (!defined($self->{pathLengthMap}{$n}{$ancestor}) ||
317 $length<$self->{pathLengthMap}{$n}{$ancestor})
318 {
319 $self->{pathLengthMap}{$n}{$ancestor}=$length;
320 }
321  
322 $visited->{$n}=1;
323  
324 for my $child (@{$self->{G}{$n}})
325 {
326 if (!exists($visited->{$child}))
327 {
328 push(@stack,[@$p,$n,$child]);
329 }
330 }
331 }
332 }
333  
334 sub _add_to_cat_list
335 {
336 my $self=shift;
337 my $node=shift;
338 my $visited=shift;
339 my $list=shift;
340  
341 my @stack=($node);
342 while (scalar(@stack))
343 {
344 my $n=pop(@stack);
345  
346 if (!exists($visited->{$n}))
347 {
348 $visited->{$n}=1;
349 push(@$list,$n);
350 }
351  
352 for my $child (@{$self->{G}{$n}})
353 {
354 if (!exists($visited->{$child}))
355 {
356 push(@stack,$child);
357 }
358 }
359 }
360 }
361  
362 sub get_list_of_cats
363 {
364 my $self=shift;
365
366 if (!exists($self->{G})||!defined($self->{G}))
367 {
368 $self->_set_err_state($ERR_NO_GRAPH);
369 return undef;
370 }
371  
372 my @list=();
373  
374 if (!defined($self->{root}) ||
375 !exists($self->{G}{$self->{root}}))
376 {
377 $self->_set_err_state($ERR_NO_ROOT_CAT);
378 return 0;
379 }
380 my %visited=($self->{root}=>1);
381 push(@list,$self->{root});
382
383 for my $c (@{$self->{G}{$self->{root}}})
384 {
385 if (!exists($visited{$c}))
386 {
387 $visited{$c}=1;
388 push(@list,$c);
389 }
390
391 for my $c2 (@{$self->{G}{$c}})
392 {
393 if (!exists($visited{$c2}))
394 {
395 $visited{$c2}=1;
396 push(@list,$c2);
397 $self->_add_to_cat_list($c2,\%visited,\@list);
398 }
399 }
400 }
401  
402 return @list;
403 }
404  
405 sub get_two_top_levels_cats
406 {
407 my $self=shift;
408 my $root=shift;
409
410 if (!exists($self->{G})||!defined($self->{G}))
411 {
412 $self->_set_err_state($ERR_NO_GRAPH);
413 return undef;
414 }
415  
416 if (defined($root))
417 {
418 $self->{root}=$root;
419 }
420  
421 my @list=();
422  
423 if (!defined($self->{root}) ||
424 !exists($self->{G}{$self->{root}}))
425 {
426 $self->_set_err_state($ERR_NO_ROOT_CAT);
427 return 0;
428 }
429
430 my %visited=($self->{root}=>1);
431 for my $c (@{$self->{G}->{$self->{root}}})
432 {
433 if (!exists($visited{$c}))
434 {
435 $visited{$c}=1;
436 push(@list,$c);
437 }
438
439 for my $c2 (@{$self->{G}->{$c}})
440 {
441 if (!exists($visited{$c2}))
442 {
443 $visited{$c2}=1;
444 push(@list,$c2);
445 }
446 }
447 }
448  
449 return @list;
450 }
451  
452 sub get_relative_scores
453 {
454 my $self=shift;
455 my $cat_nodes=shift;
456  
457 my %scores=();
458  
459 my %lengths=();
460 my ($min_length,$max_length)=(10000000,0);
461 my (%lengths_per_c);
462 for my $n (@$cat_nodes)
463 {
464 if (exists($self->{pathLengthMap}{$n}))
465 {
466 for my $c (keys %{$self->{pathLengthMap}{$n}})
467 {
468 my $l=$self->{pathLengthMap}{$n}{$c};
469 if (!exists($lengths_per_c{$c}) || $l<$lengths_per_c{$c})
470 {
471 $lengths_per_c{$c}=$l;
472 }
473 }
474 }
475 else
476 {
477 warn "No path length from \"$n\" to any of the categories!\n";
478 }
479 }
480  
481 for my $length (values %lengths_per_c)
482 {
483 if ($length<$min_length)
484 {
485 $min_length=$length;
486 }
487 if ($length>$max_length)
488 {
489 $max_length=$length;
490 }
491 }
492  
493 for my $n (@$cat_nodes)
494 {
495 if (exists($self->{pathLengthMap}{$n}))
496 {
497 for my $c (keys %{$self->{pathLengthMap}{$n}})
498 {
499 my $l=$self->{pathLengthMap}{$n}{$c};
500 my $score;
501 my $MAX=10;
502 if ($max_length>$min_length)
503 {
504 $score=$MAX*(1-($l-$min_length)/($max_length-$min_length));
505 }
506 else
507 {
508 $score=$MAX;
509 }
510 if (!exists($scores{$c}) || $score>$scores{$c})
511 {
512 $scores{$c}=$score;
513 }
514 }
515 }
516 }
517  
518 return \%scores;
519 }
520  
521 sub add_link
522 {
523 my $self=shift;
524 my $cat=shift;
525 my $parent=shift;
526  
527 push(@{$self->{G}{$parent}},$cat);
528 }
529  
530 1;
531 __END__