Tool/Segment/RuleBased.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 93.8% 46 / 49 CC 3 SCAR 4.8
CC
3
Cov
92%
get_segments
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« Block/W2A/Tokenize.pm ↑ Summary Unilang.pm »
1 package Treex::Tool::Segment::RuleBased;
2 BEGIN {
3 3 ▶ $Treex::Tool::Segment::RuleBased::VERSION = '0.06903_1';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #TODO: suspicious, Tool extends Block
9 3 extends 'Treex::Block::W2A::SegmentOnNewlines';
10  
11 3 has use_paragraphs => (
12 is => 'ro',
13 isa => 'Bool',
14 default => 1,
15 documentation =>
16 'Should paragraph boundaries be preserved as sentence boundaries?'
17 . ' Paragraph boundary is defined as two or more consecutive newlines.',
18 );
19  
20 3 has use_lines => (
21 is => 'ro',
22 isa => 'Bool',
23 default => 0,
24 documentation =>
25 'Should newlines in the text be preserved as sentence boundaries?'
26 . '(But if you want to detect sentence boundaries just based on newlines'
27 . ' and nothing else, use rather W2A::SegmentOnNewlines.)',
28 );
29  
30 # Tokens that usually do not end a sentence even if they are followed by a period and a capital letter:
31 # * single uppercase letters serve usually as first name initials
32 # * in langauge-specific descendants consider adding
33 # * period-ending items that never indicate sentence breaks
34 # * titles before names of persons etc.
35 #
36 # Note, that we cannot write
37 # sub get_unbreakers { return qr{...}; }
38 # because we want the regex to be compiled just once, not on every method call.
39 3 my $UNBREAKERS = qr{\p{Upper}};
40  
41 sub unbreakers {
42 1 ▶ return $UNBREAKERS;
Subroutine unbreakers: called CC=1 SCAR=0.0
Pod documented
43 }
44  
45 # Characters that can appear after period (or other end-sentence symbol)
46 sub closings {
47 1 ▶ return '"”»)';
Subroutine closings: called CC=1 SCAR=0.0
Pod documented
48 }
49  
50 # Characters that can appear before the first word of a sentence
51 sub openings {
52 1 ▶ return '"“«(';
Subroutine openings: called CC=1 SCAR=0.0
Pod documented
53 }
54  
55 sub get_segments {
56 1 ▶ my ( $self, $text ) = @_;
Subroutine get_segments: called CC=3 SCAR=11.1
Pod documented
57  
58 # Pre-processing
59 1 my $unbreakers = $self->unbreakers;
60 1 $text =~ s/\b($unbreakers)\./$1<<<DOT>>>/g;
61  
62 # two newlines usually separate paragraphs
63 1 ▶ if ( $self->use_paragraphs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_paragraphs) 1 0 1
64 1 $text =~ s/([^.!?])\n\n+/$1<<<SEP>>>/gsm;
65 }
66  
67 1 ▶ if ( $self->use_lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_lines) 0 1 1
68 0 $text =~ s/\n/<<<SEP>>>/gsm;
69 }
70  
71 # Normalize whitespaces
72 1 $text =~ s/\s+/ /gsm;
73  
74 # This is the main regex
75 1 my ( $openings, $closings ) = ( $self->openings, $self->closings );
76 1 $text =~ s{
77 ([.?!]) # $1 = end-sentence punctuation
78 ([$closings]?) # $2 = optional closing quote/bracket
79 \s # space
80 ([$openings]?\p{Upper}) # $3 = uppercase letter (optionally preceded by opening quote)
81 }{$1$2\n$3}gsxm;
82  
83 # Post-processing
84 1 $text =~ s/<<<SEP>>>/\n/gsmx;
85 1 $text =~ s/<<<DOT>>>/./gsxm;
86 1 $text =~ s/\s+$//gsxm;
87 1 $text =~ s/^\s+//gsxm;
88  
89 1 return split /\n/, $text;
90 }
91  
92 3 1;
93  
94 __END__
95  
96 =encoding utf-8
97  
98 =head1 NAME
99  
100 Treex::Tool::Segment::RuleBased - Rule based pseudo language-independent sentence segmenter
101  
102 =head1 VERSION
103  
104 =head1 DESCRIPTION
105  
106 Sentence boundaries are detected based on a regex rules
107 that detect end-sentence punctuation ([.?!]) followed by a uppercase letter.
108 This class is implemented in a pseudo language-independent way,
109 but it can be used as an ancestor for language-specific segmentation
110 by overriding the method C<segment_text>
111 (using C<around> see L<Moose::Manual::MethodModifiers>)
112 or just by overriding methods C<unbreakers>, C<openings> and C<closings>.
113  
114 See L<Treex::Block::W2A::EN::Segment>
115  
116 =head1 METHODS
117  
118 =over 4
119  
120 =item get_segments
121  
122 Returns list of sentences
123  
124 =back
125  
126 =head1 METHODS TO OVERRIDE
127  
128 =over 4
129  
130 =item segment_text
131  
132 Do segmentation
133  
134 =item unbreakers
135  
136 Returns regex that should match tokens that usually do not end a sentence even if they are followed by a period and a capital letter:
137 * single uppercase letters serve usually as first name initials
138 * in langauge-specific descendants consider adding
139 * period-ending items that never indicate sentence breaks
140 * titles before names of persons etc.
141  
142 =item openings
143  
144 Returns string with characters that can appear before the first word of a sentence
145  
146 =item closings
147  
148 Returns string with characters that can appear after period (or other end-sentence symbol)
149  
150 =back
151  
152 =head1 AUTHOR
153  
154 Martin Popel <popel@ufal.mff.cuni.cz>
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
159  
160 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
161  
« Block/W2A/Tokenize.pm ↑ Summary Unilang.pm »