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