Lingua/Sentence.pm

filter: Statementstmt 91.6% 77 / 84 Branchbran 71.0% 27 / 38 Conditioncond 54.1% 13 / 24 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 77.6% 132 / 170 CC 29 SCAR 24.0
CC
29
Cov
80%
_preprocess
17.5
new
11.8
split
3.4
CRAP 11.0
↑ Summary auto/share/dist/Lingua-Sentence/nonbreaking_prefix.pl »
1 package Lingua::Sentence;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.100';
12 1 $VERSION = eval $VERSION;
13  
14 7 ▶sub new {
Subroutine new: called CC=11 SCAR=24.7
Pod documented
15 7 my ($class, $lang_id, $prefix_file) = @_;
16 7 ▶ Carp::croak("Invalid language id: $lang_id")
Branch: 1 branch
BranchTrueFalseTotal
unless $lang_id and $lang_id =~ /^[a-z][a-z]$/i 0 7 7
Condition33%1/3
$lang_id and $lang_id =~ /^[a-z][a-z]$/i
!ll&&!rl&&r
007
MC/DC0%0/2
$lang_id and $lang_id =~ /^[a-z][a-z]$/i
$lang_id $lang_id =~ /^[a-z][a-z]$/i
Truth table33%1/3
A and B
A= $lang_idB= $lang_id =~ /^[a-z][a-z]$/i
ABresult
0X0
100
111
17 unless ($lang_id && $lang_id =~ /^[a-z][a-z]$/i);
18  
19 # Try loading nonbreaking prefix file specified in constructor
20 7 my $dir = File::ShareDir::dist_dir('Lingua-Sentence');
21 7 my $fallback = File::Spec->catfile($dir, 'nonbreaking_prefix.' . $lang_id);
22 7 my $fallback_en = File::Spec->catfile($dir, 'nonbreaking_prefix.en');
23 7 ▶ if (defined($prefix_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix_file) { } 1 6 7
24 1 ▶ unless (-e $prefix_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $prefix_file) 0 1 1
25 0 warn
26 "WARNING: Specified prefix file '$prefix_file' does not exist, attempting fall-back to $lang_id version...\n";
27 0 $prefix_file = $fallback;
28 }
29 }
30 else {
31 6 $prefix_file = $fallback;
32 }
33  
34 #default back to English if we don't have a language-specific prefix file
35 7 ▶ unless (-e $prefix_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $prefix_file) 1 6 7
36 1 $prefix_file = $fallback_en;
37 1 warn
38 "WARNING: No known abbreviations for language '$lang_id', attempting fall-back to English version...\n";
39 }
40  
41 # grab all non-breaking prefixes and store them in a hashref
42 7 my $nb_prefix = {};
43 7 my $pt = Path::Tiny::path($prefix_file);
44 7 ▶ if ($pt->is_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pt->is_file) { } 7 0 7
45 7 for my $line ($pt->lines_utf8({chomp => 1})) {
46 2887 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 31 2856 2887
47 2856 ▶ next if substr($line, 0, 1) eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, 0, 1) eq "#" 60 2796 2856
48 2796 ▶ if ($line =~ /^(.*?)\s+#NUMERIC_ONLY#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.*?)\s+#NUMERIC_ONLY#/) { } 10 2786 2796
49 10 $nb_prefix->{$1} = 2;
50 }
51 else {
52 2786 $nb_prefix->{$line} = 1;
53 }
54 }
55 }
56 else {
57 0 die("ERROR: No abbreviations files found in $dir\n");
58 }
59  
60 7 return bless {LangID => $lang_id, Nonbreaking => $nb_prefix,}, $class;
61 }
62  
63 8 ▶sub split {
Subroutine split: called CC=3 SCAR=12.3
Pod documented
64 8 my $self = shift;
65 8 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 8 8
66 0 return "Unnamed $self";
67 }
68 8 my $text = shift;
69 8 ▶ if (!$text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text) 0 8 8
70 0 return '';
71 }
72 8 return _preprocess($self, $text);
73 }
74  
75 5 ▶sub split_array {
Subroutine split_array: called CC=3 SCAR=11.8
Pod documented
76 5 my $self = shift;
77 5 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 5 5
78 0 return "Unnamed $self";
79 }
80 5 my $text = shift;
81 5 ▶ if (!$text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text) 0 5 5
82 0 return ();
83 }
84 5 my $splittext = _preprocess($self, $text);
85 5 chomp $splittext;
86 5 return split(/\n/, $splittext);
87 }
88  
89 13 ▶sub _preprocess {
Subroutine _preprocess: called CC=15 SCAR=28.6
90 13 my ($self, $text) = @_;
91  
92 #####add sentence breaks as needed#####
93  
94 #non-period end of sentence markers (?!) followed by sentence starters.
95 13 $text =~ s/([?!]) +(['"([\x{00bf}\x{00A1}\p{IsPi}]*[\p{IsUpper}])/$1\n$2/g;
96  
97 #multi-dots followed by sentence starters
98 13 $text
99 =~ s/(\.[\.]+) +(['"([\x{00bf}\x{00A1}\p{IsPi}]*[\p{IsUpper}])/$1\n$2/g;
100  
101 # add breaks for sentences that end with some sort of punctuation inside a quote or parenthetical and are followed by a possible sentence starter punctuation and upper case
102 13 $text
103 =~ s/([?!\.][\ ]*['")\]\p{IsPf}]+) +(['"([\x{00bf}\x{00A1}\p{IsPi}]*[\ ]*[\p{IsUpper}])/$1\n$2/g;
104  
105 # add breaks for sentences that end with some sort of punctuation are followed by a sentence starter punctuation and upper case
106 13 $text
107 =~ s/([?!\.]) +(['"([\x{00bf}\x{00A1}\p{IsPi}]+[\ ]*[\p{IsUpper}])/$1\n$2/g;
108  
109 # special punctuation cases are covered. Check all remaining periods.
110 13 my $word;
111 my $i;
112 13 my @words = split(/ +/, $text);
113 13 $text = "";
114 13 ▶ for ($i = 0; $i < (scalar(@words) - 1); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar(@words) - 1) 135 13 148
115 135 ▶ if ($words[$i] =~ /([\p{IsAlnum}\.\-]*)([\'\"\)\]\%\p{IsPf}]*)(\.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[$i] =~ /([\p{IsAlnum}\.\-]*)([\'\"\)\]\%\p{IsPf}]*)(\.+)$/) 13 122 135
116  
117 #check if $1 is a known honorific and $2 is empty, never break
118 13 my $prefix = $1;
119 13 my $starting_punct = $2;
120 13 ▶ if ( $prefix
Branch: 3 branches
BranchTrueFalseTotal
if ($prefix and $self->{'Nonbreaking'}{$prefix} and $self->{'Nonbreaking'}{$prefix} == 1 and not $starting_punct) { } 2 11 13
elsif ($words[$i] =~ /(\.)[\p{IsUpper}\-]+(\.+)$/) { } 0 11 11
elsif ($words[$i + 1] =~ /^([ ]*['"([\x{00bf}\x{00A1}\p{IsPi}]*[ ]*[\p{IsUpper}0-9])/) { } 11 0 11
Condition77%7/9
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 1 and not $starting_punct
!ll&&!rl&&r
1102
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 1
!ll&&!rl&&r
1102
$prefix and $self->{"Nonbreaking"}{$prefix}
!ll&&!rl&&r
1102
MC/DC50%2/4
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 1 and not $starting_punct
$prefix $self->{"Nonbreaking"}{$prefix} $self->{"Nonbreaking"}{$prefix} == 1 not $starting_punct
Truth table60%3/5
A and B and C and not(D)
A= $prefixB= $self->{"Nonbreaking"}{$prefix}C= $self->{"Nonbreaking"}{$prefix} == 1D= not $starting_punct
ABCDresult
0XXX0
10XX0
110X0
11100
11111
121 && $self->{Nonbreaking}{$prefix}
122 && $self->{Nonbreaking}{$prefix} == 1
123 && !$starting_punct)
124 {
125 #not breaking;
126 }
127 elsif ($words[$i] =~ /(\.)[\p{IsUpper}\-]+(\.+)$/) {
128  
129 #not breaking - upper case acronym
130 }
131 elsif ($words[$i + 1]
132 =~ /^([ ]*['"([\x{00bf}\x{00A1}\p{IsPi}]*[ ]*[\p{IsUpper}0-9])/)
133 {
134 #the next word has a bunch of initial quotes, maybe a space, then either upper case or a number
135 $words[$i] = $words[$i] . "\n"
136 unless ($prefix
137 && $self->{Nonbreaking}{$prefix}
138 11 ▶ && $self->{Nonbreaking}{$prefix} == 2
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 2 and not $starting_punct and $words[$i + 1] =~ /^[0-9]+/ 11 0 11
Condition41%5/12
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 2 and not $starting_punct and $words[$i + 1] =~ /^[0-9]+/
!ll&&!rl&&r
1100
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 2 and not $starting_punct
!ll&&!rl&&r
1100
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 2
!ll&&!rl&&r
1100
$prefix and $self->{"Nonbreaking"}{$prefix}
!ll&&!rl&&r
1100
MC/DC0%0/5
$prefix and $self->{"Nonbreaking"}{$prefix} and $self->{"Nonbreaking"}{$prefix} == 2 and not $starting_punct and $words[$i + 1] =~ /^[0-9]+/
$prefix $self->{"Nonbreaking"}{$prefix} $self->{"Nonbreaking"}{$prefix} == 2 not $starting_punct $words[$i + 1] =~ /^[0-9]+/
Truth table33%2/6
A and B and C and not(D) and E
A= $prefixB= $self->{"Nonbreaking"}{$prefix}C= $self->{"Nonbreaking"}{$prefix} == 2D= not $starting_punctE= $words[$i + 1] =~ /^[0-9]+/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
139 && !$starting_punct
140 && ($words[$i + 1] =~ /^[0-9]+/));
141  
142 #we always add a return for these unless we have a numeric non-breaker and a number start
143 }
144  
145 }
146 135 $text = $text . $words[$i] . " ";
147 }
148  
149 #we stopped one token from the end to allow for easy look-ahead. Append it now.
150 13 $text = $text . $words[$i];
151  
152 # clean up spaces at head and tail of each line as well as any double-spacing
153 13 $text =~ s/ +/ /g;
154 13 $text =~ s/\n /\n/g;
155 13 $text =~ s/ \n/\n/g;
156 13 $text =~ s/^ //g;
157 13 $text =~ s/ $//g;
158  
159 #add trailing break
160 13 ▶ $text .= "\n" unless $text =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n$/ 13 0 13
161  
162 13 return $text;
163 }
164  
165 1 1;
166 __END__
↑ Summary auto/share/dist/Lingua-Sentence/nonbreaking_prefix.pl »