| 1 | package Treex::Block::W2A::TokenizeOnWhitespace; | ||||||||||||||||||||||||||||
| 2 | 3 | $Treex::Block::W2A::TokenizeOnWhitespace::VERSION = '2.20151102'; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | extends 'Treex::Core::Block'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub tokenize_sentence { | ||||||||||||||||||||||||||
|
Subroutine
tokenize_sentence: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 0 | my ( $self, $sentence ) = @_; | |||||||||||||||||||||||||||
| 12 | 0 | return $sentence; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub process_zone { | ||||||||||||||||||||||||||
|
Subroutine
process_zone: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 1 | my ( $self, $zone ) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # create a-tree | ||||||||||||||||||||||||||||
| 19 | 1 | my $a_root = $zone->create_atree(); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # get the source sentence and tokenize | ||||||||||||||||||||||||||||
| 22 | 1 | my $sentence = $zone->sentence; | |||||||||||||||||||||||||||
| 23 | 1 | $sentence =~ s/^\s+//; | |||||||||||||||||||||||||||
| 24 | 1 | ▶ | log_fatal("No sentence to tokenize!") if !defined $sentence; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 1 | my @tokens = split( /\s/, $self->tokenize_sentence($sentence) ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Create a-nodes and detect the no_space_after attribute. | ||||||||||||||||||||||||||||
| 28 | 1 | foreach my $i ( ( 0 .. $#tokens ) ) { | |||||||||||||||||||||||||||
| 29 | 4 | my $token = $tokens[$i]; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # By default, there is a space after each token. | ||||||||||||||||||||||||||||
| 32 | 4 | my $no_space_after = 0; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Delete the token from the begining of the sentence. | ||||||||||||||||||||||||||||
| 35 | 4 | ▶ | if ($sentence =~ s/^\Q$token\E//){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 36 | # This is the expected case. The sentence starts with the $token. | ||||||||||||||||||||||||||||
| 37 | # If it is followed by a space, delete the space and set no_space_after=1. | ||||||||||||||||||||||||||||
| 38 | 3 | ▶ | $no_space_after = 1 if length($sentence) && $sentence !~ s/^\s+//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $sentence and not $sentence =~ s/^\s+//
MC/DC100%2/2
length $sentence and not $sentence =~ s/^\s+//
length $sentence
not $sentence =~ s/^\s+//
Truth table100%3/3
A and not(B)
A= length $sentenceB= not $sentence =~ s/^\s+//
| |||||||||||||||||||||||||||||
| 39 | } elsif ($i < $#tokens) { | ||||||||||||||||||||||||||||
| 40 | # The token (returned from tokenization) does not match the start of sentence. | ||||||||||||||||||||||||||||
| 41 | # E.g. '. . . word' is tokenized as '... word'. | ||||||||||||||||||||||||||||
| 42 | # Let's delete the start of sentence anyway, | ||||||||||||||||||||||||||||
| 43 | # using a non-greedy regex and the expected next token returned from the tokenization. | ||||||||||||||||||||||||||||
| 44 | 1 | my $next_token = $tokens[$i+1]; | |||||||||||||||||||||||||||
| 45 | 1 | my ($first, $rest) = ($sentence =~ /^(.*?)(\Q$next_token\E.*)$/); | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | $no_space_after = 1 if $first !~ /\s$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | $sentence = $rest; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # create new a-node | ||||||||||||||||||||||||||||
| 51 | $a_root->create_child( | ||||||||||||||||||||||||||||
| 52 | 4 | form => $token, | |||||||||||||||||||||||||||
| 53 | no_space_after => $no_space_after, | ||||||||||||||||||||||||||||
| 54 | ord => $i + 1, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 1 | return 1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||