blib/lib/Biblio/Citation/Compare.pm

filter: Statementstmt 83.9% 173 / 206 Branchbran 65.5% 80 / 122 Conditioncond 65.6% 86 / 131 MC/DCmcdc 39.0% 25 / 64 Subroutinesub 86.3% 19 / 22 Podpod 15.3% 2 / 13 totaltotal 69.0% 385 / 558 CC 111 SCAR 51.7
CC
111
Cov
74%
sameWork
269.6
safe_decode
30.0
sameAuthorsLoose
12.0
CRAP 176.1
↑ Summary
1 package Biblio::Citation::Compare;
2  
3 3 ▶use 5.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Text::LevenshteinXS qw(distance);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Text::Names qw/samePerson cleanName parseName parseName2/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Text::Roman qw/isroman roman2int/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 require Exporter;
13  
14 3 our @ISA = qw(Exporter);
15  
16 3 our %EXPORT_TAGS = ( 'all' => [ qw(
17 sameWork sameAuthors toString extractEdition sameAuthorBits sameTitle sameAuthorsLoose
18 ) ] );
19  
20 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
21  
22 3 our @EXPORT = qw( );
23  
24 3 our $VERSION = '0.57';
25  
26 # to correct bogus windows entities. unfixable ones are converted to spaces.
27 3 my %WIN2UTF = (
28 hex('80')=> hex('20AC'),# #EURO SIGN
29 hex('81')=> hex('0020'), #UNDEFINED
30 hex('82')=> hex('201A'),# #SINGLE LOW-9 QUOTATION MARK
31 hex('83')=> hex('0192'),# #LATIN SMALL LETTER F WITH HOOK
32 hex('84')=> hex('201E'),# #DOUBLE LOW-9 QUOTATION MARK
33 hex('85')=> hex('2026'),# #HORIZONTAL ELLIPSIS
34 hex('86')=> hex('2020'),# #DAGGER
35 hex('87')=> hex('2021'),# #DOUBLE DAGGER
36 hex('88')=> hex('02C6'),# #MODIFIER LETTER CIRCUMFLEX ACCENT
37 hex('89')=> hex('2030'),# #PER MILLE SIGN
38 hex('8A')=> hex('0160'),# #LATIN CAPITAL LETTER S WITH CARON
39 hex('8B')=> hex('2039'),# #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
40 hex('8C')=> hex('0152'),# #LATIN CAPITAL LIGATURE OE
41 hex('8D')=> hex('0020'),# #UNDEFINED
42 hex('8E')=> hex('017D'),# #LATIN CAPITAL LETTER Z WITH CARON
43 hex('8F')=> hex('0020'),# #UNDEFINED
44 hex('90')=> hex('0020'),# #UNDEFINED
45 hex('91')=> hex('2018'),# #LEFT SINGLE QUOTATION MARK
46 hex('92')=> hex('2019'),# #RIGHT SINGLE QUOTATION MARK
47 hex('93')=> hex('201C'),# #LEFT DOUBLE QUOTATION MARK
48 hex('94')=> hex('201D'),# #RIGHT DOUBLE QUOTATION MARK
49 hex('95')=> hex('2022'),# #BULLET
50 hex('96')=> hex('2013'),# #EN DASH
51 hex('97')=> hex('2014'),# #EM DASH
52 hex('98')=> hex('02DC'),# #SMALL TILDE
53 hex('99')=> hex('2122'),# #TRADE MARK SIGN
54 hex('9A')=> hex('0161'),# #LATIN SMALL LETTER S WITH CARON
55 hex('9B')=> hex('203A'),# #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
56 hex('9C')=> hex('0153'),# #LATIN SMALL LIGATURE OE
57 hex('9D')=> hex('0020'),# #UNDEFINED
58 hex('9E')=> hex('017E'),# #LATIN SMALL LETTER Z WITH CARON
59 hex('9F')=> hex('0178')# #LATIN CAPITAL LETTER Y WITH DIAERESIS
60 );
61 3 my $PARENS = '\s*[\[\(](.+?)[\]\)]\s*';
62 3 my $QUOTE = '"“”`¨´‘’‛“”‟„′″‴‵‶‷⁗❛❜❝❞';
63 3 my @ED_RES = (
64 '(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth)',
65 '([1-9])\s?\w{2,5}\s[ée]d',
66 '\bv\.?(?:ersion)?\s?([0-9IXV]+)',
67 '\s([IXV0-9]+)(?:$|:)'
68 );
69  
70 #die "no" unless "2nd edition" =~ /$EDITION/i;
71  
72 #my $TITLE_SPLIT = '(?:\?|\:|\.|!|\"|[$QUOTE]\b)';
73 3 my $TITLE_SPLIT = '(?:\?|\:|\.|!)';
74  
75 66 ▶sub sameAuthors {
Subroutine sameAuthors: called CC=6 SCAR=17.9
Pod documented
76 66 my ($list1, $list2, %opts) = @_;
77 66 ▶ return 0 if $#$list1 != $#$list2 and $opts{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $list1->$#* != $list2->$#* and $opts{"strict"} 6 60 66
Condition100%3/3
$list1->$#* != $list2->$#* and $opts{"strict"}
!ll&&!rl&&r
5376
MC/DC100%2/2
$list1->$#* != $list2->$#* and $opts{"strict"}
$list1->$#* != $list2->$#* $opts{"strict"}
Truth table100%3/3
A and B
A= $list1->$#* != $list2->$#*B= $opts{"strict"}
ABresult
0X0
100
111
78 60 ▶ if ($#$list2 > $#$list1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list2->$#* > $list1->$#*) 6 54 60
79 6 my $t = $list1;
80 6 $list1 = $list2;
81 6 $list2 = $t;
82 }
83 60 ▶ for (my $i = 0; $i <= $#$list2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $list2->$#*) 51 56 107
84 51 ▶ return 0 unless grep { samePerson($list2->[$i],$_, %opts) } @$list1;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {samePerson($list2->[$i], $_, %opts);} @$list1 4 47 51
85 }
86 56 return 1;
87 }
88  
89 0 ▶sub firstAuthor {
Subroutine firstAuthor: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my $e = shift;
91 0 my $a = $e->{authors};
92 0 ▶ if ($#$a > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->$#* > -1) { } 0 0 0
93 0 return $a->[0];
94 } else {
95 0 return undef;
96 }
97 }
98  
99 53 ▶sub sameWork {
Subroutine sameWork: called CC=85 SCAR=56.0
Pod documented
100  
101 53 my ($e, $c, $threshold,$loose,$nolinks,%opts) = @_;
102  
103 53 ▶ my $debug = $opts{debug} || 0;
Condition50%1/2
$opts{'debug'} || 0
l!l
053
MC/DC0%0/1
$opts{'debug'} || 0
$opts{'debug'}
Truth table50%1/2
A
A= $opts{'debug'}
Aresult
11
00
104  
105 53 ▶ $loose = 0 unless defined $loose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loose 53 0 53
106 53 ▶ $threshold = 0.15 unless $threshold;
Branch: 1 branch
BranchTrueFalseTotal
unless $threshold 53 0 53
107 53 ▶ $opts{loose} = 1 if $loose;
Branch: 1 branch
BranchTrueFalseTotal
if $loose 0 53 53
108
109 53 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 53 53
110 0 warn "sameWork 1: " . toString($e);
111 0 warn "sameWork 2: " . toString($c);
112 }
113  
114 53 ▶ if (defined $e->{doi} and length $e->{doi} and defined $c->{doi} and length $c->{doi}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{"doi"} and length $e->{"doi"} and defined $c->{"doi"} and length $c->{"doi"}) 0 53 53
Condition33%3/9
defined $e->{"doi"} and length $e->{"doi"} and defined $c->{"doi"} and length $c->{"doi"}
!ll&&!rl&&r
5300
defined $e->{"doi"} and length $e->{"doi"} and defined $c->{"doi"}
!ll&&!rl&&r
5300
defined $e->{"doi"} and length $e->{"doi"}
!ll&&!rl&&r
5300
MC/DC0%0/4
defined $e->{"doi"} and length $e->{"doi"} and defined $c->{"doi"} and length $c->{"doi"}
defined $e->{"doi"} length $e->{"doi"} defined $c->{"doi"} length $c->{"doi"}
Truth table20%1/5
A and B and C and D
A= defined $e->{"doi"}B= length $e->{"doi"}C= defined $c->{"doi"}D= length $c->{"doi"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
115 0 ▶ if ($e->{doi} eq $c->{doi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{'doi'} eq $c->{'doi'}) { } 0 0 0
116 # we don't use doi to say 1 because often we have dois that are for a whole issue
117 # however same doi lowers the threshold
118 0 ▶ $threshold /= 2 if $e->{doi} eq $c->{doi};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"doi"} eq $c->{"doi"} 0 0 0
119 0 $loose = 1;
120 0 $opts{loose} = 1;
121 } else {
122 0 return 0;
123 }
124 }
125  
126 53 ▶ return 0 if (!$c);
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 53 53
127  
128 # normalize encoding of relevant fields
129 53 local $e->{title} = decodeHTMLEntities($e->{title});
130 53 local $c->{title} = decodeHTMLEntities($c->{title});
131  
132 # first check if authors,date, and title are almost literally the same
133 53 ▶ my $tsame = (lc $e->{title} eq lc $c->{title}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc $e->{'title'} eq lc $c->{'title'} ? : 7 46 53
134 53 my $asame = sameAuthors($e->{authors},$c->{authors},strict=>1);
135 53 ▶ my $asame_loose = $asame || sameAuthors($e->{authors},$c->{authors},strict=>0); #asame_loose will be 1 while same is 0 when there are extra authors in one paper but all overlap authors match
Condition100%3/3
$asame || sameAuthors($e->{'authors'}, $c->{'authors'}, 'strict', 0)
l!l&&r!l&&!r
4661
MC/DC100%2/2
$asame || sameAuthors($e->{'authors'}, $c->{'authors'}, 'strict', 0)
$asame sameAuthors($e->{'authors'}, $c->{'authors'}, 'strict', 0)
Truth table100%3/3
A || B
A= $asameB= sameAuthors($e->{'authors'}, $c->{'authors'}, 'strict', 0)
ABresult
1X1
011
000
136 53 ▶ my $asame_bits = $asame_loose || sameAuthorBits($e->{authors},$c->{authors});
Condition66%2/3
$asame_loose || sameAuthorBits($e->{'authors'}, $c->{'authors'})
l!l&&r!l&&!r
5201
MC/DC50%1/2
$asame_loose || sameAuthorBits($e->{'authors'}, $c->{'authors'})
$asame_loose sameAuthorBits($e->{'authors'}, $c->{'authors'})
Truth table66%2/3
A || B
A= $asame_looseB= sameAuthorBits($e->{'authors'}, $c->{'authors'})
ABresult
1X1
011
000
137 53 ▶ my $dsame = (defined $e->{date} and defined $c->{date} and $e->{date} eq $c->{date}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $e->{'date'} && defined $c->{'date'} && $e->{'date'} eq $c->{'date'} ? : 29 24 53
Condition100%6/6
defined $e->{"date"} and defined $c->{"date"} and $e->{"date"} eq $c->{"date"}
!ll&&!rl&&r
17729
defined $e->{"date"} and defined $c->{"date"}
!ll&&!rl&&r
16136
MC/DC100%3/3
defined $e->{"date"} and defined $c->{"date"} and $e->{"date"} eq $c->{"date"}
defined $e->{"date"} defined $c->{"date"} $e->{"date"} eq $c->{"date"}
Truth table100%4/4
A and B and C
A= defined $e->{"date"}B= defined $c->{"date"}C= $e->{"date"} eq $c->{"date"}
ABCresult
0XX0
10X0
1100
1111
138  
139 53 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 53 53
140 0 warn "tsame: $tsame";
141 0 warn "asame: $asame";
142 0 warn "asame_loose: $asame_loose";
143 0 warn "asame_bits: $asame_bits";
144 0 warn "dsame: $dsame";
145 }
146  
147 53 ▶ return 1 if ($tsame and $asame and $dsame);
Branch: 1 branch
BranchTrueFalseTotal
if $tsame and $asame and $dsame 5 48 53
Condition83%5/6
$tsame and $asame and $dsame
!ll&&!rl&&r
4805
$tsame and $asame
!ll&&!rl&&r
4625
MC/DC66%2/3
$tsame and $asame and $dsame
$tsame $asame $dsame
Truth table75%3/4
A and B and C
A= $tsameB= $asameC= $dsame
ABCresult
0XX0
10X0
1100
1111
148  
149 # if authors quite different, not same
150 48 ▶ if (!$asame_bits) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asame_bits) 1 47 48
151 1 ▶ warn "authors too different" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
152 1 return 0;
153 }
154 # at this point the authors are plausibly the same
155  
156 # check dates
157 47 my $date_wildcards = '^forthcoming|in press|manuscript|unknown|web$';
158 47 ▶ my $compat_dates = ($dsame or ($e->{date} && $e->{date} =~ /$date_wildcards/) or ($c->{date} && $c->{date} =~ /$date_wildcards/));
Condition66%8/12
$dsame || $e->{'date'} && $e->{'date'} =~ /$date_wildcards/ || $c->{'date'} && $c->{'date'} =~ /$date_wildcards/
l!l&&r!l&&!r
23024
$dsame or $e->{"date"} and $e->{"date"} =~ /$date_wildcards/
l!l&&r!l&&!r
23024
$e->{"date"} and $e->{"date"} =~ /$date_wildcards/
!ll&&!rl&&r
1680
$c->{"date"} and $c->{"date"} =~ /$date_wildcards/
!ll&&!rl&&r
1770
MC/DC20%1/5
$dsame || $e->{'date'} && $e->{'date'} =~ /$date_wildcards/ || $c->{'date'} && $c->{'date'} =~ /$date_wildcards/
$dsame $e->{"date"} $e->{"date"} =~ /$date_wildcards/ $c->{"date"} $c->{"date"} =~ /$date_wildcards/
Truth table50%4/8
A or B and C || D and E
A= $dsameB= $e->{"date"}C= $e->{"date"} =~ /$date_wildcards/D= $c->{"date"}E= $c->{"date"} =~ /$date_wildcards/
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
159 47 ▶ if (!$dsame and !$compat_dates) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dsame || $compat_dates) { } 24 23 47
Condition66%2/3
$dsame || $compat_dates
l!l&&r!l&&!r
23024
MC/DC50%1/2
$dsame || $compat_dates
$dsame $compat_dates
Truth table66%2/3
A || B
A= $dsameB= $compat_dates
ABresult
1X1
011
000
160  
161 #disabled for most cases because we want to conflate editions and republications for now.
162 24 ▶ if ($e->{title} =~ /^Introduction.?$/ or $e->{title} =~ /^Preface.?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"title"} =~ /^Introduction.?$/ or $e->{"title"} =~ /^Preface.?$/) 0 24 24
Condition33%1/3
$e->{"title"} =~ /^Introduction.?$/ or $e->{"title"} =~ /^Preface.?$/
l!l&&r!l&&!r
0024
MC/DC0%0/2
$e->{"title"} =~ /^Introduction.?$/ or $e->{"title"} =~ /^Preface.?$/
$e->{"title"} =~ /^Introduction.?$/ $e->{"title"} =~ /^Preface.?$/
Truth table33%1/3
A or B
A= $e->{"title"} =~ /^Introduction.?$/B= $e->{"title"} =~ /^Preface.?$/
ABresult
1X1
011
000
163 return 0 if ($e->{source} and $e->{source} ne $c->{source}) or
164 0 ▶ ($e->{volume} and $e->{volume} ne $c->{volume});
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"source"} and $e->{"source"} ne $c->{"source"} or $e->{"volume"} and $e->{"volume"} ne $c->{"volume"} 0 0 0
Condition0%0/9
$e->{"source"} and $e->{"source"} ne $c->{"source"} or $e->{"volume"} and $e->{"volume"} ne $c->{"volume"}
l!l&&r!l&&!r
000
$e->{"source"} and $e->{"source"} ne $c->{"source"}
!ll&&!rl&&r
000
$e->{"volume"} and $e->{"volume"} ne $c->{"volume"}
!ll&&!rl&&r
000
MC/DC0%0/4
$e->{"source"} and $e->{"source"} ne $c->{"source"} or $e->{"volume"} and $e->{"volume"} ne $c->{"volume"}
$e->{"source"} $e->{"source"} ne $c->{"source"} $e->{"volume"} $e->{"volume"} ne $c->{"volume"}
Truth table0%0/7
A and B or C and D
A= $e->{"source"}B= $e->{"source"} ne $c->{"source"}C= $e->{"volume"}D= $e->{"volume"} ne $c->{"volume"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
165 }
166  
167 # numeric dates
168 24 ▶ if ($e->{date} and $e->{date} =~ /^\d\d\d\d$/ and $c->{date} and $c->{date} =~ /^\d\d\d\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{'date'} and $e->{'date'} =~ /^\d\d\d\d$/ and $c->{'date'} and $c->{'date'} =~ /^\d\d\d\d$/) { } 7 17 24
Condition77%7/9
$e->{"date"} and $e->{"date"} =~ /^\d\d\d\d$/ and $c->{"date"} and $c->{"date"} =~ /^\d\d\d\d$/
!ll&&!rl&&r
1707
$e->{"date"} and $e->{"date"} =~ /^\d\d\d\d$/ and $c->{"date"}
!ll&&!rl&&r
1617
$e->{"date"} and $e->{"date"} =~ /^\d\d\d\d$/
!ll&&!rl&&r
1608
MC/DC50%2/4
$e->{"date"} and $e->{"date"} =~ /^\d\d\d\d$/ and $c->{"date"} and $c->{"date"} =~ /^\d\d\d\d$/
$e->{"date"} $e->{"date"} =~ /^\d\d\d\d$/ $c->{"date"} $c->{"date"} =~ /^\d\d\d\d$/
Truth table60%3/5
A and B and C and D
A= $e->{"date"}B= $e->{"date"} =~ /^\d\d\d\d$/C= $c->{"date"}D= $c->{"date"} =~ /^\d\d\d\d$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
169 7 my $date_diff = $e->{date} - $c->{date};
170 # quite often people misremember dates so we permit some slack
171 # we will consider the dates compat if they close in time
172 # if dates are far apart, we know they are not exactly the same publicatoins.
173 # but they might be reprints of the same thing, which we want to conflate.
174 7 ▶ if ($date_diff > 3 or $date_diff < -3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date_diff > 3 or $date_diff < -3) { } 3 4 7
Condition66%2/3
$date_diff > 3 or $date_diff < -3
l!l&&r!l&&!r
034
MC/DC50%1/2
$date_diff > 3 or $date_diff < -3
$date_diff > 3 $date_diff < -3
Truth table66%2/3
A or B
A= $date_diff > 3B= $date_diff < -3
ABresult
1X1
011
000
175 3 ▶ if ($asame_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asame_bits) { } 3 0 3
176 3 $threshold /= 2;
177 3 ▶ warn "dates different, lowering similarity threshold" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
178 } else {
179 0 ▶ warn "dates+authors too different" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
180 0 return 0;
181 }
182  
183 } else {
184 # nearby date
185 4 $threshold /= 2;
186 }
187  
188 } else {
189 #messed up dates, assume the worst
190 17 $threshold /=2;
191 }
192  
193 } else {
194 23 ▶ $loose = 1 if $asame_loose or $asame_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $asame_loose or $asame_bits 23 0 23
Condition33%1/3
$asame_loose or $asame_bits
l!l&&r!l&&!r
2300
MC/DC0%0/2
$asame_loose or $asame_bits
$asame_loose $asame_bits
Truth table33%1/3
A or B
A= $asame_looseB= $asame_bits
ABresult
1X1
011
000
195 }
196
197  
198  
199  
200 47 ▶ warn "pre title length" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 47 47
201 # if title very different in lengths and do not contain ":" or brackets, not the same
202 return 0 if !$tsame and (
203 abs(length($e->{title}) - length($c->{title})) > 20
204 and
205 ($e->{title} !~ /$TITLE_SPLIT/ and $c->{title} !~ /$TITLE_SPLIT/)
206 and
207 47 ▶ ($e->{title} !~ /$PARENS/ and $c->{title} !~ /$PARENS/)
Branch: 1 branch
BranchTrueFalseTotal
if not $tsame and (abs length($e->{"title"}) - length($c->{"title"}) > 20 and not $e->{"title"} =~ /$TITLE_SPLIT/ || $c->{"title"} =~ /$TITLE_SPLIT/ and not $e->{"title"} =~ /$PARENS/ || $c->{"title"} =~ /$PARENS/) 0 47 47
Condition73%11/15
not $tsame and (abs length($e->{"title"}) - length($c->{"title"}) > 20 and not $e->{"title"} =~ /$TITLE_SPLIT/ || $c->{"title"} =~ /$TITLE_SPLIT/ and not $e->{"title"} =~ /$PARENS/ || $c->{"title"} =~ /$PARENS/)
!ll&&!rl&&r
1460
abs length($e->{"title"}) - length($c->{"title"}) > 20 and not $e->{"title"} =~ /$TITLE_SPLIT/ || $c->{"title"} =~ /$TITLE_SPLIT/ and not $e->{"title"} =~ /$PARENS/ || $c->{"title"} =~ /$PARENS/
!ll&&!rl&&r
4420
abs length($e->{"title"}) - length($c->{"title"}) > 20 and not $e->{"title"} =~ /$TITLE_SPLIT/ || $c->{"title"} =~ /$TITLE_SPLIT/
!ll&&!rl&&r
31132
$e->{'title'} =~ /$TITLE_SPLIT/ || $c->{'title'} =~ /$TITLE_SPLIT/
l!l&&r!l&&!r
1212
$e->{'title'} =~ /$PARENS/ || $c->{'title'} =~ /$PARENS/
l!l&&r!l&&!r
200
MC/DC0%0/6
not $tsame and (abs length($e->{"title"}) - length($c->{"title"}) > 20 and not $e->{"title"} =~ /$TITLE_SPLIT/ || $c->{"title"} =~ /$TITLE_SPLIT/ and not $e->{"title"} =~ /$PARENS/ || $c->{"title"} =~ /$PARENS/)
not $tsame abs length($e->{"title"}) - length($c->{"title"}) > 20 $e->{'title'} =~ /$TITLE_SPLIT/ $c->{'title'} =~ /$TITLE_SPLIT/ $e->{'title'} =~ /$PARENS/ $c->{'title'} =~ /$PARENS/
Truth table71%5/7
not(A) and B and not(C || D) and not(E || F)
A= not $tsameB= abs length($e->{"title"}) - length($c->{"title"}) > 20C= $e->{'title'} =~ /$TITLE_SPLIT/D= $c->{'title'} =~ /$TITLE_SPLIT/E= $e->{'title'} =~ /$PARENS/F= $c->{'title'} =~ /$PARENS/
ABCDEFresult
0XXXXX0
10XXXX0
111XXX0
1101XX0
11001X0
1100010
1100001
208 );
209  
210 # Compare links
211 # if (!$nolinks) {
212 # foreach my $l (@{$e->{links}}) {
213 # print "Links e:\n" . join("\n",$e->getLinks);
214 # print "Links c:\n" . join("\n",$c->getLinks);
215 # return 1 if grep { $l eq $_} @{$c->{links}};
216 # }
217 # }
218  
219 47 ▶ warn "pre loose mode: loose = $loose" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 47 47
220  
221 #print "threshold $lname1,$lname2: $threshold\n";
222 # ok if distance short enough without doing anything
223 #print "distance: " . distance(lc $e->{title},lc $c->{title}) / (length($e->{title}) +1) . "\n";
224  
225 # perform fuzzy matching
226 #my $str1 = "$e->{date}|$e->{title}";
227 47 my $str1 = lc _strip_non_word($e->{title});
228 47 my $str2 = lc _strip_non_word($c->{title});
229  
230 # check for edition strings
231 47 my $ed1 = extractEdition($str1);
232 47 my $ed2 = extractEdition($str2);
233 47 ▶ warn "ed1: $ed1" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 47 47
234 47 ▶ warn "ed2: $ed2" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 47 47
235 47 ▶ $loose =1 if $ed1 and $ed2 and $ed1 == $ed2 and !$dsame and $asame_loose;
Branch: 1 branch
BranchTrueFalseTotal
if $ed1 and $ed2 and $ed1 == $ed2 and not $dsame and $asame_loose 2 45 47
Condition91%11/12
$ed1 and $ed2 and $ed1 == $ed2 and not $dsame and $asame_loose
!ll&&!rl&&r
4502
$ed1 and $ed2 and $ed1 == $ed2 and not $dsame
!ll&&!rl&&r
4412
$ed1 and $ed2 and $ed1 == $ed2
!ll&&!rl&&r
3953
$ed1 and $ed2
!ll&&!rl&&r
3818
MC/DC80%4/5
$ed1 and $ed2 and $ed1 == $ed2 and not $dsame and $asame_loose
$ed1 $ed2 $ed1 == $ed2 not $dsame $asame_loose
Truth table83%5/6
A and B and C and not(D) and E
A= $ed1B= $ed2C= $ed1 == $ed2D= not $dsameE= $asame_loose
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
236  
237 47 ▶ return 0 if ($ed1 and !$ed2) or ($ed2 and !$ed1) or ($ed1 && $ed1 != $ed2);
Branch: 1 branch
BranchTrueFalseTotal
if $ed1 and not $ed2 or $ed2 and not $ed1 or $ed1 and $ed1 != $ed2 7 40 47
Condition100%15/15
$ed1 and not $ed2 or $ed2 and not $ed1 or $ed1 and $ed1 != $ed2
l!l&&r!l&&!r
2540
$ed1 and not $ed2 or $ed2 and not $ed1
l!l&&r!l&&!r
1145
$ed1 and not $ed2
!ll&&!rl&&r
3881
$ed2 and not $ed1
!ll&&!rl&&r
3781
$ed1 and $ed1 != $ed2
!ll&&!rl&&r
3735
MC/DC66%4/6
$ed1 and not $ed2 or $ed2 and not $ed1 or $ed1 and $ed1 != $ed2
$ed1 not $ed2 $ed2 not $ed1 $ed1 $ed1 != $ed2
Truth table33%5/15
A and not(B) or C and not(D) or E and F
A= $ed1B= not $ed2C= $ed2D= not $ed1E= $ed1F= $ed1 != $ed2
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
238 40 ▶ warn "not diff editions" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 40 40
239  
240 # remove brackets
241 40 my ($parens1,$parens2);
242 40 $str1 =~ s/$PARENS//g;
243 40 $parens1 = $1;
244 40 $str2 =~ s/$PARENS//g;
245 40 $parens2 = $1;
246 40 ▶ return 0 if $parens1 && $parens2 && numdiff($parens1,$parens2);
Branch: 1 branch
BranchTrueFalseTotal
if $parens1 and $parens2 and numdiff($parens1, $parens2) 2 38 40
Condition83%5/6
$parens1 and $parens2 and numdiff($parens1, $parens2)
!ll&&!rl&&r
3082
$parens1 and $parens2
!ll&&!rl&&r
30010
MC/DC66%2/3
$parens1 and $parens2 and numdiff($parens1, $parens2)
$parens1 $parens2 numdiff($parens1, $parens2)
Truth table75%3/4
A and B and C
A= $parens1B= $parens2C= numdiff($parens1, $parens2)
ABCresult
0XX0
10X0
1100
1111
247  
248 38 ▶ warn "the text comparison is: '$str1' vs '$str2'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 38 38
249  
250 38 ▶ warn "pre number check" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 38 38
251 # if titles differ by a number, not the same
252 38 ▶ return 0 if numdiff($str1,$str2);
Branch: 1 branch
BranchTrueFalseTotal
if numdiff($str1, $str2) 5 33 38
253  
254 # ultimate test
255 #dbg("$str1\n$str2\n");
256 #dbg(my_dist_text($str1,$str2));
257 33 my $score = (my_dist_text($str1,$str2) / (length($str1) +1));
258
259 33 ▶ warn "score: $score (threshold: $threshold)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 33 33
260 #print $score . "<br>\n";
261 33 ▶ return 1 if ( $score < $threshold);
Branch: 1 branch
BranchTrueFalseTotal
if $score < $threshold 22 11 33
262  
263 # now if loose mode and only one of the titles has a ":" or other punctuation, compare the part before the punc with the other title instead
264 11 ▶ if ($loose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loose) 7 4 11
265  
266 7 ▶ warn "loose: $str1 -- $str2" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 7 7
267  
268 7 ▶ if ($e->{title} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{'title'} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) { } 5 2 7
elsif ($c->{'title'} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) { } 2 0 2
269  
270 5 my $str1 = _strip_non_word($1);
271 5 ▶ if ($c->{title} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'title'} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) { } 0 5 5
272 0 return 0;
273 } else {
274 5 ▶ if (my_dist_text($str1,$str2) / (length($str1) +1)< $threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_dist_text($str1, $str2) / (length($str1) + 1) < $threshold) 5 0 5
275 5 return 1;
276 }
277 }
278  
279 } elsif ($c->{title} =~ /(.+?)\s*$TITLE_SPLIT\s*(.+)/) {
280  
281 2 my $str2 = _strip_non_word($1);
282 2 ▶ if (my_dist_text($str1,$str2) / (length($str1) +1)< $threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_dist_text($str1, $str2) / (length($str1) + 1) < $threshold) 1 1 2
283 1 return 1;
284 }
285  
286 } else {
287  
288 0 return 0;
289  
290 }
291 }
292
293 5 return 0;
294 }
295  
296 0 ▶sub sameAuthorsLoose {
Subroutine sameAuthorsLoose: not called CC=3 SCAR=24.8
Pod undocumented
297 0 my ($a, $b) = @_;
298 0 my $asame = sameAuthors($a,$b,strict=>1);
299 0 ▶ my $asame_loose = $asame || sameAuthors($a,$b,strict=>0);
Condition0%0/3
$asame || sameAuthors($a, $b, 'strict', 0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$asame || sameAuthors($a, $b, 'strict', 0)
$asame sameAuthors($a, $b, 'strict', 0)
Truth table0%0/3
A || B
A= $asameB= sameAuthors($a, $b, 'strict', 0)
ABresult
1X1
011
000
300 0 ▶ return $asame_loose || sameAuthorBits($a,$b);
Condition0%0/3
$asame_loose || sameAuthorBits($a, $b)
l!l&&r!l&&!r
000
MC/DC0%0/2
$asame_loose || sameAuthorBits($a, $b)
$asame_loose sameAuthorBits($a, $b)
Truth table0%0/3
A || B
A= $asame_looseB= sameAuthorBits($a, $b)
ABresult
1X1
011
000
301 }
302  
303 3 ▶sub sameAuthorBits {
Subroutine sameAuthorBits: called CC=6 SCAR=17.9
Pod undocumented
304 3 my ($a, $b) = @_;
305 3 my (@alist, @blist);
306 3 for (@$a) {
307 4 my $v = lc $_; # we copy so we don't modify the original
308 4 $v =~ s/[,\.]//g;
309 #$v =~ s/(\p{Ll})(\p{Lu})/$1 $2/g;
310 4 push @alist, split(/\s+/, $v);
311 }
312 3 for (@$b) {
313 3 my $v = lc $_;
314 3 $v =~ s/[,\.]//g;
315 #$v =~ s/(\p{Ll})(\p{Lu})/$1 $2/g;
316 3 push @blist, split(/\s+/, $v);
317 }
318 #use Data::Dumper;
319 3 @alist = sort @alist;
320 3 @blist = sort @blist;
321 #print Dumper(\@alist);
322 #print Dumper(\@blist);
323 3 ▶ return 0 if $#alist != $#blist;
Branch: 1 branch
BranchTrueFalseTotal
if $#alist != $#blist 0 3 3
324 3 ▶ for (my $i=0; $i<= $#alist; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#alist) 6 1 7
325 6 ▶ return 0 if lc $alist[$i] ne lc $blist[$i];
Branch: 1 branch
BranchTrueFalseTotal
if lc $alist[$i] ne lc $blist[$i] 2 4 6
326 }
327 1 return 1;
328 }
329  
330 #wip
331 #sub author_bits {
332 # my $list_ref = shift;
333 # my @new;
334 # for (@$list_ref) {
335 # my $v = $_; # we copy so we don't modify the original
336 # $v =~ s/,//;
337 # $v =~ s/(\p{Ll}\p
338 # push @alist, split(/\s+/, $v);
339 # }
340 #}
341  
342 101 ▶sub _strip_non_word {
Subroutine _strip_non_word: called CC=1 SCAR=0.0
343 101 my $str = shift;
344 #abbreviation "volume" v
345 101 $str =~ s/\bvolume\b/v/gi;
346 101 $str =~ s/\bvol\.?\b/v/gi;
347 101 $str =~ s/\bv\.\b/v/gi;
348  
349 101 $str =~ s/[^[0-9a-zA-Z\)\]\(\[]+/ /g;
350 101 $str =~ s/\s+/ /g;
351 101 $str =~ s/^\s+//;
352 101 $str =~ s/\s+$//;
353 101 $str;
354 }
355  
356 3 my %nums = (
357 first => 1,
358 second => 2,
359 third => 3,
360 fourth => 4,
361 fifth => 5,
362 sixth => 6,
363 seventh => 7,
364 eighth => 8,
365 ninth => 9,
366 tenth => 10,
367 );
368 28 ▶sub extract_num {
Subroutine extract_num: called CC=5 SCAR=16.1
Pod undocumented
369 28 my $s = shift;
370 28 ▶ if ($s =~ /\b(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\b(\d+)/) 13 15 28
371 13 return $1;
372 }
373 15 ▶ if (isroman($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isroman($s)) 10 5 15
374 10 return roman2int($s);
375 }
376  
377 5 for my $n (keys %nums) {
378 35 ▶ if ($s =~ /\b$n\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\b$n\b/i) 5 30 35
379 5 return $nums{$n};
380 }
381  
382 }
383 0 return $s;
384 }
385  
386 106 ▶sub extractEdition {
Subroutine extractEdition: called CC=3 SCAR=11.0
Pod undocumented
387 106 my $s = shift;
388 106 for my $re (@ED_RES) {
389 391 ▶ if ($s =~ /$re/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /$re/i) 28 363 391
390 28 return extract_num($1);
391 }
392 }
393 78 return undef;
394 }
395  
396 48 ▶sub numdiff {
Subroutine numdiff: called CC=4 SCAR=13.9
Pod undocumented
397 48 my ($s1,$s2) = @_;
398 48 my @n1 = ($s1 =~ /\b([IXV0-9]{1,4}|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelveth|1st|2nd|3rd|4th|5th|6th|7th|8th|9th|10th|11th|12th)\b/ig);
399 48 my @n2 = ($s2 =~ /\b([IXV0-9]{1,4}|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelveth|1st|2nd|3rd|4th|5th|6th|7th|8th|9th|10th|11th|12th)\b/ig);
400 #print "In s1:" . join(",",@n1) . "\n";
401 #print "In s2:" . join(",",@n2) . "\n";
402 48 ▶ return 0 if $#n1 ne $#n2;
Branch: 1 branch
BranchTrueFalseTotal
if $#n1 ne $#n2 4 44 48
403 44 for (0..$#n1) {
404 13 ▶ return 1 if lc $n1[$_] ne lc $n2[$_];
Branch: 1 branch
BranchTrueFalseTotal
if lc $n1[$_] ne lc $n2[$_] 7 6 13
405 }
406 37 return 0;
407 }
408  
409  
410 40 ▶sub my_dist_text {
Subroutine my_dist_text: called CC=1 SCAR=0.0
Pod undocumented
411 40 my $a = lc shift;
412 40 my $b = lc shift;
413 40 $a =~ s/_/ /g;
414 40 $b =~ s/_/ /g;
415 40 return distance($a, $b);
416  
417 }
418 106 ▶sub decodeHTMLEntities {
Subroutine decodeHTMLEntities: called CC=1 SCAR=0.2
Pod undocumented
419 106 my $in = shift;
420 0 $in =~ s/&([\d\w\#]+);/&safe_decode($1)/gei;
421 106 return $in;
422 }
423  
424 0 ▶sub safe_decode {
Subroutine safe_decode: not called CC=5 SCAR=34.0
Pod undocumented
425 0 my $in = shift;
426 0 ▶ if (substr($in,0,1) eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($in, 0, 1) eq '#') { } 0 0 0
427 0 ▶ my $num = substr($in,1,1) eq 'x' ? hex(substr($in,1)) : substr($in,1);
Branch: 1 branch
BranchTrueFalseTotal
substr($in, 1, 1) eq 'x' ? : 0 0 0
428 # we check and fix cp1232 entities
429 return ($num < 127 or $num > 159) ?
430 HTML::Entities::decode_entities("&$in;") :
431 0 ▶ HTML::Entities::decode_entities("&#" . $WIN2UTF{$num} . ";");
Branch: 1 branch
BranchTrueFalseTotal
$num < 127 || $num > 159 ? : 0 0 0
Condition0%0/3
$num < 127 or $num > 159
l!l&&r!l&&!r
000
MC/DC0%0/2
$num < 127 or $num > 159
$num < 127 $num > 159
Truth table0%0/3
A or B
A= $num < 127B= $num > 159
ABresult
1X1
011
000
432 } else {
433 0 HTML::Entities::decode_entities("&$in;")
434 }
435 }
436  
437 72 ▶sub toString {
Subroutine toString: called CC=1 SCAR=0.0
Pod undocumented
438 72 my $h = shift;
439 72 return join("; ",@{$h->{authors}}) . " ($h->{date}) $h->{title}\n";
440 }
441  
442 16 ▶sub sameTitle {
Subroutine sameTitle: called CC=1 SCAR=0.0
Pod undocumented
443 16 my ($a, $b, $threshold,$loose,$nolinks,%opts) = @_;
444 16 return sameWork({ title => $a }, { title => $b }, $threshold,$loose,$nolinks,%opts);
445 }
446  
447 3 1;
448 __END__
↑ Summary