File Coverage

blib/lib/NNexus/Concepts.pm
Criterion Covered Total %
statement 69 69 100.0
branch 9 10 90.0
condition 3 4 75.0
subroutine 10 10 100.0
pod 4 4 100.0
total 95 97 97.9


line stmt bran cond sub pod time code
1             # /=====================================================================\ #
2             # | NNexus Autolinker | #
3             # | Concept Manipulation and Lookup Module | #
4             # |=====================================================================| #
5             # | Part of the Planetary project: http://trac.mathweb.org/planetary | #
6             # | Research software, produced as part of work done by: | #
7             # | the KWARC group at Jacobs University | #
8             # | Copyright (c) 2012 | #
9             # | Released under the MIT License (MIT) | #
10             # |---------------------------------------------------------------------| #
11             # | Adapted from the original NNexus code by | #
12             # | James Gardner and Aaron Krowne | #
13             # |---------------------------------------------------------------------| #
14             # | Deyan Ginev #_# | #
15             # | http://kwarc.info/people/dginev (o o) | #
16             # \=========================================================ooo==U==ooo=/ #
17             package NNexus::Concepts;
18 8     8   17256 use strict;
  8         12  
  8         272  
19 8     8   28 use warnings;
  8         10  
  8         230  
20              
21 8     8   637 use NNexus::Morphology qw(is_possessive is_plural normalize_word);
  8         16  
  8         423  
22 8     8   42 use Encode qw( is_utf8 );
  8         14  
  8         285  
23 8     8   29 use Exporter;
  8         10  
  8         304  
24 8     8   4179 use List::MoreUtils qw(uniq);
  8         61733  
  8         59  
25              
26             our @ISA = qw(Exporter);
27             our @EXPORT_OK = qw(flatten_concept_harvest diff_concept_harvests clone_concepts links_from_concept);
28              
29             sub flatten_concept_harvest {
30 9     9 1 27 my ($indexed_concepts) = @_;
31 9         18 my $new_concepts=[];
32 9         22 foreach my $c(@$indexed_concepts) {
33 13   100     59 my $synonyms = delete $c->{synonyms}||[];
34 13   50     41 my $categories = delete $c->{categories}||[];
35 13         35 my @all_names = (@$synonyms, $c->{concept});
36             # Extend to normalized names:
37 32         73 @all_names = map {
38 13         24 join(' ', grep {$_} map { normalize_word($_) } split(/\s+/,$_))
  18         59  
  32         66  
39             } @all_names;
40             # In case some of the synonyms were misguidedly normalizing, let's get the unique elements:
41 13         70 @all_names = uniq(@all_names);
42             # Flatten names
43 13         27 my @synset = map {my %syn = %$c; $syn{concept}=$_; \%syn;} @all_names;
  18         53  
  18         26  
  18         33  
44             # Flatten categories
45 13         14 my @catset;
46 13         20 foreach my $sync (@synset) {
47 18         27 push @catset, map {my %cat = %$sync; $cat{category}=$_; \%cat;} @$categories;
  24         53  
  24         41  
  24         46  
48             }
49 13         108 push @$new_concepts, @catset;
50             }
51 9         26 return $new_concepts;
52             }
53              
54             sub diff_concept_harvests {
55 9     9 1 1165 my ($old_concepts,$new_concepts) = @_;
56 9         16 my $delete_concepts=[];
57 9         21 my $add_concepts = [@$new_concepts];
58 9         40 while (@$old_concepts) {
59 10         12 my $c = shift @$old_concepts;
60 10         16 my $cname = $c->{concept};
61 10         11 my $ccat = $c->{category};
62 10 100       13 my @filtered_new = grep {($_->{concept} ne $cname) ||
  29         80  
63             ($_->{category} ne $ccat)} @$add_concepts;
64 10 100       19 if (scalar(@filtered_new) == scalar(@$add_concepts)) {
65             # Not found, delete $c
66 3         11 push @$delete_concepts, $c;
67             } else {
68             # Found, next
69 7         28 $add_concepts = \@filtered_new;
70             }
71             }
72 9         26 return ($delete_concepts,$add_concepts);
73             }
74              
75             sub clone_concepts {
76 401     401 1 436 my ($concepts) = @_;
77             # Shallow clone suffices
78 401         1084 [map { {%$_} } @$concepts];
  182         835  
79             }
80              
81             sub links_from_concept {
82 13     13 1 16 my ($concept) = @_;
83 13         19 my @links = ();
84 13 100       138 @links = ($concept->{link}) if $concept->{link};
85             # Also include multilinks, if any:
86 13 100       28 if ($concept->{multilinks}) {
87 3         1 my @multi = @{$concept->{multilinks}};
  3         6  
88 3         6 while (@multi) {
89 6         4 my $next_link = shift @multi;
90 6 50       7 next if (grep {$_ eq $next_link} @links);
  3         6  
91 6         21 push @links, $next_link;
92             }
93             }
94 13         28 return @links; }
95              
96             1;
97              
98             __END__