| 1 | ## -*- Mode: CPerl -*- | ||||||||||
| 2 | ## File: DiaColloDB::Compat::v0_09::Relation::Unigrams.pm | ||||||||||
| 3 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||
| 4 | ## Description: collocation db, profiling relation: unigram database (using DiaColloDB::PackedFile) | ||||||||||
| 5 | |||||||||||
| 6 | package DiaColloDB::Compat::v0_09::Relation::Unigrams; | ||||||||||
| 7 | 1 | ▶ | use DiaColloDB::Compat::v0_09::Relation; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use DiaColloDB::PackedFile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use DiaColloDB::Utils qw(:sort :env :run :pack :file); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Fcntl qw(:seek); | ||||||||
|
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::PackedFile DiaColloDB::Compat::v0_09::Relation); | |||||||||
| 17 | |||||||||||
| 18 | ##============================================================================== | ||||||||||
| 19 | ## Constructors etc. | ||||||||||
| 20 | |||||||||||
| 21 | ## $ug = CLASS_OR_OBJECT->new(%args) | ||||||||||
| 22 | ## + %args, object structure: | ||||||||||
| 23 | ## ( | ||||||||||
| 24 | ## ##-- PackedFile: user options | ||||||||||
| 25 | ## file => $filename, ##-- default: undef (none) | ||||||||||
| 26 | ## flags => $flags, ##-- fcntl flags or open-mode (default='r') | ||||||||||
| 27 | ## perms => $perms, ##-- creation permissions (default=(0666 &~umask)) | ||||||||||
| 28 | ## reclen => $reclen, ##-- record-length in bytes: (default: guess from pack format if available) | ||||||||||
| 29 | ## packas => $packas, ##-- pack-format or array; see DiaColloDB::Utils::packFilterStore(); ##-- OVERRIDE default='N' | ||||||||||
| 30 | ## ## | ||||||||||
| 31 | ## ##-- PackedFile: filters | ||||||||||
| 32 | ## filter_fetch => $filter, ##-- DB_File-style filter for fetch | ||||||||||
| 33 | ## filter_store => $filter, ##-- DB_File-style filter for store | ||||||||||
| 34 | ## ## | ||||||||||
| 35 | ## ##-- PackedFile: low-level data | ||||||||||
| 36 | ## fh => $fh, ##-- underlying filehandle | ||||||||||
| 37 | ## ## | ||||||||||
| 38 | ## ##-- Unigrams: high-level data | ||||||||||
| 39 | ## N => $N, ##-- total frequency | ||||||||||
| 40 | ## ) | ||||||||||
| 41 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 42 | 0 | my $that = shift; | |||||||||
| 43 | 0 | my $ug = $that->DiaColloDB::PackedFile::new( | |||||||||
| 44 | N=>0, | ||||||||||
| 45 | packas=>'N', | ||||||||||
| 46 | @_ | ||||||||||
| 47 | ); | ||||||||||
| 48 | 0 | return $ug; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | ##============================================================================== | ||||||||||
| 52 | ## Persistent API: disk usage: INHERITED | ||||||||||
| 53 | |||||||||||
| 54 | ##============================================================================== | ||||||||||
| 55 | ## API: open/close: mostly INHERITED | ||||||||||
| 56 | |||||||||||
| 57 | ## $filename = $obj->headerFile() | ||||||||||
| 58 | ## + returns header filename; default returns "$obj->{base}.hdr" or "$obj->{dbdir}/header.json" | ||||||||||
| 59 | 0 | ▶ | sub headerFile { | ||||||||
|
Subroutine
headerFile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 60 | 0 | ▶ | return undef if (!ref($_[0])); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | ▶ | return "$_[0]{file}.hdr" if (defined($_[0]{file})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | return undef; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | ##============================================================================== | ||||||||||
| 66 | ## API: filters: INHERITED | ||||||||||
| 67 | |||||||||||
| 68 | ##============================================================================== | ||||||||||
| 69 | ## PackedFile API: positioning: INHERITED | ||||||||||
| 70 | |||||||||||
| 71 | ##============================================================================== | ||||||||||
| 72 | ## PackedFile API: record access: INHERITED | ||||||||||
| 73 | |||||||||||
| 74 | ##============================================================================== | ||||||||||
| 75 | ## I/O: text | ||||||||||
| 76 | ## + largely INHERITED from DiaColloDB::PackedFile | ||||||||||
| 77 | |||||||||||
| 78 | ## $bool = $ug->saveTextFh_v0_10($fh,%opts) | ||||||||||
| 79 | ## + save from text file in v0.10.x format: lines of the form: | ||||||||||
| 80 | ## N ##-- 1 field : N | ||||||||||
| 81 | ## FREQ ID1 DATE ##-- 3 fields: unigram frequency for (ID1,DATE) | ||||||||||
| 82 | ## + %opts: | ||||||||||
| 83 | ## i2s => \&CODE, ##-- code-ref for formatting indices; called as $s=CODE($i) | ||||||||||
| 84 | 0 | ▶ | sub saveTextFh_v0_10 { | ||||||||
|
Subroutine
saveTextFh_v0_10: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 85 | 0 | my ($ug,$outfh,%opts) = @_; | |||||||||
| 86 | 0 | ▶ | $ug->logconfess("saveTextFile(): cannot save unopened DB") if (!$ug->opened); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | $outfh->print($ug->{N}, "\n") if (defined($ug->{N})); ##-- save N line | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | my $size = $ug->size(); | |||||||||
| 90 | 0 | my $i2s = $opts{i2s}; | |||||||||
| 91 | 0 | my ($i,$val); | |||||||||
| 92 | 0 | ▶ | for ($i=0, $ug->reset(); $i < $size; ++$i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $val = $ug->get(); | |||||||||
| 94 | 0 | ▶ | $outfh->print($val, "\t", ($i2s ? $i2s->($i) : $i), "\n"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | return $ug; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | ##============================================================================== | ||||||||||
| 103 | ## PackedFile API: tie interface: INHERITED | ||||||||||
| 104 | |||||||||||
| 105 | ##============================================================================== | ||||||||||
| 106 | ## Relation API: create | ||||||||||
| 107 | |||||||||||
| 108 | ## $ug = $CLASS_OR_OBJECT->create($coldb,$tokdat_file,%opts) | ||||||||||
| 109 | ## + populates current database from $tokdat_file, | ||||||||||
| 110 | ## a tt-style text file containing 1 token-id perl line with optional blank lines | ||||||||||
| 111 | ## + %opts: clobber %$ug, also: | ||||||||||
| 112 | ## ( | ||||||||||
| 113 | ## size=>$size, ##-- set initial size | ||||||||||
| 114 | ## ) | ||||||||||
| 115 | ## + DISABLED | ||||||||||
| 116 | |||||||||||
| 117 | ##============================================================================== | ||||||||||
| 118 | ## Relation API: union | ||||||||||
| 119 | |||||||||||
| 120 | ## $ug = CLASS_OR_OBJECT->union($coldb, \@pairs, %opts) | ||||||||||
| 121 | ## + merge multiple co-frequency indices into new object | ||||||||||
| 122 | ## + @pairs : array of pairs ([$ug,\@xi2u],...) | ||||||||||
| 123 | ## of unigram-objects $ug and tuple-id maps \@xi2u for $ug | ||||||||||
| 124 | ## + %opts: clobber %$ug | ||||||||||
| 125 | ## + implicitly flushes the new index | ||||||||||
| 126 | ## + DISABLED | ||||||||||
| 127 | |||||||||||
| 128 | ##============================================================================== | ||||||||||
| 129 | ## Relation API: dbinfo | ||||||||||
| 130 | |||||||||||
| 131 | ## \%info = $rel->dbinfo($coldb) | ||||||||||
| 132 | ## + embedded info-hash for $coldb->dbinfo() | ||||||||||
| 133 | 0 | ▶ | sub dbinfo { | ||||||||
|
Subroutine
dbinfo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 134 | 0 | my $ug = shift; | |||||||||
| 135 | 0 | my $info = $ug->SUPER::dbinfo(); | |||||||||
| 136 | 0 | $info->{N} = $ug->{N}; | |||||||||
| 137 | 0 | $info->{size} = $ug->size(); | |||||||||
| 138 | 0 | return $info; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | ##============================================================================== | ||||||||||
| 143 | ## Relation API: default: profiling | ||||||||||
| 144 | |||||||||||
| 145 | ## $prf = $ug->subprofile1(\@xids, %opts) | ||||||||||
| 146 | ## + get frequency profile for @xids (db must be opened) | ||||||||||
| 147 | ## + %opts: | ||||||||||
| 148 | ## groupby => \&gbsub, ##-- key-extractor $key2_or_undef = $gbsub->($i2) | ||||||||||
| 149 | ## coldb => $coldb, ##-- for debugging | ||||||||||
| 150 | 0 | ▶ | sub subprofile1 { | ||||||||
|
Subroutine
subprofile1: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 151 | 0 | my ($ug,$ids,%opts) = @_; | |||||||||
| 152 | 0 | ▶ | $ids = [$ids] if (!UNIVERSAL::isa($ids,'ARRAY')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | |||||||||||
| 154 | 0 | my $fh = $ug->{fh}; | |||||||||
| 155 | 0 | my $packf = $ug->{packas}; | |||||||||
| 156 | 0 | my $reclen = $ug->{reclen}; | |||||||||
| 157 | 0 | my $groupby = $opts{groupby}; | |||||||||
| 158 | 0 | my $pf1 = 0; | |||||||||
| 159 | 0 | my $pf2 = {}; | |||||||||
| 160 | 0 | my ($i,$f,$key2, $buf); | |||||||||
| 161 | |||||||||||
| 162 | 0 | foreach $i (@$ids) { | |||||||||
| 163 | 0 | ▶ | CORE::seek($fh, $i*$reclen, SEEK_SET) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 0 | ▶ | CORE::read($fh, $buf, $reclen)==$reclen or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 0 | $f = unpack($packf,$buf); | |||||||||
| 166 | 0 | $pf1 += $f; | |||||||||
| 167 | 0 | ▶ | $key2 = $groupby ? $groupby->($i) : $i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 0 | ▶ | next if (!defined($key2)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 0 | $pf2->{$key2} += $f | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | return DiaColloDB::Profile->new( | ||||||||||
| 173 | N=>$ug->{N}, | ||||||||||
| 174 | 0 | f1=>$pf1, | |||||||||
| 175 | f2=>$pf2, | ||||||||||
| 176 | f12=>{ %$pf2 }, | ||||||||||
| 177 | ); | ||||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | ##============================================================================== | ||||||||||
| 181 | ## Relation API: default: query info | ||||||||||
| 182 | |||||||||||
| 183 | ## \%qinfo = $rel->qinfo($coldb, %opts) | ||||||||||
| 184 | ## + get query-info hash for profile administrivia (ddc hit links) | ||||||||||
| 185 | ## + %opts: as for profile(), additionally: | ||||||||||
| 186 | ## ( | ||||||||||
| 187 | ## qreqs => \@qreqs, ##-- as returned by $coldb->parseRequest($opts{query}) | ||||||||||
| 188 | ## gbreq => \%groupby, ##-- as returned by $coldb->groupby($opts{groupby}) | ||||||||||
| 189 | ## ) | ||||||||||
| 190 | 0 | ▶ | sub qinfo { | ||||||||
|
Subroutine
qinfo: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 191 | 0 | my ($rel,$coldb,%opts) = @_; | |||||||||
| 192 | 0 | my ($q1strs,$q2strs,$qxstrs,$fstrs) = $rel->qinfoData($coldb,%opts); | |||||||||
| 193 | |||||||||||
| 194 | 0 | my @qstrs = (@$q1strs, @$q2strs, @$qxstrs); | |||||||||
| 195 | 0 | ▶ | @qstrs = ('*') if (!@qstrs); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | ▶ | my $qstr = ('('.join(' WITH ', @qstrs).') =1' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | .' #SEPARATE' | ||||||||||
| 198 | .(@$fstrs ? (' '.join(' ',@$fstrs)) : ''), | ||||||||||
| 199 | ); | ||||||||||
| 200 | return { | ||||||||||
| 201 | 0 | fcoef => 1, | |||||||||
| 202 | qtemplate => $qstr, | ||||||||||
| 203 | }; | ||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | ##============================================================================== | ||||||||||
| 207 | ## Pacakge Alias(es) | ||||||||||
| 208 | package DiaColloDB::Compat::v0_09::Unigrams; | ||||||||||
| 209 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 210 | 1 | our @ISA = qw(DiaColloDB::Compat::v0_09::Relation::Unigrams); | |||||||||
| 211 | |||||||||||
| 212 | ##============================================================================== | ||||||||||
| 213 | ## Footer | ||||||||||
| 214 | 1 | 1; | |||||||||
| 215 | |||||||||||
| 216 | __END__ | ||||||||||