DiaColloDB/Document/DDCTabs.pm

filter: Statementstmt 15.2% 11 / 72 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 50.0% 3 / 6 Podpod 66.6% 2 / 3 totaltotal 7.3% 16 / 217 CC 47 SCAR 76.2
CC
47
Cov
6%
fromFile
2256.0
new
2.0
extension
2.0
CRAP 2039.2
« DiaColloDB/methods/compile.pm ↑ Summary DiaColloDB/Relation/DDC.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Document::DDCTabs.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, source document, DDC tab-dump
5  
6 package DiaColloDB::Document::DDCTabs;
7 1 ▶use DiaColloDB::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ##==============================================================================
12 ## Globals & Constants
13  
14 1 our @ISA = qw(DiaColloDB::Document);
15  
16 ##==============================================================================
17 ## Constructors etc.
18  
19 ## $doc = CLASS_OR_OBJECT->new(%args)
20 ## + %args, object structure:
21 ## (
22 ## ##-- parsing options
23 ## eosre => $re, ##-- EOS regex (empty or undef for file-breaks only; default='^$')
24 ## utf8 => $bool, ##-- enable utf8 parsing? (default=1)
25 ## trimPND => $bool, ##-- create trimmed "pnd" meta-attribute? (default=1)
26 ## trimAuthor => $bool, ##-- trim "author" meta-attribute (eliminate DTA PNDs)? (default=1)
27 ## trimGenre => $bool, ##-- create trimmed "genre" meta-attribute? (default=1)
28 ## foreign => $bool, ##-- alias for trimAuthor=0 trimPND=0 trimGenre=0
29 ## ##
30 ## ##-- document data
31 ## date =>$date, ##-- year
32 ## wf =>$iw, ##-- index-field for $word attribute (default=0)
33 ## pf =>$ip, ##-- index-field for $pos attribute (default=1)
34 ## lf =>$il, ##-- index-field for $lemma attribute (default=2)
35 ## pagef =>$ipage, ##-- index-field for $page attribute (default=undef:none)
36 ## tokens =>\@tokens, ##-- tokens, including undef for EOS
37 ## meta =>\%meta, ##-- document metadata (e.g. author, title, collection, ...)
38 ## ## + may also generate special $meta->{genre} as 1st component of $meta->{textClass} if available
39 ## )
40 ## + each token in @tokens is a HASH-ref {w=>$word,p=>$pos,l=>$lemma,...}
41 ## + default attribute positions ($iw,$ip,$il,$ipage) are overridden doc lines '%%$DDC:index[INDEX]=LONGNAME w' etc if present
42 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
43 0 my $that = shift;
44 0 my $doc = $that->SUPER::new(
45 utf8=>1,
46 trimPND=>1,
47 trimAuthor=>1,
48 trimGenre=>1,
49 eosre=>qr{^$},
50 wf=>0,
51 pf=>1,
52 lf=>2,
53 pagef=>undef,
54 @_, ##-- user arguments
55 );
56 0 return $doc;
57 }
58  
59 ##==============================================================================
60 ## API: I/O
61  
62 ## $ext = $doc->extension()
63 ## + default extension, for Corpus::Compiled
64 0 ▶sub extension {
Subroutine extension: not called CC=1 SCAR=6.9
Pod undocumented
65 0 return '.tabs';
66 }
67  
68 ##--------------------------------------------------------------
69 ## API: I/O: parse
70  
71 ## $bool = $doc->fromFile($filename_or_fh, %opts)
72 ## + parse tokens from $filename_or_fh
73 ## + %opts : clobbers %$doc
74 0 ▶sub fromFile {
Subroutine fromFile: not called CC=47 SCAR=77.2
Pod documented
75 0 my ($doc,$file,%opts) = @_;
76 0 ▶ $doc = $doc->new() if (!ref($doc));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc 0 0 0
77 0 @$doc{keys %opts} = values %opts;
78 0 ▶ $doc->{label} = ref($file) ? "$file" : $file;
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
79 0 ▶ my $fh = ref($file) ? $file : IO::File->new("<$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
80 0 ▶ $doc->logconfess("fromFile(): cannot open file '$file': $!") if (!ref($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh 0 0 0
81 0 ▶ binmode($fh,':utf8') if ($doc->{utf8});
Branch: 1 branch
BranchTrueFalseTotal
if $doc->{"utf8"} 0 0 0
82  
83 0 ▶ my ($wf,$pf,$lf,$pagef) = map {($_//-1)} @$doc{qw(wf pf lf pagef)};
Condition0%0/2
$_ // -1
l!l
00
MC/DC0%0/1
$_ // -1
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
84 0 my $tokens = $doc->{tokens};
85 0 @$tokens = qw();
86 0 my $meta = $doc->{meta};
87 0 %$meta = qw();
88 0 my $eos = undef;
89 0 my $eosre = $doc->{eosre};
90 0 ▶ $eosre = qr{$eosre} if ($eosre && !ref($eosre));
Branch: 1 branch
BranchTrueFalseTotal
if $eosre and not ref $eosre 0 0 0
Condition0%0/3
$eosre and not ref $eosre
!ll&&!rl&&r
000
MC/DC0%0/2
$eosre and not ref $eosre
$eosre not ref $eosre
Truth table0%0/3
A and not(B)
A= $eosreB= not ref $eosre
ABresult
0X0
100
111
91 0 my $last_was_eos = 1;
92 0 my $is_eos = 0;
93 0 my $curpage = '';
94 0 my ($w,$p,$l,$page);
95 0 ▶ while (defined($_=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
96 0 chomp;
97 0 ▶ if (/^%%/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^%%/) { } 0 0 0
elsif ($eosre and $_ =~ /$eosre/) { } 0 0 0
98 0 ▶ if (/^%%(?:\$DDC:meta\.date_|\$?date)=([0-9]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%%(?:\$DDC:meta\.date_|\$?date)=([0-9]+)/) 0 0 0
99 0 $doc->{date} = $1;
100 }
101 0 ▶ if (/^%%\$DDC:meta\.([^=]+)=(.*)$/) {
Branch: 7 branches
BranchTrueFalseTotal
if (/^%%\$DDC:meta\.([^=]+)=(.*)$/) { } 0 0 0
elsif (/^%%\$DDC:index\[([0-9]+)\]=Token\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/) { } 0 0 0
elsif (/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/) { } 0 0 0
elsif (/^%%\$DDC:index\[([0-9]+)\]=Lemma\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/) { } 0 0 0
elsif (/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/) { } 0 0 0
elsif (/^%%\$DDC:BREAK.([^=\[\]]+)/) { } 0 0 0
elsif (/^%%\$DDC:PAGE=/) { } 0 0 0
102 0 $meta->{$1} = $2;
103 }
104 ▶ elsif (/^%%\$DDC:index\[([0-9]+)\]=Token\b/ || /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/) {
Condition0%0/3
/^%%\$DDC:index\[([0-9]+)\]=Token\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%\$DDC:index\[([0-9]+)\]=Token\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/
/^%%\$DDC:index\[([0-9]+)\]=Token\b/ /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/
Truth table0%0/3
A or B
A= /^%%\$DDC:index\[([0-9]+)\]=Token\b/B= /^%%\$DDC:index\[([0-9]+)\]=\S+ w$/
ABresult
1X1
011
000
105 0 $wf = $doc->{wf} = $1;
106 }
107 ▶ elsif (/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ || /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/) {
Condition0%0/3
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/
Truth table0%0/3
A or B
A= /^%%\$DDC:index\[([0-9]+)\]=Pos\b/B= /^%%\$DDC:index\[([0-9]+)\]=\S+ p$/
ABresult
1X1
011
000
108 0 $pf = $doc->{pf} = $1;
109 }
110 ▶ elsif (/^%%\$DDC:index\[([0-9]+)\]=Lemma\b/ || /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/) {
Condition0%0/3
/^%%\$DDC:index\[([0-9]+)\]=Lemma\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%\$DDC:index\[([0-9]+)\]=Lemma\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/
/^%%\$DDC:index\[([0-9]+)\]=Lemma\b/ /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/
Truth table0%0/3
A or B
A= /^%%\$DDC:index\[([0-9]+)\]=Lemma\b/B= /^%%\$DDC:index\[([0-9]+)\]=\S+ l$/
ABresult
1X1
011
000
111 0 $lf = $doc->{lf} = $1;
112 }
113 ▶ elsif (/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ || /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/) {
Condition0%0/3
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ or /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/
/^%%\$DDC:index\[([0-9]+)\]=Pos\b/ /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/
Truth table0%0/3
A or B
A= /^%%\$DDC:index\[([0-9]+)\]=Pos\b/B= /^%%\$DDC:index\[([0-9]+)\]=\S+ page$/
ABresult
1X1
011
000
114 0 $pagef = $doc->{pagef} = $1;
115 }
116 elsif (/^%%\$DDC:BREAK.([^=\[\]]+)/) {
117 0 push(@$tokens,"#$1");
118 }
119 elsif (/^%%\$DDC:PAGE=/) {
120 0 push(@$tokens,"#page");
121 }
122 0 ▶ if ($eosre && $_ =~ $eosre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eosre and $_ =~ /$eosre/) 0 0 0
Condition0%0/3
$eosre and $_ =~ /$eosre/
!ll&&!rl&&r
000
MC/DC0%0/2
$eosre and $_ =~ /$eosre/
$eosre $_ =~ /$eosre/
Truth table0%0/3
A and B
A= $eosreB= $_ =~ /$eosre/
ABresult
0X0
100
111
123 0 ▶ push(@$tokens,$eos) if (!$last_was_eos);
Branch: 1 branch
BranchTrueFalseTotal
unless $last_was_eos 0 0 0
124 0 $last_was_eos = 1;
125 }
126 0 next;
127 }
128 ▶ elsif ($eosre && $_ =~ $eosre) {
Condition0%0/3
$eosre and $_ =~ /$eosre/
!ll&&!rl&&r
000
MC/DC0%0/2
$eosre and $_ =~ /$eosre/
$eosre $_ =~ /$eosre/
Truth table0%0/3
A and B
A= $eosreB= $_ =~ /$eosre/
ABresult
0X0
100
111
129 0 ▶ push(@$tokens,$eos) if (!$last_was_eos);
Branch: 1 branch
BranchTrueFalseTotal
unless $last_was_eos 0 0 0
130 0 $last_was_eos = 1;
131 0 next;
132 }
133 0 ($w,$p,$l,$page) = (split(/\t/,$_))[$wf,$pf,$lf,$pagef];
134  
135 ##-- honor dta-style $page index
136 0 ▶ if ($pagef > 0 && $page ne $curpage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagef > 0 and $page ne $curpage) 0 0 0
Condition0%0/3
$pagef > 0 and $page ne $curpage
!ll&&!rl&&r
000
MC/DC0%0/2
$pagef > 0 and $page ne $curpage
$pagef > 0 $page ne $curpage
Truth table0%0/3
A and B
A= $pagef > 0B= $page ne $curpage
ABresult
0X0
100
111
137 0 push(@$tokens, "#page");
138 0 $curpage = $page;
139 }
140  
141 ##-- add token
142 0 ▶ push(@$tokens, {w=>($w//''), p=>($p//''), l=>($l//'')});
Condition0%0/6
$w // ""
l!l
00
$p // ""
l!l
00
$l // ""
l!l
00
MC/DC0%0/1
$w // ""
$w
MC/DC0%0/1
$p // ""
$p
MC/DC0%0/1
$l // ""
$l
Truth table0%0/2
A
A= $w
Aresult
11
00
Truth table0%0/2
A
A= $p
Aresult
11
00
Truth table0%0/2
A
A= $l
Aresult
11
00
143 0 $last_was_eos = 0;
144 }
145 0 ▶ push(@$tokens,$eos) if (!$last_was_eos);
Branch: 1 branch
BranchTrueFalseTotal
unless $last_was_eos 0 0 0
146  
147 0 ▶ if (!$doc->{foreign}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc->{"foreign"}) 0 0 0
148 ##-- hack: compute top-level $meta->{genre} from $meta->{textClass} if requested
149 0 ▶ $meta->{genre} //= $meta->{textClass};
Condition0%0/3
$meta->{"genre"} //= $meta->{"textClass"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->{"genre"} //= $meta->{"textClass"}
$meta->{"genre"} $meta->{"textClass"}
Truth table0%0/3
A //= B
A= $meta->{"genre"}B= $meta->{"textClass"}
ABresult
1X1
011
000
150 $meta->{genre} =~ s/\:.*$//
151 0 ▶ if ($doc->{trimGenre} && defined($meta->{genre}));
Branch: 1 branch
BranchTrueFalseTotal
if $doc->{"trimGenre"} and defined $meta->{"genre"} 0 0 0
Condition0%0/3
$doc->{"trimGenre"} and defined $meta->{"genre"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doc->{"trimGenre"} and defined $meta->{"genre"}
$doc->{"trimGenre"} defined $meta->{"genre"}
Truth table0%0/3
A and B
A= $doc->{"trimGenre"}B= defined $meta->{"genre"}
ABresult
0X0
100
111
152  
153 ##-- hack: compute/trim top-level $meta->{pnd} if requested
154 0 ▶ $meta->{pnd} //= $meta->{author};
Condition0%0/3
$meta->{"pnd"} //= $meta->{"author"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->{"pnd"} //= $meta->{"author"}
$meta->{"pnd"} $meta->{"author"}
Truth table0%0/3
A //= B
A= $meta->{"pnd"}B= $meta->{"author"}
ABresult
1X1
011
000
155 0 ▶ if ($doc->{trimPND} && defined($meta->{pnd})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{"trimPND"} and defined $meta->{"pnd"}) 0 0 0
Condition0%0/3
$doc->{"trimPND"} and defined $meta->{"pnd"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doc->{"trimPND"} and defined $meta->{"pnd"}
$doc->{"trimPND"} defined $meta->{"pnd"}
Truth table0%0/3
A and B
A= $doc->{"trimPND"}B= defined $meta->{"pnd"}
ABresult
0X0
100
111
156 0 $meta->{pnd} = join(' ', ($meta->{pnd} =~ m/\#[0-9a-zA-Z]+/g));
157 0 ▶ delete($meta->{pnd}) if (($meta->{pnd}//'') eq '');
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"pnd"} // "") eq "" 0 0 0
Condition0%0/2
$meta->{"pnd"} // ""
l!l
00
MC/DC0%0/1
$meta->{"pnd"} // ""
$meta->{"pnd"}
Truth table0%0/2
A
A= $meta->{"pnd"}
Aresult
11
00
158 }
159  
160 ##-- hack: trim top-level $meta->{author} if requested
161 $meta->{author} =~ s/\s*\([^\)]*\)$//
162 0 ▶ if ($doc->{trimAuthor} && defined($meta->{author}));
Branch: 1 branch
BranchTrueFalseTotal
if $doc->{"trimAuthor"} and defined $meta->{"author"} 0 0 0
Condition0%0/3
$doc->{"trimAuthor"} and defined $meta->{"author"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doc->{"trimAuthor"} and defined $meta->{"author"}
$doc->{"trimAuthor"} defined $meta->{"author"}
Truth table0%0/3
A and B
A= $doc->{"trimAuthor"}B= defined $meta->{"author"}
ABresult
0X0
100
111
163 }
164  
165 0 ▶ $fh->close() if (!ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
166 0 return $doc;
167 }
168  
169 ##==============================================================================
170 ## Footer
171 1 1;
172  
173 __END__
« DiaColloDB/methods/compile.pm ↑ Summary DiaColloDB/Relation/DDC.pm »