| 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; | ||||||||||||||
| 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 Lingua::Sentence; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | ▶ | use Lingua::Poetry::Haiku::Finder::Sentence; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | my @LINE_LENGTHS = ( 5, 7, 5 ); | |||||||||||||
| 19 | 1 | my $HAIKU_LENGTH = sum0 @LINE_LENGTHS; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | has text => ( | |||||||||||||
| 22 | is => 'ro', | ||||||||||||||
| 23 | isa => Str, | ||||||||||||||
| 24 | required => !!1, | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | has sentences => ( | |||||||||||||
| 28 | is => 'lazy', | ||||||||||||||
| 29 | isa => ArrayRef[ InstanceOf[ __PACKAGE__ . '::Sentence' ] ], | ||||||||||||||
| 30 | init_arg => undef, | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 1 | has haikus => ( | |||||||||||||
| 34 | is => 'lazy', | ||||||||||||||
| 35 | isa => ArrayRef[ Str ], | ||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||
| 37 | ); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | has _splitter => ( | ||||||||||||||
| 40 | is => 'lazy', | ||||||||||||||
| 41 | isa => Object, | ||||||||||||||
| 42 | 1 | ▶ | builder => sub { 'Lingua::Sentence'->new("en") }, | ||||||||||||
|
Subroutine
_build__splitter: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 1 | handles => { | |||||||||||||
| 44 | '_split_array' => 'split_array', | ||||||||||||||
| 45 | }, | ||||||||||||||
| 46 | ); | ||||||||||||||
| 47 | |||||||||||||||
| 48 | { | ||||||||||||||
| 49 | 1 | my $class = __PACKAGE__ . '::Sentence'; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | ▶ | sub _build_sentences { | ||||||||||||
|
Subroutine
_build_sentences: called CC=1 SCAR=0.0
| |||||||||||||||
| 52 | 1 | my ( $self ) = ( shift ); | |||||||||||||
| 53 | |||||||||||||||
| 54 | 1 | my $text = $self->text; | |||||||||||||
| 55 | 1 | $text =~ s/\s+/ /sg; | |||||||||||||
| 56 | 1 | $text =~ s/^\s+//s; | |||||||||||||
| 57 | 1 | $text =~ s/\s+$//s; | |||||||||||||
| 58 | |||||||||||||||
| 59 | return [ | ||||||||||||||
| 60 | 1 | map $class->new( text => $_ ), $self->_split_array( $text ) | |||||||||||||
| 61 | ]; | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 1 | ▶ | sub _format_haiku { | ||||||||||||
|
Subroutine
_format_haiku: called CC=6 SCAR=17.9
| |||||||||||||||
| 66 | 3 | my ( $self, $sentences ) = ( shift, @_ ); | |||||||||||||
| 67 | my @parts = map { | ||||||||||||||
| 68 | 4 | my $end = ( __PACKAGE__ . '::NonWord' )->new( text => ' ' ); | |||||||||||||
| 69 | 4 | @{ $_->parts }, $end; | |||||||||||||
| 70 | 3 | } @{ $sentences }; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 3 | my @lines; | |||||||||||||
| 73 | 3 | my $current_line = 0; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 3 | ▶ | while ( @parts ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 84 | my $part = shift @parts; | |||||||||||||
| 77 | 84 | ▶ | if ( $part->is_word ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 40 | ▶ | my $current_line_length = sum0 map $_->syllables, @{ $lines[$current_line] || [] }; | ||||||||||||
|
Condition100%2/2
$lines[$current_line] || []
MC/DC100%1/1
$lines[$current_line] || []
$lines[$current_line]
Truth table100%2/2
A
A= $lines[$current_line]
| |||||||||||||||
| 79 | 40 | ▶ | if ( $current_line_length >= $LINE_LENGTHS[$current_line] ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 6 | ++$current_line; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | 84 | ▶ | push @{ $lines[$current_line] ||= [] }, $part; | ||||||||||||
|
Condition100%2/2
$lines[$current_line] ||= []
MC/DC100%1/1
$lines[$current_line] ||= []
$lines[$current_line]
Truth table100%2/2
A
A= $lines[$current_line]
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | return join "\n", map { | ||||||||||||||
| 87 | 3 | my @line = @$_; | |||||||||||||
| 88 | 9 | my $text = join "", map $_->text, @line; | |||||||||||||
| 89 | 9 | $text =~ s/\s+/ /sg; | |||||||||||||
| 90 | 9 | $text =~ s/^\s+//s; | |||||||||||||
| 91 | 9 | $text =~ s/\s+$//s; | |||||||||||||
| 92 | 9 | $text; | |||||||||||||
| 93 | } @lines; | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 1 | ▶ | sub _build_haikus { | ||||||||||||
|
Subroutine
_build_haikus: called CC=5 SCAR=16.1
| |||||||||||||||
| 97 | 1 | my ( $self ) = ( shift ); | |||||||||||||
| 98 | |||||||||||||||
| 99 | 1 | my @sentences = @{ $self->sentences }; | |||||||||||||
| 100 | 1 | my @found; | |||||||||||||
| 101 | 1 | STARTER: for my $start_index ( 0 .. $#sentences ) { | |||||||||||||
| 102 | 138 | ENDER: for my $end_index ( $start_index .. $#sentences ) { | |||||||||||||
| 103 | 176 | my @slice = @sentences[ $start_index .. $end_index ]; | |||||||||||||
| 104 | 176 | my $slice_syllables = sum0 map $_->syllables, @slice; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 176 | ▶ | if ( $slice_syllables == $HAIKU_LENGTH ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 107 | 3 | push @found, $self->_format_haiku( \@slice ); | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | elsif ( $slice_syllables > $HAIKU_LENGTH ) { | ||||||||||||||
| 110 | 138 | next STARTER; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 1 | return \@found; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 1 | ▶ | sub from_text { | ||||||||||||
|
Subroutine
from_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 119 | 1 | my ( $class, $text ) = ( shift, @_ ); | |||||||||||||
| 120 | 1 | return $class->new( text => $text ); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 1 | ▶ | sub from_filehandle { | ||||||||||||
|
Subroutine
from_filehandle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 124 | 1 | my ( $class, $fh ) = ( shift, @_ ); | |||||||||||||
| 125 | 1 | my $text = do { local $/; <$fh> }; | |||||||||||||
| 126 | 1 | return $class->from_text( $text ); | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 0 | ▶ | sub from_filename { | ||||||||||||
|
Subroutine
from_filename: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 130 | 0 | my ( $class, $filename ) = ( shift, @_ ); | |||||||||||||
| 131 | 0 | ▶ | open my $fh, '<', $filename or die "Cannot open $filename: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | return $class->from_filehandle( $fh ); | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 1 | 1; | |||||||||||||
| 136 | |||||||||||||||
| 137 | __END__ | ||||||||||||||