Parser/Jiao/Utility.pm

filter: Statementstmt 88.1% 112 / 127 Branchbran 50.0% 12 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 9 totaltotal 75.7% 134 / 177 CC 14 SCAR 16.5
CC
14
Cov
81%
chr_valid
12.0
normalise_name
7.3
num_of_figures
6.2
CRAP 5.2
« Parser/Jiao.pm ↑ Summary Parser.pm »
1 package Biblio::Citation::Parser::Jiao::Utility;
2  
3 ######################################################################
4 #
5 # ParaTools::Citation::Parser::Jiao::Utility;
6 #
7 ######################################################################
8 #
9 # This file is part of ParaCite Tools
10 # Based on Zhuoan Jiao's (zj@ecs.soton.ac.uk) citation parser (available
11 # at http://arabica.ecs.soton.ac.uk/code/doc/ReadMe.html)
12 #
13 # The code is relatively unchanged, except to bring into compliance
14 # with the ParaCite metadata style, and to allow interoperability with
15 # the other parsers.
16 #
17 # Copyright (c) 2002 University of Southampton, UK. SO17 1BJ.
18 #
19 # ParaTools is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
23 #
24 # ParaTools is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with ParaTools; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #
33 ######################################################################
34  
35 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 require Exporter;
39  
40 1 @ISA = qw(Exporter);
41 1 @EXPORT = qw(&normalisation &normalise_html &normalise_name
42 &normalise_date &normalise_journal &num_of_figures);
43  
44 1 $VERSION = '0.01';
45  
46 #
47 # Normalisation utilities
48 #
49 2 ▶sub normalisation {
Subroutine normalisation: called CC=1 SCAR=0.0
Pod undocumented
50 2 my($Text) = @_;
51 # replace embedded '\n' with ' '
52 2 $Text =~ s/^\s+//s;
53 2 $Text =~ s/\s+$//s;
54 2 $Text =~ s/\s+/ /g; # Use single space
55 # while ($Text =~ /.+?\n.+?/g) {
56 # $Text =~ s/(.+?)\n(.+?)/$1 $2/
57 # };
58 2 $Text =~ s/``(.*?)''/"$1"/sg; # Replace ``A Paper Title'' with "A Paper Title"
59  
60 2 $Text =~ s/\s*-\s*/-/g; # remove space around '-'
61 2 $Text =~ s/\s*'\s*/'/g; # remove space around '
62 2 $Text =~ s/\s*:\s*/:/g; # remove space around :
63 2 $Text =~ s/\(\s+/\(/g; # ( 1998) ==> (1998)
64 2 $Text =~ s/\s+\)/\)/g; # (1998 ) ==> (1998)
65 # while ($Text =~/--/g) {$Text =~ s/--/-/}; # use single '-'.
66 2 $Text =~ s/--+/-/g;
67 2 $Text =~ s/~//g; # remove '~' (e.g. C.~B.~Hanna)
68 2 $Text =~ s/[,;\s]+$//; # remove last ',;\s' on a line
69 # 'Nr.' caused error in 'find_jnl_name', i.e. it became as
70 # 'journal name' if not removed. (see: arXiv:quant-ph/9905016)
71 2 $Text =~ s/([,;])\s*Nr\.\s*(\d+[,;])/$1$2/i;
72 # "[12] For example, R. Machleidt, ...Phys. Rep. 149, 1 (1987)
73 2 $Text =~ s/^([^a-z]+)for example\W+/$1/i;
74 # '[18] *** G. Do Dang, ... (arXiv:nucl-th/9911081)
75 2 $Text =~ s/\*+//g;
76 # Phys. Rev. D56 => Phys. Rev. D. 56
77 2 $Text =~ s/phys.{1,6}rev.{1,6}([a-z])(\d+)/PHYS. REV. $1 $2/ig;
78 # Physica 34 D => Physic D 34
79 2 $Text =~ s/physica\s+(\d+)\s+([a-z])/PHYSICA $2 $1/ig;
80 # Nucl. Phys. B567 => Nucl. Phys. B
81 2 $Text =~ s/nuc.{1,6}phys.{1,6}\s+([a-z])(\d+)/NUCL. PHYS. $1 $2/ig;
82 2 return $Text;
83 };
84
85 0 ▶sub chr_valid {
Subroutine chr_valid: not called CC=3 SCAR=24.8
Pod undocumented
86 0 my $c = shift;
87 0 ▶ if( $c < 128 || $c > 255 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < 128 or $c > 255) { } 0 0 0
Condition0%0/3
$c < 128 or $c > 255
l!l&&r!l&&!r
000
MC/DC0%0/2
$c < 128 or $c > 255
$c < 128 $c > 255
Truth table0%0/3
A or B
A= $c < 128B= $c > 255
ABresult
1X1
011
000
88 0 return chr($c);
89 } else {
90 0 return ' ';
91 }
92 }
93  
94 1 ▶sub normalise_html {
Subroutine normalise_html: called CC=1 SCAR=0.0
Pod undocumented
95 1 my($Text) = @_;
96
97 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 # remove <BR> tag
100 1 $Text =~ s/<BR>//ig;
101 # Convert HTML entities to Unicode
102 0 $Text =~ s/\&#x(\w+);/chr_valid(hex($1))/eg;
103 0 $Text =~ s/\&#(\d+);/chr_valid($1)/eg;
104 1 $Text =~ s/&(\w)acute[;,]/$1/g; # a ' on top of (\w)
105 1 $Text =~ s/&(\w)cedil[;,]/$1/g; # a 'tail' under (\w), e.g Francios
106 1 $Text =~ s/&(\w)grave[;,]/$1/g; # a ` on top of (\w)
107 1 $Text =~ s/&(\w)tilde[;,]/$1/g;
108 1 $Text =~ s/&(\w)uml[;,]/$1e/g; # a '..' on top of (\w)
109 1 $Text =~ s/&(\w)slash[;,]/$1/g;
110 #$Text =~ s/&#-88;(\w)/$1/g; # as &(\w)uml (see astro-ph/9811179)
111 1 $Text =~ s/&#-\d+;\s*(\w)/$1/g;
112 1 $Text =~ s/&szlig[;,]/ss/g;
113 1 $Text =~ s/&amp[;,]/ and /g;
114 1 $Text =~ s/&#20;\s*(\w)/$1/g; # a ~ on top of (\w)
115 1 $Text =~ s/\/?i&gt;//g; # cogprints
116 1 $Text =~ s/&[a-z]+;//g; # otherwise ';' cause ref line break.
117 # $Text =~ s/&#\d+;//g; # e.g. '&#21' in 'hep-th/0001001 [99]'.
118 1 $Text =~ s/\\"(\w)/$1e/g; # G\"unter => Gueter
119
120 1 $Text =~ s/\^//g; # remove ^
121 1 $Text =~ s/([A-Z])\s*&\s*([A-Z])/$1 and $2/g; # replace '&' with 'and'
122 1 $Text =~ s/[, ]+& / and /;
123 # remove HTML markups (<b></b><i></i> etc.)
124 1 $Text =~ s/<[a-z\/]{1,3}>//ig;
125 1 return $Text
126 }
127
128
129 1 ▶sub normalise_name {
Subroutine normalise_name: called CC=6 SCAR=19.9
Pod undocumented
130 1 my($Text) = @_;
131 1 my $Suffix = '';
132 1 $Text =~ s/~//; # remove typo
133 # Jr.
134 1 ▶ if ($Text =~ s/[, \.]+(Jr|Sr|Snr)\.?\s*$//i){
Branch: 2 branches
BranchTrueFalseTotal
if ($Text =~ s/[, \.]+(Jr|Sr|Snr)\.?\s*$//i) { } 0 1 1
elsif ($Text =~ s/([, \.]+)(Jr|Sr|Snr)[. ]/$1/i) { } 0 1 1
135 0 $Suffix = $1
136 }
137 elsif ($Text =~ s/([, \.]+)(Jr|Sr|Snr)[. ]/$1/i){
138 0 $Suffix = $2
139 };
140
141 # van der Buren D => D van der Buren"
142 1 ▶ if ($Text =~ /^\s*(((van|von|de|den|der)\s+)+)(\S\S+)\s+(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text =~ /^\s*(((van|von|de|den|der)\s+)+)(\S\S+)\s+(.+)/i) 0 1 1
143 0 $Text = "$5 $1 $4"
144 };
145 1 $Text =~ s/\s+/ /g; # single space
146 1 $Text =~ s/^\W+//;
147 1 $Text =~ s/\s+$//;
148 # "A. Smith" => "A.Smith"
149 1 $Text =~ s/([a-z])s+\./$1\./ig;
150 # Ghisellini G. A. ==> G.A. Ghisellini
151 # Konenkov D. Yu. => D.Yu. Konenkov
152 1 ▶ if ($Text =~ /^([^\s.]{2,})\s+(([A-Z][a-zA-Z]?\W+)*)([A-Z][a-zA-Z]?)\W*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text =~ /^([^\s.]{2,})\s+(([A-Z][a-zA-Z]?\W+)*)([A-Z][a-zA-Z]?)\W*$/) 0 1 1
153 0 $Text = "$2$4 $1"
154 };
155
156 1 ▶ $Text = "$Text $Suffix" if ($Suffix);
Branch: 1 branch
BranchTrueFalseTotal
if $Suffix 0 1 1
157 1 $Text = tdb_normalise_name($Text);
158 1 return $Text;
159 };
160  
161 # Based on Tim's Authors::splitauthors and Authors::_cmonauthor();
162 # This subroutine is called simply because we want the author names
163 # to be transformed to a same style used by Tim's programs. Otherwise
164 # a join on author names in Publication and Reference tables will
165 # miss a lot of targets.
166 1 ▶sub tdb_normalise_name{
Subroutine tdb_normalise_name: called CC=2 SCAR=6.9
Pod undocumented
167 1 my $author = shift;
168
169 # Strip any brackets
170 1 $author =~ s/\s*\([^\)]*\)\s*//g;
171 # Get rid of any dashes (except for dashes like Hu-Su)
172 1 $author =~ s/(\W)-/$1/g;
173 # Remove any "the"s, e.g. The OPAL Collaboration
174 1 $author =~ s/\bthe\s+//ig;
175 # Sort out Jr/Jr.
176 1 $author =~ s/,?\sJr\.?/_Jr/ig;
177
178 1 $author =~ s/[\{\}]//g; # Remove any specialisations
179 1 $author =~ s/\\.//g; # Remove any escapes
180 # Convert Convert Hawking S. to S.Hawking (already done - zj)
181 # ($author =~ /(\w\w+)\s+([\.\w\s]+\.)$/) && ($author = $2.$1);
182 1 $author =~ s/\.\s/\./g; # Convert S.W. Hawking to S.W.Hawking
183 1 $author =~ s/([A-Z])\s/$1\./g; # Convert S W Hawking to S.W.Hawking
184 1 ▶ ($author = lc($author)) && ($author =~ s/\b(\w)/\U$1/g);
Branch: 1 branch
BranchTrueFalseTotal
$author = lc $author and $author =~ s/\b(\w)/\U$1\E/g 1 0 1
185 # Convert STEPHEN_HAWKING to Stephen_Hawking
186 1 $author =~ s/\s/_/g; # Convert Stephen W.Hawking to Stephen_W.Hawking
187 1 $author =~ s/\.\.+/\./g; # Remove double dots
188 1 return $author;
189 }
190  
191  
192 1 ▶sub normalise_date {
Subroutine normalise_date: called CC=1 SCAR=0.0
Pod undocumented
193 1 my($Text) = @_;
194
195 # 12-14 Dec.
196 1 $Text =~ s/[^\w\/][0-3][0-9]?[a-z]*?(\s*\-\s*[0-3][0-9]?[a-z]*?)?\s+
197 (Jan[\.\s]|January\b|Feb[\.\s]|February\b|
198 Mar[\.\s]|March\b|Apr[\.\s]|April\b|May|
199 Jun[\.\s]|June\b|Jul[\.\s]|July\b|Aug[\.\s]|August\b|
200 Sep[\.\s]|September\b|Oct[\.\s]|October\b|
201 Nov[\.\s]|November\b|Dec[\.\s]|December\b)//xig;
202
203 # Dec 12-14
204 1 $Text =~ s/(Jan[\.\s]|January\b|Feb[\.\s]|February\b|
205 Mar[\.\s]|March\b|Apr[\.\s]|April\b|May|
206 Jun[\.\s]|June\b|Jul[\.\s]|July\b|Aug[\.\s]|August\b|
207 Sep[\.\s]|September\b|Oct[\.\s]|October\b|
208 Nov[\.\s]|November\b|Dec[\.\s]|December\b)
209 [^\w\/]*[0-3][0-9]?[a-z]*?(\s*\-\s*[0-3][0-9]?[a-z]*?)?\b//xig;
210
211 1 return $Text;
212 };
213
214
215 1 ▶sub normalise_journal {
Subroutine normalise_journal: called CC=1 SCAR=0.0
Pod undocumented
216 1 my($Text) = @_;
217 1 $Text =~ s/^in\s+//i; # "in ..."
218 1 $Text =~ s/^(see )?also\s+//i; # "also ...";
219 1 $Text =~ s/\s*:\s*/:/g;
220 1 $Text =~ s/\s\s/ /g; # single space;
221 1 $Text =~ s/\.\s/\./g; # "J. Physics" => "J.Physics"
222 1 $Text =~ s/\.\(/\. \(/g;
223
224 1 $Text =~ s/^\W+//;
225 1 $Text =~ s/[^\w.]+$//;
226
227 # remove anything in brackets at the end.
228 1 $Text =~ s/\s*\([^)]+$//; # e.g. R. Ram, J. Phys. (Paris
229 1 $Text =~ s/^[^(]+\)\s*//; # e.g. a) R. Ram, J. Phys. 10, 120, 1998
230
231 # unify cases
232 #($Text = lc($Text)) && ($Text =~ s/\b(\w)/\U$1/g);
233 1 $Text = uc($Text);
234 1 return $Text;
235 };
236  
237  
238 1 ▶sub num_of_figures {
Subroutine num_of_figures: called CC=6 SCAR=18.2
Pod undocumented
239 1 my($Text) = @_;
240 1 my($N, @Nlist);
241 1 $N = 0;
242 1 @Nlist =();
243
244 1 $Text = normalisation($Text);
245 # e.g. "p. 24-26" regarded as one number.
246 # ignore 'N = 2' kind of equations, and
247 # ignore '25th' kinds (e.g. Proc. 25th ICRC).
248 # ignore 'protein Aquaporin-1 in ...'
249 # ignore 'hep-th/9901001'
250 # ignore ' ... 1.55, ...'
251 1 ▶ while ($Text =~ /(?:^|\b)[a-z]*(\d+)([a-z]*)
Branch: 1 branch
BranchTrueFalseTotal
if ($Text =~ /(?:^|\b)[a-z]*(\d+)([a-z]*) (?:-[a-z]*\d+[a-z]*)* (?:\b|$)/gix) 3 1 4
252 (?:-[a-z]*\d+[a-z]*)*
253 (?:\b|$)/gix) {
254 3 ▶ next if ($2 =~ /^th$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $2 =~ /^th$/i 0 3 3
255 3 ▶ next if ($' =~ /^\.\d+/);
Branch: 1 branch
BranchTrueFalseTotal
if $' =~ /^\.\d+/ 0 3 3
256 3 ▶ next if ($` =~ /\d+\.$/);
Branch: 1 branch
BranchTrueFalseTotal
if $` =~ /\d+\.$/ 0 3 3
257 3 ▶ next if ($` =~ /[=<>\/-]\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $` =~ m[[=<>/-]\s*$] 0 3 3
258 3 push(@Nlist, $1);
259 };
260 1 $N = scalar(@Nlist);
261 1 return $N
262 }
263
264
265 0 ▶sub remove_extra_spc {
Subroutine remove_extra_spc: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my($Text) = @_;
267 0 $Text =~ s/^\s+//;
268 0 $Text =~ s/\s+$//;
269 0 $Text =~ s/\s\s+/ /g;
270 0 return $Text
271 };
272  
273 1 1;
274  
275 __END__
« Parser/Jiao.pm ↑ Summary Parser.pm »