Common.pm

filter: Statementstmt 53.9% 76 / 141 Branchbran 13.3% 4 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 9 / 15 Podpod 90.0% 9 / 10 totaltotal 50.0% 98 / 196 SCAR 41.0
CC
17
Cov
47%
custom_norm
210.0
reverse_punct
4.0
pretty_doubles
2.0
CRAP 60.6
« WordList.pm ↑ Summary Lookup.pm »
1 package WordLists::Common;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Unicode::Normalize; #provides NFD
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use WordLists::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 our $VERSION = $WordLists::Base::VERSION;
8
9 8 our $AUTOLOAD;
10 8 require Exporter;
11 8 our @ISA = qw (Exporter);
12 8 our @EXPORT = ();
13 8 our @EXPORT_OK = qw(
14 pretty_doubles
15 pretty_singles
16 pretty_endash
17 norm_spacing
18 custom_norm
19 generic_norm_hw
20 generic_norm_pos
21 generic_minimal_pos
22 uniques
23 @sPosWords
24 @sDefaultAttList
25 @sDefiningAttlist
26 @sParsingParameters
27 reverse_punct
28 );
29 8 our @sDefaultAttList = qw(hw pos def eg);
30 8 our @sDefiningAttlist = qw(hw pos);
31 8 our @sParsingParameters = qw(is_header field_sep attlist default_attlist header_marker);
32 8 our @sPosWords = (qw(
33 n
34 noun
35 v
36 verb
37 adj
38 adjective
39 adv
40 advb
41 adverb
42 conj
43 conjunction
44 excl
45 exclamation
46 expression
47 pref
48 prefix
49 suffix
50 det
51 determiner
52 quant
53 quantifier
54 postmodifier
55 predeterminer
56 abbreviation
57 pv
58 mv
59 auxiliary
60 aux
61 prep
62 preposition
63 number
64 ordinal
65 cardinal
66
67 ),
68 'ordinal number',
69 'cardinal number',
70 'plural noun',
71 'compound noun',
72 'phrasal verb',
73 'modal verb',
74 'auxiliary verb',
75 );
76 sub pretty_doubles($)
77 {
78 0 ▶ my $s = shift;
Subroutine pretty_doubles: not called CC=1 SCAR=6.9
Pod documented
79 0 $s =~ s/[“"”]/"/g;
80 0 $s =~ s/"$/”/g;
81 0 $s =~ s/^"/“/g;
82 0 $s =~ s/"([\s\t\r\n])/”$1/g;
83 0 $s =~ s/([\s\t\r\n])"/$1“/g;
84 0 $s =~ s/([\(\{\[])"/$1“/g;
85 0 $s =~ s/"([\)\}\]])/”$1/g;
86 0 $s =~ s/([\w\.\?\!])"/$1”/g;
87 0 $s =~ s/"/“/g;
88 0 return $s;
89 }
90 sub reverse_punct ($)
91 {
92 4 ▶ my $s = shift;
Subroutine reverse_punct: called CC=4 SCAR=13.9
Pod undocumented
93 4 my %sReversal = (qw`
94 ( )
95 [ ]
96 { }
97 < >
98 ‘ ’
99 “ ”
100 ‹ ›
101 « »
102 ¡ !
103 ¿ ?
104 `);
105 4 ▶ if (defined $sReversal{$s})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sReversal{$s}) 2 2 4
106 {
107 2 return $sReversal{$s};
108 }
109 2 foreach (qw`< [ { (`)
110 {
111 3 my $sToFind = quotemeta ($_) . "([^" . quotemeta ($sReversal{$_}) . "]+)". quotemeta $sReversal{$_};
112 3 ▶ if ($s =~ m/^$sToFind$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^$sToFind$/) 2 1 3
113 {
114 2 my $sR = $s;
115 2 $sR =~ s/^$sToFind/$_\/$1$sReversal{$_}/g;
116 2 return $sR;
117 }
118 }
119 0 return $s;
120 }
121 sub pretty_singles($)
122 {
123 0 ▶ my $s = shift;
Subroutine pretty_singles: not called CC=1 SCAR=6.9
Pod documented
124 0 $s =~ s/[‘'’]/'/g;
125 0 $s =~ s/'$/’/g;
126 0 $s =~ s/^'/‘/g;
127 0 $s =~ s/'([\s\t\r\n])/’$1/g;
128 0 $s =~ s/([\s\t\r\n])'/$1‘/g;
129 0 $s =~ s/([\(\{\[])'/$1‘/g;
130 0 $s =~ s/'([\)\}\]])/’$1/g;
131 0 $s =~ s/([\w\.\?\!])'/$1’/g;
132 0 $s =~ s/'/“/g;
133 0 return $s;
134 }
135
136 sub pretty_endash($)
137 {
138 0 ▶ my $s = shift;
Subroutine pretty_endash: not called CC=1 SCAR=6.9
Pod documented
139 0 $s =~ s/([\s\t\r\n])-([\s\t\r\n])/$1–$2/g;
140 0 $s =~ s/([\s\t\r\n])-$/$1–/g;
141 0 $s =~ s/^-([\s\t\r\n])/–$1/g;
142 0 return $s;
143 }
144
145 sub norm_spacing($)
146 {
147 13 ▶ my $s = shift;
Subroutine norm_spacing: called CC=1 SCAR=0.0
Pod documented
148 13 $s =~ s/^\s+//;
149 13 $s =~ s/\s+$//;
150 13 $s =~ s/\s+/ /;
151 13 return $s;
152 }
153
154 sub custom_norm
155 {
156 0 ▶ my $s = shift;
Subroutine custom_norm: not called CC=14 SCAR=53.5
Pod documented
157 0 my $args = shift;
158 0 ▶ return $s unless ref $args eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq ref {} 0 0 0
159 0 ▶ $s = lc $s if $args->{'lc'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"lc"} 0 0 0
160 0 ▶ $s = uc $s if $args->{'uc'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"uc"} 0 0 0
161 0 ▶ if ($args->{'trim_space'})
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trim_space"}) 0 0 0
162 {
163 0 $s =~ s/^\s+//;
164 0 $s =~ s/\s+$//;
165 0 $s =~ s/[\t\r\n\s]+/ /g;
166 }
167 0 ▶ $s =~ s/\bsb\b/someone/g if $args->{'sb'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"sb"} 0 0 0
168 0 ▶ $s =~ s/\bsth\b/something/g if $args->{'sth'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"sth"} 0 0 0
169 0 ▶ $s =~ s/\(.*\)//g if $args->{'brackets'} eq 'kill';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"brackets"} eq "kill" 0 0 0
170 0 ▶ $s =~ tr/()//d if $args->{'brackets'} eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"brackets"} eq "ignore" 0 0 0
171 0 ▶ $s =~ s/\[.*\]//g if $args->{'squares'} eq 'kill';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"squares"} eq "kill" 0 0 0
172 0 ▶ $s =~ tr/[]//d if $args->{'squares'} eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"squares"} eq "ignore" 0 0 0
173 0 ▶ if ($args->{'accents'})
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"accents"}) 0 0 0
174 {
175 0 $s = NFD($s); # These two lines use Unicode::Normalize::NFD to
176 0 $s =~ s/\pM//og; # remove accents but keep the underlying characters
177 }
178
179 0 ▶ $s =~ s/[^[:alpha:][:digit:]]//g if $args->{'alnum_only'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"alnum_only"} 0 0 0
180 # can't and can`t should match. So, unfortunately, does cant
181 0 ▶ $s =~ s/_//g if $args->{'alnum_only'};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"alnum_only"} 0 0 0
182 0 return $s;
183 }
184
185 sub generic_norm_hw($)
186 {
187 15 ▶ my $s = lc shift;
Subroutine generic_norm_hw: called CC=1 SCAR=0.0
Pod documented
188 15 $s =~ s/\(.*\)//g;
189 15 $s =~ s/\bsb\b/someone/g;
190 15 $s =~ s/\bsth\b/something/g;
191 15 $s =~ s/^the //g;
192 15 $s = NFD($s); # These two lines use Unicode::Normalize::NFD to
193 15 $s =~ s/\pM//og; # remove accents but keep the underlying characters
194 15 $s =~ s/[^[:alpha:][:digit:]]//g;
195 # can't and can`t should match. So, unfortunately, does cant
196 15 $s =~ s/_//g;
197 15 return $s;
198 }
199
200 sub generic_norm_pos($)
201 {
202 13 ▶ my $sPos = lc shift;
Subroutine generic_norm_pos: called CC=1 SCAR=0.0
Pod documented
203 13 $sPos =~ tr/\-\t\r\n \./ /;
204
205 13 $sPos = norm_spacing ($sPos);
206
207 13 $sPos =~ s<\b(pl|plural)\b>
208 <plural>;
209
210 13 $sPos =~ s<\b(comp|compound)\b>
211 <compound>;
212
213 13 $sPos =~ s<\b(n|noun)\b>
214 <noun>;
215
216 13 $sPos =~ s<\b(a|adj|adjective)\b>
217 <adjective>;
218
219 13 $sPos =~ s<\b(adv|advb|adverb)\b>
220 <adverb>;
221
222 13 $sPos =~ s<\b(preposition|prep)\b>
223 <preposition>;
224
225 13 $sPos =~ s<\b(quant|quantifier|q)\b>
226 <quantifier>;
227
228 13 $sPos =~ s<\b(pre)(det|determiner|d)\b>
229 <$1determiner>;
230
231 13 $sPos =~ s<\b(pronoun|pron)\b>
232 <pronoun>;
233
234 13 $sPos =~ s<\b(v|verb)\b>
235 <verb>;
236
237 13 $sPos =~ s<\b(phr|phrase)\b>
238 <phrase>;
239
240 13 $sPos =~ s<\b(exp|expr|expression)\b>
241 <phrase>;
242
243 13 $sPos =~ s<\b(mod|modal)\b>
244 <modal>;
245
246 13 $sPos =~ s<\bphrase\s+verb\b>
247 <phrasal verb>;
248
249 13 $sPos =~ s<\bp\s*verb\b>
250 <phrasal verb>;
251
252 13 $sPos =~ s<\b(prefix|pref)\b>
253 <prefix>;
254
255 13 $sPos =~ s<\b(suffix|suff)\b>
256 <suffix>;
257
258 13 $sPos =~ s<\b(short|abbreviated|abbreviation|abbrev|abbr)( form)?\b>
259 <abbreviation>;
260
261 13 $sPos =~ s<\b(conj|conjunction)\b>
262 <conjunction>;
263
264 13 $sPos =~ s<\b(int|interj|inter|interjection)\b>
265 <interjection>;
266
267 13 $sPos =~ s<\b(ex|excl|exclam|exclamation)\b>
268 <exclamation>;
269
270 13 return $sPos;
271 }
272
273
274 sub generic_minimal_pos($)
275 {
276 0 ▶ my $sPos = generic_norm_pos(shift);
Subroutine generic_minimal_pos: not called CC=1 SCAR=6.9
Pod documented
277
278 0 $sPos =~ s<(adverb)>
279 <adv>;
280 0 $sPos =~ s<(adjective)>
281 <adj>;
282 0 $sPos =~ s<(phrasal)>
283 <p>;
284 0 $sPos =~ s<(modal)>
285 <>;
286 0 $sPos =~ s<(verb)>
287 <v>;
288 0 $sPos =~ s<(noun)>
289 <n>;
290 0 $sPos =~ s<(adjective)>
291 <adj>;
292 0 $sPos =~ s<(preposition)>
293 <prep>;
294 0 $sPos =~ s<(exclamation)>
295 <excl>;
296 0 $sPos =~tr/ //d;
297 0 return $sPos;
298 }
299
300 sub uniques
301 {
302 0 ▶ my %bSeen;
Subroutine uniques: not called CC=1 SCAR=6.9
Pod documented
303 0 return grep {$bSeen{$_}++; $bSeen{$_} ==1;} @_;
304 }
305
306 8 1;
307
308
309 =pod
310
311 =head1 NAME
312
313 WordLists::Common
314
315 =head1 SYNOPSIS
316
317 use WordLists::Common qw(pretty_doubles pretty_singles);
318 print pretty_doubles (pretty_singles (
319 qq{"That's right," she said, "I was told to 'get lost!'".}
320 ) );
321
322 =head1 DESCRIPTION
323
324 This provides common functions and values of relevance to wordlists - such as normalising parts of speech and typographic dashes and quotes. Exportable functions and values include:
325
326 =over
327
328 =item *
329 C<@sPosWords>, a list of things which look like parts of speech (to help parsing things like "head verb", "head up", "head noun")
330
331 =item *
332 A function C<pretty_endash> replacing space + hyphen + space with space + en-dash + space.
333
334 =item *
335 A function C<pretty_doubles> replacing double quotes with 'smart' double quotes.
336
337 =item *
338 A function C<pretty_singles> replacing apostrophe/single-quote with 'smart' single quotes.
339
340 =item *
341 A function C<norm_spacing>
342
343 =item *
344 A function C<custom_norm> which takes several options:
345
346 =over
347
348 =item *
349 C<lc> - if true, lowercases the string.
350
351 =item *
352 C<uc> - if true, uppercases the string. Overrides C<lc>.
353
354 =item *
355 C<trim_space> - if true, removes initial and final space, and also condenses repeating white space to a single C<\x20>.
356
357 =item *
358 C<alnum_only> - if true, removes characters other than alphabetic ones or digits.
359
360 =item *
361 C<brackets> - if this is 'kill', removes the contents of any C<()> brackets; if 'ignore', removes the brackets themselves.
362
363 =item *
364 C<squares> - if this is 'kill', removes the contents of any C<[]> brackets; if 'ignore', removes the brackets themselves.
365
366 =item *
367 C<accents> - if true, removes accents and modifier characters from letters.
368
369 =item *
370 C<sb> - if true, replaces 'sb' with 'someone'.
371
372 =item *
373 C<sth> - if true, replaces 'sth' with 'something'.
374
375 =back
376
377 =item *
378 A function C<generic_norm_hw> which returns a word without accents or characters other than [a-z0-9].
379
380 =item *
381 A function C<generic_norm_pos> for normalising parts of speech so that 'v' and 'verb' match.
382
383 =item *
384 A function C<generic_minimal_pos> which will normalise parts of speech and reduce them to 'minimal' ones.
385
386 =item *
387 A function C<uniques> which will reduce a list to the unique members.
388
389 =back
390
391 =head1 BUGS
392
393 Please use the Github issues tracker.
394
395 =head1 LICENSE
396
397 Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
398
399 =cut
« WordList.pm ↑ Summary Lookup.pm »