| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||||||||||
| 2 | ## File: DiaColloDB::Compat::v0_09::Relation.pm | ||||||||||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||
| 4 | ## Description: collocation db, relation API (abstract & utilities) | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package DiaColloDB::Compat::v0_09::Relation; | ||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use DiaColloDB::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DiaColloDB::Relation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use DiaColloDB::Utils qw(:si); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Algorithm::BinarySearch::Vec qw(:api); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ##============================================================================== | ||||||||||||||||||||||||||||
| 14 | ## Globals & Constants | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our @ISA = qw(DiaColloDB::Relation DiaColloDB::Compat); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ##============================================================================== | ||||||||||||||||||||||||||||
| 19 | ## Constructors etc. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ## $rel = CLASS_OR_OBJECT->new(%args) | ||||||||||||||||||||||||||||
| 22 | ## + %args, object structure: see subclases | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($that,%args) = @_; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | return bless({ %args }, ref($that)||$that); | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | ##============================================================================== | ||||||||||||||||||||||||||||
| 29 | ## Relation API: create | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ## $rel = $CLASS_OR_OBJECT->create($coldb, $tokdat_file, %opts) | ||||||||||||||||||||||||||||
| 32 | ## + populates current database from $tokdat_file, | ||||||||||||||||||||||||||||
| 33 | ## a tt-style text file containing 1 token-id perl line with optional blank lines | ||||||||||||||||||||||||||||
| 34 | ## + %opts: clobber %$rel | ||||||||||||||||||||||||||||
| 35 | ## + DISABLED | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | *create = DiaColloDB::Compat->nocompat('create'); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | ##============================================================================== | ||||||||||||||||||||||||||||
| 41 | ## Relation API: union | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ## $rel = $CLASS_OR_OBJECT->union($coldb, \@pairs, %opts) | ||||||||||||||||||||||||||||
| 44 | ## + merge multiple co-frequency indices into new object | ||||||||||||||||||||||||||||
| 45 | ## + @pairs : array of pairs ([$argrel,\@xi2u],...) | ||||||||||||||||||||||||||||
| 46 | ## of relation-objects $argrel and tuple-id maps \@xi2u for $rel | ||||||||||||||||||||||||||||
| 47 | ## + %opts: clobber %$rel | ||||||||||||||||||||||||||||
| 48 | ## + implicitly flushes the new index | ||||||||||||||||||||||||||||
| 49 | ## + DISABLED | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | *union = DiaColloDB::Compat->nocompat('union'); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ##============================================================================== | ||||||||||||||||||||||||||||
| 55 | ## Relation API: info | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | ## \%info = $rel->dbinfo($coldb) | ||||||||||||||||||||||||||||
| 58 | ## + embedded info-hash for $coldb->dbinfo() | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub dbinfo { | ||||||||||||||||||||||||||
|
Subroutine
dbinfo: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 0 | my $rel = shift; | |||||||||||||||||||||||||||
| 61 | 0 | my $info = { class=>ref($rel) }; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($rel->can('du')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $info->{du_b} = $rel->du(); | |||||||||||||||||||||||||||
| 64 | 0 | $info->{du_h} = si_str($info->{du_b}); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | return $info; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ##============================================================================== | ||||||||||||||||||||||||||||
| 71 | ## Relation API: profiling & comparison: top-level | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 74 | ## Relation API: profile | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | ## $mprf = $rel->profile($coldb, %opts) | ||||||||||||||||||||||||||||
| 77 | ## + get a relation profile for selected items as a DiaColloDB::Profile::Multi object | ||||||||||||||||||||||||||||
| 78 | ## + %opts: | ||||||||||||||||||||||||||||
| 79 | ## ( | ||||||||||||||||||||||||||||
| 80 | ## ##-- selection parameters | ||||||||||||||||||||||||||||
| 81 | ## query => $query, ##-- target request ATTR:REQ... | ||||||||||||||||||||||||||||
| 82 | ## date => $date1, ##-- string or array or range "MIN-MAX" (inclusive) : default=all | ||||||||||||||||||||||||||||
| 83 | ## ## | ||||||||||||||||||||||||||||
| 84 | ## ##-- aggregation parameters | ||||||||||||||||||||||||||||
| 85 | ## slice => $slice, ##-- date slice (default=1, 0 for global profile) | ||||||||||||||||||||||||||||
| 86 | ## groupby => $groupby, ##-- string or array "ATTR1[:HAVING1] ...": default=$coldb->attrs; see groupby() method | ||||||||||||||||||||||||||||
| 87 | ## ## | ||||||||||||||||||||||||||||
| 88 | ## ##-- scoring and trimming parameters | ||||||||||||||||||||||||||||
| 89 | ## eps => $eps, ##-- smoothing constant (default=0) | ||||||||||||||||||||||||||||
| 90 | ## score => $func, ##-- scoring function ("f"|"lf"|"fm"|"lfm"|"mi"|"ld"|"ll") : default="f" | ||||||||||||||||||||||||||||
| 91 | ## kbest => $k, ##-- return only $k best collocates per date (slice) : default=-1:all | ||||||||||||||||||||||||||||
| 92 | ## cutoff => $cutoff, ##-- minimum score | ||||||||||||||||||||||||||||
| 93 | ## global => $bool, ##-- trim profiles globally (vs. locally for each date-slice?) (default=0) | ||||||||||||||||||||||||||||
| 94 | ## ## | ||||||||||||||||||||||||||||
| 95 | ## ##-- profiling and debugging parameters | ||||||||||||||||||||||||||||
| 96 | ## strings => $bool, ##-- do/don't stringify item keys (default=do) | ||||||||||||||||||||||||||||
| 97 | ## packed => $bool, ##-- leave item keys packed (default=don't) | ||||||||||||||||||||||||||||
| 98 | ## fill => $bool, ##-- if true, returned multi-profile will have null profiles inserted for missing slices | ||||||||||||||||||||||||||||
| 99 | ## onepass => $bool, ##-- if true, use fast but incorrect 1-pass method (Cofreqs subclass only) | ||||||||||||||||||||||||||||
| 100 | ## ) | ||||||||||||||||||||||||||||
| 101 | ## + default implementation | ||||||||||||||||||||||||||||
| 102 | ## - calls $rel->subprofile1() for every requested date-slice, then | ||||||||||||||||||||||||||||
| 103 | ## - calls $rel->subprofile2() to compute item2 frequencies, and finally | ||||||||||||||||||||||||||||
| 104 | ## - collects the result in a DiaColloDB::Profile::Multi object | ||||||||||||||||||||||||||||
| 105 | ## + default values for %opts should be set by higher-level call, e.g. DiaColloDB::profile() | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub profile { | ||||||||||||||||||||||||||
|
Subroutine
profile: not called CC=30 SCAR=68.4
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($reldb,$coldb,%opts) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ##-- sanity check(s) | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $reldb->logconfess("profile(): incompatible DB class ".ref($coldb).", v".($coldb->{version}//'???')." for directory $coldb->{dbdir}") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$coldb->{"version"} // "???"
MC/DC0%0/1
$coldb->{"version"} // "???"
$coldb->{"version"}
Truth table0%0/2
A
A= $coldb->{"version"}
| |||||||||||||||||||||||||||||
| 111 | if (!$coldb->isa('DiaColloDB::Compat::v0_09::DiaColloDB')); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ##-- common variables | ||||||||||||||||||||||||||||
| 114 | 0 | my $logProfile = $coldb->{logProfile}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | ##-- variables: by attribute | ||||||||||||||||||||||||||||
| 117 | 0 | my $groupby= $coldb->groupby($opts{groupby}); | |||||||||||||||||||||||||||
| 118 | 0 | my $attrs = $coldb->attrs(); | |||||||||||||||||||||||||||
| 119 | 0 | my $adata = $coldb->attrData($attrs); | |||||||||||||||||||||||||||
| 120 | 0 | my $a2data = {map {($_->{a}=>$_)} @$adata}; | |||||||||||||||||||||||||||
| 121 | 0 | my $areqs = $coldb->parseRequest($opts{query}, logas=>'query', default=>$attrs->[0]); | |||||||||||||||||||||||||||
| 122 | 0 | foreach (@$areqs) { | |||||||||||||||||||||||||||
| 123 | 0 | $a2data->{$_->[0]}{req} = $_->[1]; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | ##-- sanity check(s) | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if (!@$areqs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $reldb->logwarn($coldb->{error}="profile(): no target attributes specified (supported attributes: ".join(' ',@{$coldb->attrs}).")"); | |||||||||||||||||||||||||||
| 129 | 0 | return undef; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (!@{$groupby->{attrs}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $reldb->logconfess($coldb->{error}="profile(): cannot profile with empty groupby clause"); | |||||||||||||||||||||||||||
| 133 | 0 | return undef; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ##-- prepare: get target IDs (by attribute) | ||||||||||||||||||||||||||||
| 137 | 0 | my ($ac); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | foreach $ac (grep {($_->{req}//'') ne ''} @$adata) { | ||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"req"} // ""
MC/DC0%0/1
$_->{"req"} // ""
$_->{"req"}
Truth table0%0/2
A
A= $_->{"req"}
| |||||||||||||||||||||||||||||
| 139 | 0 | $ac->{reqids} = $coldb->enumIds($ac->{enum},$ac->{req},logLevel=>$logProfile,logPrefix=>"profile(): get target $ac->{a}-values"); | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (!@{$ac->{reqids}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $reldb->logwarn($coldb->{error}="profile(): no $ac->{a}-attribute values match user query '$ac->{req}'"); | |||||||||||||||||||||||||||
| 142 | 0 | return undef; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | ##-- prepare: get tuple-ids (by attribute) | ||||||||||||||||||||||||||||
| 147 | 0 | $reldb->vlog($logProfile, "profile(): get target tuple IDs"); | |||||||||||||||||||||||||||
| 148 | 0 | my $xivec = undef; | |||||||||||||||||||||||||||
| 149 | 0 | my $nbits = undef; | |||||||||||||||||||||||||||
| 150 | 0 | my $pack_xv = undef; | |||||||||||||||||||||||||||
| 151 | 0 | my $test_xv = undef; ##-- test value via vec() | |||||||||||||||||||||||||||
| 152 | 0 | foreach $ac (grep {$_->{reqids}} @$adata) { | |||||||||||||||||||||||||||
| 153 | ##-- sanity checks | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $nbits //= $ac->{a2x}{len_i}*8; | ||||||||||||||||||||||||||
|
Condition0%0/2
$nbits //= $ac->{"a2x"}{"len_i"} * 8
MC/DC0%0/1
$nbits //= $ac->{"a2x"}{"len_i"} * 8
$nbits
Truth table0%0/2
A
A= $nbits
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $pack_xv //= "$ac->{a2x}{pack_i}*"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$pack_xv //= qq[$ac->{"a2x"}{"pack_i"}*]
MC/DC0%0/1
$pack_xv //= qq[$ac->{"a2x"}{"pack_i"}*]
$pack_xv
Truth table0%0/2
A
A= $pack_xv
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | vec($test_xv='',0,$nbits) = 0x12345678 if (!defined($test_xv)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | $reldb->logconfess("profile(): multimap pack-size mismatch: nbits($ac->{a2x}{base}.*) != $nbits") | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($ac->{a2x}{len_i} != $nbits/8); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | $reldb->logconfess("profile(): multimap pack-template '$ac->{a2x}{pack_i}' for $ac->{a2x}{base}.* is not big-endian") | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if (pack($ac->{a2x}{pack_i},0x12345678) ne $test_xv); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ##-- target set construction | ||||||||||||||||||||||||||||
| 163 | 0 | my $axiset = ''; | |||||||||||||||||||||||||||
| 164 | 0 | $axiset = vunion($axiset, $ac->{a2x}->fetchraw($_), $nbits) foreach (@{$ac->{reqids}}); | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | $xivec = defined($xivec) ? vintersect($xivec, $axiset, $nbits) : $axiset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | ##-- check maxExpand | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | $nbits //= packsize($coldb->{pack_id}); | ||||||||||||||||||||||||||
|
Condition0%0/3
$nbits //= packsize($coldb->{"pack_id"})
MC/DC0%0/2
$nbits //= packsize($coldb->{"pack_id"})
$nbits
packsize($coldb->{"pack_id"})
Truth table0%0/3
A //= B
A= $nbitsB= packsize($coldb->{"pack_id"})
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $nxis = $xivec ? length($xivec)/($nbits/8) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($coldb->{maxExpand}>0 && $nxis > $coldb->{maxExpand}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$coldb->{"maxExpand"} > 0 and $nxis > $coldb->{"maxExpand"}
MC/DC0%0/2
$coldb->{"maxExpand"} > 0 and $nxis > $coldb->{"maxExpand"}
$coldb->{"maxExpand"} > 0
$nxis > $coldb->{"maxExpand"}
Truth table0%0/3
A and B
A= $coldb->{"maxExpand"} > 0B= $nxis > $coldb->{"maxExpand"}
| |||||||||||||||||||||||||||||
| 172 | 0 | $reldb->logwarn("profile(): Warning: target set exceeds max expansion size ($nxis > $coldb->{maxExpand}): truncating"); | |||||||||||||||||||||||||||
| 173 | 0 | substr($xivec, -($nxis - $coldb->{maxExpand})*($nbits/8)) = ''; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my $xis = [$xivec ? unpack($pack_xv, $xivec) : qw()]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ##-- prepare: parse and filter tuples | ||||||||||||||||||||||||||||
| 178 | 0 | $reldb->vlog($logProfile, "profile(): parse and filter target tuples (date=$opts{date}, slice=$opts{slice}, fill=$opts{fill})"); | |||||||||||||||||||||||||||
| 179 | 0 | my $d2xis = $coldb->xidsByDate($xis, @opts{qw(date slice fill)}); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | ##-- profile: get relation profiles (by date-slice, pass 1: f12) | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my $onepass = $opts{onepass} || ($reldb->can('subprofile2') eq \&subprofile2); | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts{'onepass'} || $reldb->can('subprofile2') eq \&DiaColloDB::Compat::v0_09::Relation::subprofile2
MC/DC0%0/2
$opts{'onepass'} || $reldb->can('subprofile2') eq \&DiaColloDB::Compat::v0_09::Relation::subprofile2
$opts{'onepass'}
$reldb->can('subprofile2') eq \&DiaColloDB::Compat::v0_09::Relation::subprofile2
Truth table0%0/3
A || B
A= $opts{'onepass'}B= $reldb->can('subprofile2') eq \&DiaColloDB::Compat::v0_09::Relation::subprofile2
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $reldb->vlog($logProfile, "profile(): get frequency profile(s): ".($onepass ? 'single-pass' : 'pass-1')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | my %d2prf = qw(); | |||||||||||||||||||||||||||
| 185 | 0 | my @slices = sort {$a<=>$b} keys %$d2xis; | |||||||||||||||||||||||||||
| 186 | 0 | my ($d,$prf); | |||||||||||||||||||||||||||
| 187 | 0 | foreach $d (@slices) { | |||||||||||||||||||||||||||
| 188 | 0 | $prf = $reldb->subprofile1($d2xis->{$d}, groupby=>$groupby->{xi2g}, coldb=>$coldb, onepass=>$onepass, opts=>\%opts); | |||||||||||||||||||||||||||
| 189 | 0 | $prf->{label} = $d; | |||||||||||||||||||||||||||
| 190 | 0 | $prf->{titles} = $groupby->{titles}; | |||||||||||||||||||||||||||
| 191 | 0 | $d2prf{$d} = $prf; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | ##-- profile: complete slice-wise profiles (pass 2: f2) | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (!$onepass) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | $reldb->vlog($logProfile, "profile(): get frequency profile(s): pass-2"); | |||||||||||||||||||||||||||
| 197 | 0 | $reldb->subprofile2(\%d2prf, %opts, coldb=>$coldb, groupby=>$groupby, a2data=>$a2data, opts=>\%opts); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | ##-- compile & collect: multi-profile | ||||||||||||||||||||||||||||
| 201 | 0 | foreach $prf (values %d2prf) { | |||||||||||||||||||||||||||
| 202 | 0 | $prf->compile($opts{score}, eps=>$opts{eps}); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | my $mp = DiaColloDB::Profile::Multi->new(profiles=>[@d2prf{@slices}], | ||||||||||||||||||||||||||||
| 205 | titles=>$groupby->{titles}, | ||||||||||||||||||||||||||||
| 206 | 0 | qinfo =>$reldb->qinfo($coldb, %opts, qreqs=>$areqs, gbreq=>$groupby), | |||||||||||||||||||||||||||
| 207 | ); | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | ##-- trim and stringify | ||||||||||||||||||||||||||||
| 210 | 0 | $reldb->vlog($logProfile, "profile(): trim and stringify"); | |||||||||||||||||||||||||||
| 211 | 0 | $mp->trim(%opts, empty=>!$opts{fill}); | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | if (!$opts{packed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if ($opts{strings}//1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"strings"} // 1
MC/DC0%0/1
$opts{"strings"} // 1
$opts{"strings"}
Truth table0%0/2
A
A= $opts{"strings"}
| |||||||||||||||||||||||||||||
| 214 | 0 | $mp->stringify($groupby->{g2s}); | |||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||
| 216 | 0 | $mp->stringify($groupby->{g2txt}); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | ##-- return | ||||||||||||||||||||||||||||
| 221 | 0 | return $mp; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 226 | ## Relation API: comparison (diff) | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | ## $mpdiff = $rel->compare($coldb, %opts) | ||||||||||||||||||||||||||||
| 229 | ## + get a relation comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object | ||||||||||||||||||||||||||||
| 230 | ## + %opts: | ||||||||||||||||||||||||||||
| 231 | ## ( | ||||||||||||||||||||||||||||
| 232 | ## ##-- selection parameters | ||||||||||||||||||||||||||||
| 233 | ## (a|b)?query => $query, ##-- target query as for parseRequest() | ||||||||||||||||||||||||||||
| 234 | ## (a|b)?date => $date1, ##-- string or array or range "MIN-MAX" (inclusive) : default=all | ||||||||||||||||||||||||||||
| 235 | ## ## | ||||||||||||||||||||||||||||
| 236 | ## ##-- aggregation parameters | ||||||||||||||||||||||||||||
| 237 | ## groupby => $groupby, ##-- string or array "ATTR1[:HAVING1] ...": default=$coldb->attrs; see groupby() method | ||||||||||||||||||||||||||||
| 238 | ## (a|b)?slice => $slice, ##-- date slice (default=1, 0 for global profile) | ||||||||||||||||||||||||||||
| 239 | ## ## | ||||||||||||||||||||||||||||
| 240 | ## ##-- scoring and trimming parameters | ||||||||||||||||||||||||||||
| 241 | ## eps => $eps, ##-- smoothing constant (default=0) | ||||||||||||||||||||||||||||
| 242 | ## score => $func, ##-- scoring function ("f"|"lf"|"fm"|"lfm"|"mi"|"ld"|"ll") : default="f" | ||||||||||||||||||||||||||||
| 243 | ## kbest => $k, ##-- return only $k best collocates per date (slice) : default=-1:all | ||||||||||||||||||||||||||||
| 244 | ## cutoff => $cutoff, ##-- minimum score | ||||||||||||||||||||||||||||
| 245 | ## global => $bool, ##-- trim profiles globally (vs. locally for each date-slice?) (default=0) | ||||||||||||||||||||||||||||
| 246 | ## diff => $diff, ##-- low-level score-diff operation (adiff|diff|sum|min|max|avg|havg); default='adiff' | ||||||||||||||||||||||||||||
| 247 | ## ## | ||||||||||||||||||||||||||||
| 248 | ## ##-- profiling and debugging parameters | ||||||||||||||||||||||||||||
| 249 | ## strings => $bool, ##-- do/don't stringify item keys (default=do) | ||||||||||||||||||||||||||||
| 250 | ## packed => $bool, ##-- leave item keys packed (override stringification; default=don't) | ||||||||||||||||||||||||||||
| 251 | ## ## | ||||||||||||||||||||||||||||
| 252 | ## ##-- sublcass abstraction parameters | ||||||||||||||||||||||||||||
| 253 | ## _gbparse => $bool, ##-- if true (default), 'groupby' clause will be parsed only once, using $coldb->groupby() method | ||||||||||||||||||||||||||||
| 254 | ## _abkeys => \@abkeys, ##-- additional key-suffixes KEY s.t. (KEY=>VAL) gets passed to profile() calls if e.g. (aKEY=>VAL) is in %opts | ||||||||||||||||||||||||||||
| 255 | ## ) | ||||||||||||||||||||||||||||
| 256 | ## + default implementation wraps profile() method | ||||||||||||||||||||||||||||
| 257 | ## + default values for %opts should be set by higher-level call, e.g. DiaColloDB::compare() | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub compare { | ||||||||||||||||||||||||||
|
Subroutine
compare: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 259 | 0 | my ($reldb,$coldb,%opts) = @_; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | ##-- common variables | ||||||||||||||||||||||||||||
| 262 | 0 | my $logProfile = $coldb->{logProfile}; | |||||||||||||||||||||||||||
| 263 | 0 | ▶ | my $groupby = $opts{groupby} || [@{$coldb->attrs}]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts{'groupby'} || [@{$coldb->attrs;}]
MC/DC0%0/1
$opts{'groupby'} || [@{$coldb->attrs;}]
$opts{'groupby'}
Truth table0%0/2
A
A= $opts{'groupby'}
| |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | $groupby = $coldb->groupby($groupby) if ($opts{_gbparse}//1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"_gbparse"} // 1
MC/DC0%0/1
$opts{"_gbparse"} // 1
$opts{"_gbparse"}
Truth table0%0/2
A
A= $opts{"_gbparse"}
| |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | my %aopts = map {exists($opts{"a$_"}) ? ($_=>$opts{"a$_"}) : qw()} (qw(query date slice), @{$opts{_abkeys}//[]}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"_abkeys"} // []
MC/DC0%0/1
$opts{"_abkeys"} // []
$opts{"_abkeys"}
Truth table0%0/2
A
A= $opts{"_abkeys"}
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my %bopts = map {exists($opts{"b$_"}) ? ($_=>$opts{"b$_"}) : qw()} (qw(query date slice), @{$opts{_abkeys}//[]}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"_abkeys"} // []
MC/DC0%0/1
$opts{"_abkeys"} // []
$opts{"_abkeys"}
Truth table0%0/2
A
A= $opts{"_abkeys"}
| |||||||||||||||||||||||||||||
| 267 | 0 | my %popts = (kbest=>-1,cutoff=>'',global=>0,strings=>0,packed=>1,fill=>1, groupby=>$groupby); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | ##-- get profiles to compare | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | my $mpa = $reldb->profile($coldb,%opts, %aopts,%popts) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my $mpb = $reldb->profile($coldb,%opts, %bopts,%popts) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | ##-- alignment and trimming | ||||||||||||||||||||||||||||
| 274 | 0 | ▶ | $reldb->vlog($logProfile, "compare(): align and trim (".($opts{global} ? 'global' : 'local').")"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | my $ppairs = DiaColloDB::Profile::MultiDiff->align($mpa,$mpb); | |||||||||||||||||||||||||||
| 276 | 0 | DiaColloDB::Profile::MultiDiff->trimPairs($ppairs, %opts); | |||||||||||||||||||||||||||
| 277 | 0 | my $diff = DiaColloDB::Profile::MultiDiff->new($mpa,$mpb, titles=>$mpa->{titles}, diff=>$opts{diff}); | |||||||||||||||||||||||||||
| 278 | 0 | ▶ | $diff->trim( DiaColloDB::Profile::Diff->diffkbest($opts{diff})=>$opts{kbest} ) if (!$opts{global}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | ##-- finalize: stringify | ||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if (!$opts{packed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if ($opts{strings}//1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$opts{"strings"} // 1
MC/DC0%0/1
$opts{"strings"} // 1
$opts{"strings"}
Truth table0%0/2
A
A= $opts{"strings"}
| |||||||||||||||||||||||||||||
| 283 | 0 | $diff->stringify($groupby->{g2s}); | |||||||||||||||||||||||||||
| 284 | } else { | ||||||||||||||||||||||||||||
| 285 | 0 | $diff->stringify($groupby->{g2txt}); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | return $diff; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | ## $mpdiff = $rel->diff($coldb, %opts) | ||||||||||||||||||||||||||||
| 293 | ## + alias for compare() | ||||||||||||||||||||||||||||
| 294 | 0 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 295 | 0 | my $rel = shift; | |||||||||||||||||||||||||||
| 296 | 0 | return $rel->compare(@_); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | ##============================================================================== | ||||||||||||||||||||||||||||
| 301 | ## Relation API: default: subprofile1() | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | ## $prf = $rel->subprofile1(\@xids, %opts) | ||||||||||||||||||||||||||||
| 304 | ## + get joint frequency profile for @xids (db must be opened) | ||||||||||||||||||||||||||||
| 305 | ## + %opts: | ||||||||||||||||||||||||||||
| 306 | ## groupby => \&gbsub, ##-- key-extractor $key2_or_undef = $gbsub->($i2) | ||||||||||||||||||||||||||||
| 307 | ## coldb => $coldb, ##-- parent DiaColloDB object (for shared data, debugging) | ||||||||||||||||||||||||||||
| 308 | ## opts => \%opts, ##-- pass-through for options to top-level profile() method | ||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub subprofile1 { | ||||||||||||||||||||||||||
|
Subroutine
subprofile1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 0 | my ($rel,$ids,%opts) = @_; | |||||||||||||||||||||||||||
| 311 | 0 | $rel->logconfess("subprofile(): abstract method called"); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | ## \%slice2prf = $rel->subprofile2(\%slice2prf, %opts) | ||||||||||||||||||||||||||||
| 315 | ## + populate f2 frequencies for profiles in \%slice2prf | ||||||||||||||||||||||||||||
| 316 | ## + %opts: | ||||||||||||||||||||||||||||
| 317 | ## groupby => \%gbreq, ##-- parsed groupby object | ||||||||||||||||||||||||||||
| 318 | ## a2data => \%a2data, ##-- maps indexed attributes to associated datastructures | ||||||||||||||||||||||||||||
| 319 | ## coldb => $coldb, ##-- parent DiaColloDB object (for shared data, debugging) | ||||||||||||||||||||||||||||
| 320 | ## opts => \%opts, ##-- pass-through for options to top-level profile() method | ||||||||||||||||||||||||||||
| 321 | ## + default implementation just returns \%slice2prf | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub subprofile2 { | ||||||||||||||||||||||||||
|
Subroutine
subprofile2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 323 | #my ($rel,$slice2prf,%opts) = @_; | ||||||||||||||||||||||||||||
| 324 | 0 | return $_[1]; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | ##============================================================================== | ||||||||||||||||||||||||||||
| 328 | ## Relation API: default: qinfo() | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | ## \%qinfo = $rel->qinfo($coldb, %opts) | ||||||||||||||||||||||||||||
| 331 | ## + get query-info hash for profile administrivia (ddc hit links) | ||||||||||||||||||||||||||||
| 332 | ## + %opts: as for profile(), additionally: | ||||||||||||||||||||||||||||
| 333 | ## ( | ||||||||||||||||||||||||||||
| 334 | ## qreqs => \@areqs, ##-- as returned by $coldb->parseRequest($opts{query}) | ||||||||||||||||||||||||||||
| 335 | ## gbreq => \%groupby, ##-- as returned by $coldb->groupby($opts{groupby}) | ||||||||||||||||||||||||||||
| 336 | ## ) | ||||||||||||||||||||||||||||
| 337 | ## + returned hash \%qinfo should have keys: | ||||||||||||||||||||||||||||
| 338 | ## ( | ||||||||||||||||||||||||||||
| 339 | ## fcoef => $fcoef, ##-- frequency coefficient (2*$coldb->{dmax} for CoFreqs) | ||||||||||||||||||||||||||||
| 340 | ## qtemplate => $qtemplate, ##-- query template with __W1.I1__ rsp __W2.I2__ replacing groupby fields | ||||||||||||||||||||||||||||
| 341 | ## ) | ||||||||||||||||||||||||||||
| 342 | 0 | ▶ | sub qinfo { | ||||||||||||||||||||||||||
|
Subroutine
qinfo: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | 0 | my ($rel,$coldb,%opts) = @_; | |||||||||||||||||||||||||||
| 344 | 0 | $rel->logconfess("qinfo(): abstract method called"); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | ## (\@q1strs,\@q2strs,\@qxstrs,\@fstrs) = $rel->qinfoData($coldb,%opts) | ||||||||||||||||||||||||||||
| 348 | ## + parses @opts{qw(qreqs gbreq)} into conditions on w1, w2 and metadata filters (for ddc linkup) | ||||||||||||||||||||||||||||
| 349 | ## + call this from subclass qinfo() methods | ||||||||||||||||||||||||||||
| 350 | 0 | ▶ | sub qinfoData { | ||||||||||||||||||||||||||
|
Subroutine
qinfoData: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | 0 | my ($rel,$coldb,%opts) = @_; | |||||||||||||||||||||||||||
| 352 | 0 | my (@q1strs,@q2strs,@qxstrs,@fstrs,$q,$q2); | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | ##-- query clause | ||||||||||||||||||||||||||||
| 355 | 0 | foreach (@{$opts{qreqs}}) { | |||||||||||||||||||||||||||
| 356 | 0 | $q = $coldb->attrQuery(@$_); | |||||||||||||||||||||||||||
| 357 | 0 | ▶ | if (UNIVERSAL::isa($q,'DDC::Any::CQFilter')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 358 | 0 | push(@fstrs, $q->toString); | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | ▶ | elsif (defined($q) && !UNIVERSAL::isa($q,'DDC::Any::CQTokAny')) { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $q and not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
MC/DC0%0/2
defined $q and not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
defined $q
not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
Truth table0%0/3
A and not(B)
A= defined $qB= not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
| |||||||||||||||||||||||||||||
| 361 | 0 | push(@q1strs, $q->toString); | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | ##-- groupby clause | ||||||||||||||||||||||||||||
| 366 | 0 | my $xi=1; | |||||||||||||||||||||||||||
| 367 | 0 | foreach (@{$opts{gbreq}{areqs}}) { | |||||||||||||||||||||||||||
| 368 | 0 | ▶ | if ($_->[0] =~ /^doc\.(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | push(@fstrs, DDC::Any::CQFHasField->new($1,"__W2.${xi}__")->toString); | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | else { | ||||||||||||||||||||||||||||
| 372 | 0 | push(@q2strs, DDC::Any::CQTokExact->new($_->[0],"__W2.${xi}__")->toString); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | 0 | ++$xi; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | ##-- common restrictions (trunk/2015-10-28: these are too expensive for large corpora (timeouts): ignore 'em | ||||||||||||||||||||||||||||
| 378 | #push(@qxstrs, qq(\$p=/$coldb->{pgood}/)) if ($coldb->{pgood}); | ||||||||||||||||||||||||||||
| 379 | #push(@qxstrs, qq(\$=!/$coldb->{pbad}/)) if ($coldb->{pbad}); | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | ##-- utf8 | ||||||||||||||||||||||||||||
| 382 | 0 | foreach (@q1strs,@q2strs,@qxstrs,@fstrs) { | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | utf8::decode($_) if (!utf8::is_utf8($_)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 0 | return (\@q1strs,\@q2strs,\@qxstrs,\@fstrs); | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | ##============================================================================== | ||||||||||||||||||||||||||||
| 391 | ## Footer | ||||||||||||||||||||||||||||
| 392 | 1 | 1; | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | __END__ | ||||||||||||||||||||||||||||