| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||||
| 2 | ## File: DiaColloDB::Corpus::Filters.pm | ||||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||
| 4 | ## Description: collocation db, source corpus content filters | ||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | package DiaColloDB::Corpus::Filters; | ||||||||||||||||||||||
| 7 | 1 | ▶ | use DiaColloDB::Persistent; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 1 | ▶ | use Exporter; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | ##============================================================================== | ||||||||||||||||||||||
| 12 | ## Administrivia | ||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 1 | our @ISA = qw(Exporter DiaColloDB::Persistent); | |||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 1 | our @NAMES = qw(pgood pbad wgood wbad lgood lbad); | |||||||||||||||||||||
| 17 | 1 | our @FILES = map {$_."file"} @NAMES; | |||||||||||||||||||||
| 18 | our %EXPORT_TAGS = | ||||||||||||||||||||||
| 19 | ( | ||||||||||||||||||||||
| 20 | 'names' => [qw(@NAMES)], | ||||||||||||||||||||||
| 21 | 1 | 'defaults' => [map {uc($_)."_DEFAULT"} @NAMES], | |||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||
| 23 | 1 | $EXPORT_TAGS{all} = [@{$EXPORT_TAGS{names}},@{$EXPORT_TAGS{defaults}}]; | |||||||||||||||||||||
| 24 | 1 | our @EXPORT_OK = @{$EXPORT_TAGS{all}}; | |||||||||||||||||||||
| 25 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | ##============================================================================== | ||||||||||||||||||||||
| 28 | ## Defaults (formerly in DiaColloDB.pm) | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | ## $PGOOD_DEFAULT | ||||||||||||||||||||||
| 31 | ## + default positive pos regex for document parsing | ||||||||||||||||||||||
| 32 | ## + don't use qr// here, since Storable doesn't like pre-compiled Regexps | ||||||||||||||||||||||
| 33 | 1 | our $PGOOD_DEFAULT = q/^(?:N|TRUNC|VV|ADJ)/; #ITJ|FM|XY | |||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | ## $PBAD_DEFAULT | ||||||||||||||||||||||
| 36 | ## + default negative pos regex for document parsing | ||||||||||||||||||||||
| 37 | 1 | our $PBAD_DEFAULT = undef; | |||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | ## $WGOOD_DEFAULT | ||||||||||||||||||||||
| 40 | ## + default positive word regex for document parsing | ||||||||||||||||||||||
| 41 | 1 | our $WGOOD_DEFAULT = q/[[:alpha:]]/; | |||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | ## $WBAD_DEFAULT | ||||||||||||||||||||||
| 44 | ## + default negative word regex for document parsing | ||||||||||||||||||||||
| 45 | 1 | our $WBAD_DEFAULT = q/[\.]/; | |||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | ## $LGOOD_DEFAULT | ||||||||||||||||||||||
| 48 | ## + default positive lemma regex for document parsing | ||||||||||||||||||||||
| 49 | 1 | our $LGOOD_DEFAULT = undef; | |||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | ## $LBAD_DEFAULT | ||||||||||||||||||||||
| 52 | ## + default negative lemma regex for document parsing | ||||||||||||||||||||||
| 53 | 1 | our $LBAD_DEFAULT = undef; | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | ##============================================================================== | ||||||||||||||||||||||
| 56 | ## Methods | ||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | ## $filters = CLASS_OR_OBJECT->new(%opts) | ||||||||||||||||||||||
| 59 | ## + simple HASH-ref wrapping filters | ||||||||||||||||||||||
| 60 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 61 | 0 | my $that = shift; | |||||||||||||||||||||
| 62 | my $filters = bless({ | ||||||||||||||||||||||
| 63 | pgood => $PGOOD_DEFAULT, | ||||||||||||||||||||||
| 64 | pbad => $PBAD_DEFAULT, | ||||||||||||||||||||||
| 65 | wgood => $WGOOD_DEFAULT, | ||||||||||||||||||||||
| 66 | wbad => $WBAD_DEFAULT, | ||||||||||||||||||||||
| 67 | lgood => $LGOOD_DEFAULT, | ||||||||||||||||||||||
| 68 | lbad => $LBAD_DEFAULT, | ||||||||||||||||||||||
| 69 | 0 | ▶ | (map {($_=>undef)} @FILES), | ||||||||||||||||||||
|
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
| |||||||||||||||||||||||
| 70 | @_, | ||||||||||||||||||||||
| 71 | }, ref($that)||$that); | ||||||||||||||||||||||
| 72 | 0 | return $filters; | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | ## $filters = $CLASS_OR_OBJECT->null() | ||||||||||||||||||||||
| 76 | 0 | ▶ | sub null { | ||||||||||||||||||||
|
Subroutine
null: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 77 | 0 | my $that = shift; | |||||||||||||||||||||
| 78 | 0 | ▶ | return bless({},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
| |||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | ## $filters = $filters->clear() | ||||||||||||||||||||||
| 82 | 0 | ▶ | sub clear { | ||||||||||||||||||||
|
Subroutine
clear: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 83 | 0 | my $filters = shift; | |||||||||||||||||||||
| 84 | 0 | $_ = undef foreach (values %$filters); | |||||||||||||||||||||
| 85 | 0 | return $filters; | |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | ## $bool = $filters->empty() | ||||||||||||||||||||||
| 89 | ## + returns true iff all filters are undefined | ||||||||||||||||||||||
| 90 | 0 | ▶ | sub isnull { | ||||||||||||||||||||
|
Subroutine
isnull: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 91 | 0 | return !grep {$_} @{$_[0]}{@NAMES,@FILES}; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | ## $bool = $filters1->equal($filters2) | ||||||||||||||||||||||
| 95 | ## $bool = PACKAGE->equal($filters1,$filters2) | ||||||||||||||||||||||
| 96 | ## + returns true iff filters are equal | ||||||||||||||||||||||
| 97 | 0 | ▶ | sub equal { | ||||||||||||||||||||
|
Subroutine
equal: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||
| 98 | 0 | my $that = shift; | |||||||||||||||||||||
| 99 | 0 | ▶ | my ($f1,$f2) = map {($_//{})} (@_ > 1 ? @_ : ($that,shift)); | ||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_ // {}
MC/DC0%0/1
$_ // {}
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||
| 100 | 0 | ▶ | return !grep {($f1->{$_}//'') ne ($f2->{$_}//'')} @NAMES,@FILES; | ||||||||||||||||||||
|
Condition0%0/4
$f1->{$_} // ""
$f2->{$_} // ""
MC/DC0%0/1
$f1->{$_} // ""
$f1->{$_}
MC/DC0%0/1
$f2->{$_} // ""
$f2->{$_}
Truth table0%0/2
A
A= $f1->{$_}
Truth table0%0/2
A
A= $f2->{$_}
| |||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | ## \%name2obj = $filters->compile() | ||||||||||||||||||||||
| 104 | ## \%name2obj = PACKAGE->compile(\%filters) | ||||||||||||||||||||||
| 105 | ## + returns HASH-ref of compiled filter regexes and (stop|go)-hashes | ||||||||||||||||||||||
| 106 | ## ${NAME} => $REGEX, | ||||||||||||||||||||||
| 107 | ## ${NAME}file => \%HASHREF, | ||||||||||||||||||||||
| 108 | 0 | ▶ | sub compile { | ||||||||||||||||||||
|
Subroutine
compile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 109 | 0 | my $that = shift; | |||||||||||||||||||||
| 110 | 0 | ▶ | my $filters = @_ ? shift : $that; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 111 | return { | ||||||||||||||||||||||
| 112 | ##-- compile: filter regexes | ||||||||||||||||||||||
| 113 | 0 | (map {($_=>qr{$filters->{$_}})} grep {$filters->{$_}} @NAMES), | |||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | ##-- compile: filter list-files | ||||||||||||||||||||||
| 116 | 0 | (map {($_=>$that->loadListFile($filters->{$_}))} grep {$filters->{$_}} @FILES), | |||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | ## \%line2undef = $coldb->loadListFile($filename_or_undef) | ||||||||||||||||||||||
| 121 | 0 | ▶ | sub loadListFile { | ||||||||||||||||||||
|
Subroutine
loadListFile: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||
| 122 | 0 | my ($that,$file) = @_; | |||||||||||||||||||||
| 123 | 0 | ▶ | return undef if (($file//'') eq ''); | ||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$file // ""
MC/DC0%0/1
$file // ""
$file
Truth table0%0/2
A
A= $file
| |||||||||||||||||||||||
| 124 | 0 | ▶ | CORE::open(my $fh,"<$file") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 125 | or $that->logconfess("loadListFile(): open failed for '$file': $!"); | ||||||||||||||||||||||
| 126 | 0 | my $h = {}; | |||||||||||||||||||||
| 127 | 0 | ▶ | while (defined($_=<$fh>)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 128 | 0 | chomp; | |||||||||||||||||||||
| 129 | 0 | ▶ | next if (/^\s*(?:\#.*)$/); ##-- skip comments and blank lines | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 130 | 0 | $h->{$_} = undef; | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | 0 | CORE::close($file); | |||||||||||||||||||||
| 133 | 0 | return $h; | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | ##============================================================================== | ||||||||||||||||||||||
| 138 | ## Footer | ||||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||