Block/W2A/Tokenize.pm

filter: Statementstmt 31.8% 14 / 44 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod n/a totaltotal 32.7% 18 / 55 CC 3 SCAR 12.0
CC
3
Cov
29%
_mark_urls
6.0
_restore_urls
6.0
__ANON__
2.0
CRAP 3.3
« Block/Read/BaseAlignedTextReader.pm ↑ Summary Tool/Segment/RuleBased.pm »
1 package Treex::Block::W2A::Tokenize;
2 BEGIN {
3 1 ▶ $Treex::Block::W2A::Tokenize::VERSION = '0.06903_1';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'Treex::Block::W2A::TokenizeOnWhitespace';
9  
10 override 'tokenize_sentence' => sub {
11 0 ▶ my ( $self, $sentence ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
12  
13 # first off, add a space to the beginning and end of each line, to reduce necessary number of regexps.
14 0 $sentence =~ s/$/ /;
15 0 $sentence =~ s/^/ /;
16  
17 # detect web sites and email addresses and protect them from tokenization
18 0 $sentence = $self->_mark_urls($sentence);
19  
20 # the following characters (double-characters) are separated everywhere
21 0 $sentence =~ s/(;|!|<|>|\{|\}|\[|\]|\(|\)|\?|\#|\$|£|\%|\&|``|\'\'|‘‘|"|“|”|«|»|--|—|„|‚|\*|\^)/ $1 /g; ## no critic (RegularExpressions::ProhibitComplexRegexes) this is not complex
22  
23 # short hyphen is separated if it is followed or preceeded by non-alphanuneric character and is not a part of --
24 0 $sentence =~ s/([^\-\w])\-([^\-])/$1 - $2/g;
25 0 $sentence =~ s/([^\-])\-([^\-\w])/$1 - $2/g;
26  
27 # apostroph is separated if it is followed or preceeded by non-alphanumeric character, is not part of '', and is not followed by a digit (e.g. '60).
28 0 $sentence =~ s/([^\'’\w])([\'’])([^\'’\d])/$1 $2 $3/g;
29 0 $sentence =~ s/([^\'’])([\'’])([^\'’\w])/$1 $2 $3/g;
30  
31 # dot, comma, slash, and colon are separated if they do not connect two numbers
32 0 $sentence =~ s/(\D|^)([\.,:\/])/$1 $2 /g;
33 0 $sentence =~ s/([\.,:\/])(\D|$)/ $1 $2/g;
34  
35 # three dots belong together
36 0 $sentence =~ s/\.\s*\.\s*\./.../g;
37  
38 # get back web sites and e-mails
39 0 $sentence = $self->_restore_urls($sentence);
40  
41 # clean out extra spaces
42 0 $sentence =~ s/\s+/ /g;
43 0 $sentence =~ s/^ *//g;
44 0 $sentence =~ s/ *$//g;
45  
46 0 return $sentence;
47 1 };
48  
49 1 has _urls => ( is => 'rw' );
50  
51 # internally marks URLs, so they won't be splitted
52 sub _mark_urls {
53 0 ▶ my ( $self, $sentence ) = @_;
Subroutine _mark_urls: not called CC=2 SCAR=17.9
54 0 my @urls;
55 0 ▶ while ( $sentence =~ s/(\W)((http:\/\/)?([\w\-]+\.)+(com|cz|de|es|eu|fr|hu|it|sk))(\W)/$1 XXXURLXXX $6/ ) { ## no critic (RegularExpressions::ProhibitComplexRegexes) this is not complex
Branch: 1 branch
BranchTrueFalseTotal
if ($sentence =~ s[(\W)((http://)?([\w\-]+\.)+(com|cz|de|es|eu|fr|hu|it|sk))(\W)][$1 XXXURLXXX $6]) 0 0 0
56 0 push @urls, $2;
57 }
58 0 $self->_set_urls( \@urls );
59 0 return $sentence;
60 }
61  
62 # pushes bask URLs, marked by C<_mark_urls>
63 sub _restore_urls {
64 0 ▶ my ( $self, $sentence ) = @_;
Subroutine _restore_urls: not called CC=2 SCAR=17.9
65 0 my @urls = @{ $self->_urls };
66 0 ▶ while (@urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@urls) 0 0 0
67 0 my $url = shift @urls;
68 0 $sentence =~ s/XXXURLXXX/$url/;
69 }
70 0 return $sentence;
71 }
72  
73 1 1;
74  
75 __END__
76  
77 =encoding utf-8
78  
79 =head1 NAME
80  
81 Treex::Block::W2A::Tokenize - language independent rule based tokenizer
82  
83 =head1 VERSION
84  
85 =head1 DESCRIPTION
86  
87 Each sentence is split into a sequence of tokens using a series of regexs.
88 Flat a-tree is built and attributes C<no_space_after> are filled.
89 This class uses language-independent regex rules for tokenization,
90 but it can be used as an ancestor for language-specific tokenization
91 by overriding the method C<tokenize_sentence>
92 or by using C<around> (see L<Moose::Manual::MethodModifiers>).
93  
94 =head1 AUTHOR
95  
96 David Mareček <marecek@ufal.mff.cuni.cz>
97  
98 Martin Popel <popel@ufal.mff.cuni.cz>
99  
100 =head1 COPYRIGHT AND LICENSE
101  
102 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
103  
104 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
105  
« Block/Read/BaseAlignedTextReader.pm ↑ Summary Tool/Segment/RuleBased.pm »