Tool/Segment/RuleBased.pm

filter: Statementstmt 49.6% 63 / 127 Branchbran 17.5% 7 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 68.4% 13 / 19 Podpod 46.1% 6 / 13 totaltotal 38.5% 89 / 231 CC 28 SCAR 45.2
CC
28
Cov
37%
split_at_list_items
237.9
handle_long_segment
20.0
split_hard
12.0
CRAP 91.6
« Block/Read/CoNLLX.pm ↑ Summary Tool/Lexicon/CS.pm »
1 package Treex::Tool::Segment::RuleBased;
2 4 $Treex::Tool::Segment::RuleBased::VERSION = '2.20151102';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 has use_paragraphs => (
10 is => 'ro',
11 isa => 'Bool',
12 default => 1,
13 documentation =>
14 'Should paragraph boundaries be preserved as sentence boundaries?'
15 . ' Paragraph boundary is defined as two or more consecutive newlines.',
16 );
17  
18 4 has use_lines => (
19 is => 'ro',
20 isa => 'Bool',
21 default => 0,
22 documentation =>
23 'Should newlines in the text be preserved as sentence boundaries?'
24 . '(But if you want to detect sentence boundaries just based on newlines'
25 . ' and nothing else, use rather W2A::SegmentOnNewlines.)',
26 );
27  
28 4 has limit_words => (
29 is => 'ro',
30 isa => 'Int',
31 default => 250,
32 documentation =>
33 'Should very long segments (longer than the given number of words) be split?'
34 . 'The number of words is only approximate; detected by counting whitespace only,'
35 . 'not by full tokenization. Set to zero to disable this function completely.',
36 );
37  
38 4 has detect_lists => (
39 is => 'ro',
40 isa => 'Int',
41 default => 100,
42 documentation =>
43 'Minimum (approx.) number of words to toggle list detection, 0 = never, 1 = always.'
44 );
45  
46 # Tokens that usually do not end a sentence even if they are followed by a period and a capital letter:
47 # * single uppercase letters serve usually as first name initials
48 # * in langauge-specific descendants consider adding
49 # * period-ending items that never indicate sentence breaks
50 # * titles before names of persons etc.
51 #
52 # Note, that we cannot write
53 # sub get_unbreakers { return qr{...}; }
54 # because we want the regex to be compiled just once, not on every method call.
55 4 my $UNBREAKERS = qr{\p{Upper}};
56  
57 1 ▶sub unbreakers {
Subroutine unbreakers: called CC=1 SCAR=0.0
Pod documented
58 1 return $UNBREAKERS;
59 }
60  
61 # Characters that can appear after period (or other end-sentence symbol)
62 1 ▶sub closings {
Subroutine closings: called CC=1 SCAR=0.0
Pod documented
63 1 return '"”»)';
64 }
65  
66 # Characters that can appear before the first word of a sentence
67 1 ▶sub openings {
Subroutine openings: called CC=1 SCAR=0.0
Pod documented
68 1 return '"“«(';
69 }
70  
71 # Contextual rules for "un-breaking" (to be overridden)
72 1 ▶sub apply_contextual_rules {
Subroutine apply_contextual_rules: called CC=1 SCAR=0.0
Pod documented
73 1 my ($self, $text) = @_;
74 1 return $text;
75 }
76  
77 1 ▶sub get_segments {
Subroutine get_segments: called CC=4 SCAR=14.0
Pod documented
78 1 my ( $self, $text ) = @_;
79  
80 # Pre-processing
81 1 $text = $self->apply_contextual_rules($text);
82  
83 1 my $unbreakers = $self->unbreakers;
84 1 $text =~ s/\b($unbreakers)\./$1<<<DOT>>>/g;
85  
86 # two newlines usually separate paragraphs
87 1 ▶ if ( $self->use_paragraphs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_paragraphs) 1 0 1
88 1 $text =~ s/([^.!?])\n\n+/$1<<<SEP>>>/gsm;
89 }
90  
91 1 ▶ if ( $self->use_lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_lines) 0 1 1
92 0 $text =~ s/\n/<<<SEP>>>/gsm;
93 }
94  
95 # Normalize whitespaces
96 1 $text =~ s/\s+/ /gsm;
97  
98 # This is the main work
99 1 $text = $self->split_at_terminal_punctuation($text);
100  
101 # Post-processing
102 1 $text =~ s/<<<SEP>>>/\n/gsmx;
103 1 $text =~ s/<<<DOT>>>/./gsxm;
104 1 $text =~ s/\s+$//gsxm;
105 1 $text =~ s/^\s+//gsxm;
106  
107 # try to separate various list items (e.g. TV programmes, calendars)
108 1 my @segs = map { $self->split_at_list_items($_) } split /\n/, $text;
109  
110 # handle segments that are too long
111 1 ▶ return map { $self->segment_too_long($_) ? $self->handle_long_segment($_) : $_ } @segs;
Branch: 1 branch
BranchTrueFalseTotal
$self->segment_too_long($_) ? : 0 2 2
112 }
113  
114 1 ▶sub split_at_terminal_punctuation {
Subroutine split_at_terminal_punctuation: called CC=1 SCAR=0.0
Pod documented
115 1 my ( $self, $text ) = @_;
116 1 my ( $openings, $closings ) = ( $self->openings, $self->closings );
117 1 $text =~ s{
118 ([.?!]) # $1 = end-sentence punctuation
119 ([$closings]?) # $2 = optional closing quote/bracket
120 \s # space
121 ([$openings]?\p{Upper}) # $3 = uppercase letter (optionally preceded by opening quote)
122 }{$1$2\n$3}gsxm;
123 1 return $text;
124 }
125  
126 0 ▶sub handle_long_segment {
Subroutine handle_long_segment: not called CC=4 SCAR=30.0
Pod undocumented
127 0 my ( $self, $seg ) = @_;
128  
129 # split at some other dividing punctuation characters (poems, unending speech)
130 0 ▶ my @split = map { $self->segment_too_long($_) ? $self->split_at_dividing_punctuation($_) : $_ } $seg;
Branch: 1 branch
BranchTrueFalseTotal
$self->segment_too_long($_) ? : 0 0 0
131  
132 # split at any punctuation
133 0 ▶ @split = map { $self->segment_too_long($_) ? $self->split_at_any_punctuation($_) : $_ } @split;
Branch: 1 branch
BranchTrueFalseTotal
$self->segment_too_long($_) ? : 0 0 0
134  
135 # split hard if still too long
136 0 ▶ return map { $self->segment_too_long($_) ? $self->split_hard($_) : $_ } @split;
Branch: 1 branch
BranchTrueFalseTotal
$self->segment_too_long($_) ? : 0 0 0
137 }
138  
139 # Return 1 if the segment is too long
140 2 ▶sub segment_too_long {
Subroutine segment_too_long: called CC=3 SCAR=11.3
Pod undocumented
141 2 my ( $self, $seg ) = @_;
142  
143 # skip everything if the limit is infinity
144 2 ▶ return 0 if ( $self->limit_words == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->limit_words == 0 0 2 2
145  
146 # return 1 if the number of space-separated segments exceeds the limit
147 2 my $wc = () = $seg =~ m/\s+/g;
148 2 ▶ return 1 if ( $wc >= $self->limit_words );
Branch: 1 branch
BranchTrueFalseTotal
if $wc >= $self->limit_words 0 2 2
149 2 return 0;
150 }
151  
152 # "Non-final" punctuation that could divide segments (NB: single dot excluded due to abbreviations)
153 4 my $DIV_PUNCT = qr{(!|\.\.+|\?|\*|[–—-](\s*[–—-])+|;)};
154  
155 0 ▶sub split_at_dividing_punctuation {
Subroutine split_at_dividing_punctuation: not called CC=1 SCAR=6.9
Pod undocumented
156 0 my ( $self, $text ) = @_;
157  
158 0 my $closings = $self->closings;
159 0 $text =~ s/($DIV_PUNCT\s*[$closings]?,?)/$1\n/g;
160  
161 0 return split /\n/, $self->_join_too_short_segments($text);
162 }
163  
164 # Universal list types (currently only semicolon-separated lists, to be overridden in language-specific blocks)
165 4 my $LIST_TYPES = [
166 {
167 name => ';', # a label for the list type (just for debugging)
168 sep => ';\h+', # separator regexp
169 sel_sep => undef, # separator regexp used only for the selection of this list (sep used if not set)
170 type => 'e', # type of separator (ending: e / staring: s)
171 max => 400, # maximum average list-item length (overrides the default)
172 min => 30, # minimum average list-item length (overrides the default)
173 # negative pre-context, not used if not set (here: skip semicolons separating just numbers)
174 neg_pre => '[0-9]\h*(?=;\h*[0-9]+(?:[^\.0-9]|\.[0-9]|$))',
175 },
176 ];
177  
178 # Language-specific blocks should override this method and provide usual list types for the given language
179 0 ▶sub list_types {
Subroutine list_types: not called CC=1 SCAR=6.9
Pod undocumented
180 0 return @{$LIST_TYPES};
181 }
182  
183 4 my $MAX_AVG_ITEM_LEN = 400; # default maximum average list item length, in characters
184 4 my $MIN_AVG_ITEM_LEN = 30; # default minimum average list item length, in characters
185 4 my $MIN_LIST_ITEMS = 3; # minimum number of items in a list
186 4 my $PRIORITY = 2.5; # multiple of list items a lower-rank list type must have over a higher-rank type
187  
188 2 ▶sub split_at_list_items {
Subroutine split_at_list_items: called CC=17 SCAR=54.7
Pod undocumented
189  
190 2 my ( $self, $text ) = @_;
191  
192 # skip this if list detection is turned off
193 2 ▶ return $text if ( $self->detect_lists == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->detect_lists == 0 0 2 2
194  
195 # skip too short lines
196 2 my $wc = () = $text =~ m/\s+/g;
197 2 ▶ return $text if ( $self->detect_lists > $wc );
Branch: 1 branch
BranchTrueFalseTotal
if $self->detect_lists > $wc 2 0 2
198  
199 0 my @list_types = $self->list_types;
200 0 my $sel_list_type;
201 my $sel_len;
202  
203 # find out which list type is the best for the given text
204 0 ▶ for ( my $i = 0; $i < @list_types; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @list_types) 0 0 0
205  
206 0 my $cur_list_type = $list_types[$i];
207 0 ▶ my $sep = $cur_list_type->{sel_sep} || $cur_list_type->{sep};
Condition0%0/3
$cur_list_type->{'sel_sep'} || $cur_list_type->{'sep'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cur_list_type->{'sel_sep'} || $cur_list_type->{'sep'}
$cur_list_type->{'sel_sep'} $cur_list_type->{'sep'}
Truth table0%0/3
A || B
A= $cur_list_type->{'sel_sep'}B= $cur_list_type->{'sep'}
ABresult
1X1
011
000
208 0 my $neg = $cur_list_type->{neg_pre};
209 0 ▶ my $min = $cur_list_type->{min} || $MIN_AVG_ITEM_LEN;
Condition0%0/3
$cur_list_type->{'min'} || $MIN_AVG_ITEM_LEN
l!l&&r!l&&!r
000
MC/DC0%0/2
$cur_list_type->{'min'} || $MIN_AVG_ITEM_LEN
$cur_list_type->{'min'} $MIN_AVG_ITEM_LEN
Truth table0%0/3
A || B
A= $cur_list_type->{'min'}B= $MIN_AVG_ITEM_LEN
ABresult
1X1
011
000
210 0 ▶ my $max = $cur_list_type->{max} || $MAX_AVG_ITEM_LEN;
Condition0%0/3
$cur_list_type->{'max'} || $MAX_AVG_ITEM_LEN
l!l&&r!l&&!r
000
MC/DC0%0/2
$cur_list_type->{'max'} || $MAX_AVG_ITEM_LEN
$cur_list_type->{'max'} $MAX_AVG_ITEM_LEN
Truth table0%0/3
A || B
A= $cur_list_type->{'max'}B= $MAX_AVG_ITEM_LEN
ABresult
1X1
011
000
211  
212 0 my $items = () = $text =~ m/$sep/gi;
213  
214 # count number of items; exclude negative pre-context matches, if negative pre-context is specified
215 0 my $false = 0;
216 0 ▶ $false = () = $text =~ m/$neg(?=$sep)/gi if ($neg);
Branch: 1 branch
BranchTrueFalseTotal
if $neg 0 0 0
217 0 $items -= $false;
218  
219 0 ▶ my $len = $items > 0 ? ( length($text) / $items ) : 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
$items > 0 ? : 0 0 0
220  
221 # test if this type overrides the previously set one
222 0 ▶ if ( $items >= $MIN_LIST_ITEMS && $len < $max && $len > $min && ( !$sel_len || $len * $PRIORITY < $sel_len ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($items >= $MIN_LIST_ITEMS and $len < $max and $len > $min and !$sel_len || $len * $PRIORITY < $sel_len) 0 0 0
Condition0%0/12
$items >= $MIN_LIST_ITEMS and $len < $max and $len > $min and !$sel_len || $len * $PRIORITY < $sel_len
!ll&&!rl&&r
000
$items >= $MIN_LIST_ITEMS and $len < $max and $len > $min
!ll&&!rl&&r
000
$items >= $MIN_LIST_ITEMS and $len < $max
!ll&&!rl&&r
000
not $sel_len or $len * $PRIORITY < $sel_len
l!l&&r!l&&!r
000
MC/DC0%0/5
$items >= $MIN_LIST_ITEMS and $len < $max and $len > $min and !$sel_len || $len * $PRIORITY < $sel_len
$items >= $MIN_LIST_ITEMS $len < $max $len > $min not $sel_len $len * $PRIORITY < $sel_len
Truth table0%0/6
A and B and C and not(D) or E
A= $items >= $MIN_LIST_ITEMSB= $len < $maxC= $len > $minD= not $sel_lenE= $len * $PRIORITY < $sel_len
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
223 0 $sel_list_type = $cur_list_type;
224 0 $sel_len = $len;
225 }
226 }
227  
228 # return if no list type found
229 0 ▶ return $text if ( !$sel_list_type );
Branch: 1 branch
BranchTrueFalseTotal
unless $sel_list_type 0 0 0
230  
231 # list type detected, split by the given list type
232 0 my $sep = $sel_list_type->{sep};
233 0 my $neg = $sel_list_type->{neg_pre};
234 0 my $name = $sel_list_type->{name};
235  
236 # protect negative pre-context, if any is specified
237 0 ▶ $text =~ s/($neg)(?=$sep)/$1<<<NEG>>>/gi if ($neg);
Branch: 1 branch
BranchTrueFalseTotal
if $neg 0 0 0
238  
239 # split at the given list type
240 0 ▶ if ( $sel_list_type->{type} eq 'e' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel_list_type->{'type'} eq 'e') { } 0 0 0
241 0 $text =~ s/(?<!<<<NEG>>>)($sep)/$1\n/gi;
242 }
243 else {
244 0 $text =~ s/(?<!<<<NEG>>>)($sep)/\n$1/gi;
245 }
246  
247 # remove negative pre-context protection
248 0 $text =~ s/<<<NEG>>>//g;
249  
250 # delete too short splits
251 0 $text = $self->_join_too_short_segments($text);
252  
253 # return the split result
254 0 return split /\n/, $text;
255 }
256  
257 0 ▶sub _join_too_short_segments {
Subroutine _join_too_short_segments: not called CC=1 SCAR=6.9
258 0 my ( $self, $text ) = @_;
259  
260 0 $text =~ s/^\n//;
261 0 $text =~ s/\n$//;
262 0 $text =~ s/\n(?=\h*(\S+(\h+\S+){0,2})?\h*(\n|$))/ /g;
263 0 return $text;
264 }
265  
266 0 ▶sub split_at_any_punctuation {
Subroutine split_at_any_punctuation: not called CC=2 SCAR=17.9
Pod undocumented
267 0 my ( $self, $text ) = @_;
268  
269 0 my $closings = $self->closings;
270  
271 # prefer punctuation followed by a letter
272 0 $text =~ s/([,;!?–—-]+\s*[$closings]?)\s+(\p{Alpha})/$1\n$2/g;
273  
274 # delete too short splits
275 0 $text = $self->_join_too_short_segments($text);
276  
277 0 my @split = split /\n/, $text;
278  
279 # split at any punctuation if the text is still too long
280 return map {
281 0 ▶ $_ =~ s/([,;!?–—-]+\s*[$closings]?)/$1\n/g if ( $self->segment_too_long($_) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->segment_too_long($_) 0 0 0
282 0 split /\n/, $self->_join_too_short_segments($_)
283 } @split;
284 }
285  
286 0 ▶sub split_hard {
Subroutine split_hard: not called CC=3 SCAR=24.8
Pod undocumented
287 0 my ( $self, $text ) = @_;
288  
289 0 my @tokens = split /(\s+)/, $text;
290 0 my @result;
291 0 my $pos = 0;
292  
293 0 ▶ while ( $pos < @tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @tokens) 0 0 0
294 0 my $limit = $pos + $self->limit_words * 2 - 1;
295 0 ▶ $limit = @tokens - 1 if ( $limit > @tokens - 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $limit > @tokens - 1 0 0 0
296 0 push @result, join( '', @tokens[ $pos .. $limit ] );
297 0 $pos = $limit + 1;
298 }
299 0 return @result;
300 }
301  
302 4 1;
303  
304 __END__
« Block/Read/CoNLLX.pm ↑ Summary Tool/Lexicon/CS.pm »