File Coverage

blib/lib/Lingua/Poetry/Haiku/Finder/Sentence.pm
Criterion Covered Total %
statement 56 57 98.2
branch 5 6 83.3
condition n/a
subroutine 15 15 100.0
pod n/a
total 76 78 97.4


line stmt bran cond sub pod time code
1 1     1   21 use 5.012;
  1         4  
2 1     1   12 use strict;
  1         2  
  1         21  
3 1     1   5 use warnings;
  1         2  
  1         56  
4              
5             package Lingua::Poetry::Haiku::Finder::Sentence;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.002';
9              
10 1     1   6 use Moo;
  1         1  
  1         10  
11 1     1   357 use Types::Standard -types;
  1         2  
  1         17  
12 1     1   5247 use List::Util 'sum0';
  1         3  
  1         69  
13 1     1   7 use namespace::autoclean;
  1         2  
  1         18  
14              
15 1     1   560 use Lingua::Poetry::Haiku::Finder::SentencePart;
  1         3  
  1         32  
16 1     1   468 use Lingua::Poetry::Haiku::Finder::Word;
  1         3  
  1         73  
17 1     1   488 use Lingua::Poetry::Haiku::Finder::NonWord;
  1         2  
  1         872  
18              
19             has text => (
20             is => 'ro',
21             isa => Str,
22             required => !!1,
23             );
24              
25             has parts => (
26             is => 'lazy',
27             isa => ArrayRef[ ConsumerOf[ __PACKAGE__ . 'Part' ] ],
28             init_arg => undef,
29             );
30              
31             has syllables => (
32             is => 'lazy',
33             isa => Int,
34             init_arg => undef,
35             );
36              
37             sub _build_parts {
38 138     138   1923 my ( $self ) = ( shift );
39 138         736 return [ $self->_find_parts( $self->text ) ];
40             }
41              
42             {
43             ( my $class = __PACKAGE__ ) =~ s/::Sentence/::Word/;
44            
45             sub _found_word {
46 3956     3956   8442 my ( $self, $text ) = ( shift, @_ );
47 3956         71935 return $class->new( text => $text );
48             }
49             }
50              
51             {
52             ( my $class = __PACKAGE__ ) =~ s/::Sentence/::NonWord/;
53            
54             sub _found_nonword {
55 3973     3973   8636 my ( $self, $text ) = ( shift, @_ );
56 3973         73691 return $class->new( text => $text );
57             }
58             }
59              
60             sub _find_parts {
61 138     138   454 my ( $self, $text ) = ( shift, @_ );
62 138         320 my @parts;
63            
64 138         423 local $_ = $text;
65 138         2247 s/\s+/ /sg;
66 138         418 s/^\s+//s;
67 138         877 s/\s+$//s;
68            
69 138         483 while ( length ) {
70 7922 100       306436 if ( /^([^_\W][\w-]*)/ ) {
    50          
71 3956         8767 my $word = $1;
72 3956         7976 substr( $_, 0, length $word ) = '';
73 3956         9306 push @parts, $self->_found_word( $word );
74             }
75             elsif ( /^([_\W]*)/ ) {
76 3966         9315 my $nonword = $1;
77 3966         7928 substr( $_, 0, length $nonword ) = '';
78 3966 100       7484 if ( $nonword =~ /^(\S+)(\s+.+)$/ ) {
79 7         28 my ( $first, $second) = ( $1, $2 );
80 7         22 push @parts, $self->_found_nonword( $first ), $self->_found_nonword( $second );
81             }
82             else {
83 3959         9303 push @parts, $self->_found_nonword( $nonword );
84             }
85             }
86             else {
87 0         0 die 'Unexpected!';
88             }
89             }
90            
91 138         9446 return @parts;
92             }
93              
94             sub _build_syllables {
95 138     138   2143 my ( $self ) = ( shift );
96 138         389 return sum0 map $_->syllables, @{ $self->parts };
  138         2596  
97             }
98              
99             1;