Block/W2A/Tokenize.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 100% 75 / 75 CC 2 SCAR 1.5
CC
2
Cov
100%
_restore_urls
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Block/Read/BaseAlignedTextReader.pm ↑ Summary Block/W2A/Segment.pm »
1 package Treex::Block::W2A::Tokenize;
2 2 $Treex::Block::W2A::Tokenize::VERSION = '2.20151102';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 extends 'Treex::Block::W2A::TokenizeOnWhitespace';
9  
10 # On some inputs we get "Malformed utf8 character" warnings, but only with Perl 5.12, not with 5.14.
11 # Let's suppress all utf8 warnings. TODO: find the real cause.
12 2 ▶no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶override 'tokenize_sentence' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15 2 my ( $self, $sentence ) = @_;
16  
17 # first off, add a space to the beginning and end of each line, to reduce necessary number of regexps.
18 2 $sentence =~ s/$/ /;
19 2 $sentence =~ s/^/ /;
20  
21 # The following characters (double-characters) are separated everywhere and cannot be used inside URLs.
22 # It is quite common than an URL is enclosed in quotes
23 # and URI::Find::Schemeless does not recognize such URLs, so we need to separate it first.
24 2 $sentence =~ s/(["<>{}“”«»–|—„‚‘]|\[|\]|``|\'\'|‘‘|\^)/ $1 /g;
25  
26 # detect web sites and email addresses and protect them from tokenization
27 2 $sentence = $self->_mark_urls($sentence);
28  
29 # the following characters (double-characters) are separated everywhere (except inside URLs)
30 2 $sentence =~ s/([;!()?#\$£%&*…]|--)/ $1 /g;
31  
32 # short hyphen is separated if it is followed or preceeded by non-alphanuneric character and is not a part of --, or a unary minus
33 2 $sentence =~ s/([^\-\w])\-([^\-0-9])/$1 - $2/g;
34 2 $sentence =~ s/([0-9]\s+)\-([0-9])/$1 - $2/g; # preceded by a number - not a unary minus
35 2 $sentence =~ s/([^\-])\-([^\-\w])/$1 - $2/g;
36  
37 # plus is separated everywhere, except at the end of a word (separated by a space) and as unary plus
38 2 $sentence =~ s/(\w)\+(\w)/$1 + $2/g;
39 2 $sentence =~ s/([0-9]\s*)\+([0-9])/$1 + $2/g;
40 2 $sentence =~ s/\+([^\w\+])/+ $1/g;
41  
42 # 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).
43 2 $sentence =~ s/([^\'’\w])([\'’])([^\'’\d])/$1 $2 $3/g;
44 2 $sentence =~ s/([^\'’])([\'’])([^\'’\w])/$1 $2 $3/g;
45  
46 # dot, comma, slash, and colon are separated if they do not connect two numbers
47 2 $sentence =~ s/(\D|^)([\.,:\/])/$1 $2 /g;
48 2 $sentence =~ s/([\.,:\/])(\D|$)/ $1 $2/g;
49  
50 # three dots belong together
51 2 $sentence =~ s/\.\s*\.\s*\./.../g;
52  
53 # get back web sites and e-mails
54 2 $sentence = $self->_restore_urls($sentence);
55  
56 # clean out extra spaces
57 2 $sentence =~ s/\s+/ /g;
58 2 $sentence =~ s/^ *//g;
59 2 $sentence =~ s/ *$//g;
60  
61 2 return $sentence;
62 2 };
63  
64 2 ▶use URI::Find::Schemeless;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 my @urls;
66 1 ▶my $finder = URI::Find::Schemeless->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 my ($uri, $orig_uri) = @_;
68 1 push @urls, $orig_uri;
69 1 return 'XXXURLXXX';
70 2 });
71  
72 # internally marks URLs, so they won't be splitted
73 2 ▶sub _mark_urls {
Subroutine _mark_urls: called CC=1 SCAR=0.0
74 2 my ( $self, $sentence ) = @_;
75 2 @urls = ();
76 # URI::Find::Schemeless cannot detect URLs which are not preceded by a space (or beggining of string).
77 # Various types of brackets were already separated, but it is possible to find e.g. "go to:http://mysite.com"
78 # without the space after colon (or comma, or other word). Let's detect such URLs as well.
79 2 $sentence =~ s{(?<!\s)(https?://|www\.)}{ $1}g;
80 2 $finder->find(\$sentence);
81 2 return $sentence;
82 }
83  
84 # pushes bask URLs, marked by C<_mark_urls>
85 2 ▶sub _restore_urls {
Subroutine _restore_urls: called CC=2 SCAR=6.9
86 2 my ( $self, $sentence ) = @_;
87 2 ▶ while (@urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@urls) 1 2 3
88 1 my $url = shift @urls;
89 1 $sentence =~ s/XXXURLXXX/$url/;
90 }
91 2 return $sentence;
92 }
93  
94 2 1;
95  
96 __END__
« Block/Read/BaseAlignedTextReader.pm ↑ Summary Block/W2A/Segment.pm »