Book-Collate

 view release on metacpan or  search on metacpan

lib/Book/Collate/Section.pm  view on Meta::CPAN

our $VERSION = 'v0.0.2';


=head1 SYNOPSIS

Section Object

    use Section;

    my $section = Section->new(
      raw_data    => "TITLE: An odd event
      [1429.123.0457] Nowhere
      Al looked again, and he was there.",
      has_header  => 1,
      number      => 1,
      file        => '1429_123_0457_nowhere',
    );

=head1 SUBROUTINES/METHODS

=head2 new

new takes a hash of data, to include: the section number ('number') if relevant,
and the raw_data from the file.

It uses the raw_data, and a "has_header" flag to create the section header.

The section's title is set by the first data line beginning with "TITLE:".

=cut

sub new {
  my ($class, %data) = @_;
  my $self = {
    _has_header     => $data{has_header} || 0,
    _header         => undef,
    _headless_data  => undef,
    _number         => $data{number},
    _raw_data       => $data{raw_data},
    _report         => undef,
    _title          => undef,
    _filename       => $data{filename},
  };
  bless $self, $class;
  $self->{_raw_data}      = $self->_trim($self->{_raw_data});
  $self->{_title}         = $self->_pull_title($self->{_raw_data});
  $self->{_headless_data} = $self->_write_headless_data($self->{_raw_data});
  $self->_write_report($self->{_headless_data});
  return $self;
}

=head2 _trim

Removes leading and trailing whitespace. Note that this also removes
the final newline.

=cut

sub _trim {
  my ( $self, $string )  = @_;
  my $caller = caller();
  ( my $new_string ) = $string =~ m/^\s*(\S.*\S)\s*$/s;
  return $new_string;
}

=head2 avg_sentence_length

Returns the average sentence length.

=cut
 
sub avg_sentence_length { $_[0]->{_report}->avg_sentence_length };

=head2 avg_word_length

Returns the average word length.

=cut

sub avg_word_length {
  my ( $self ) = @_;
  return $self->{_report}->avg_word_length;
}

=head2 filename

Returns the filename of the section.

=cut

sub filename { return $_[0]->{_filename}; }


=head2 grade_level

Returns the Flesch-Kincaid Grade Level score per: 
  https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests

=cut

sub grade_level {
  my ($self)  = @_;
  return $self->{_report}->grade_level();
}

=head2 header

Returns the section header, if appropriate, or undef.

=cut

sub header {
  my ($self)  = @_;
  return undef unless $self->{_has_header};
  my (@lines) = split(/\n/, $self->raw_data() );
  foreach my $line (@lines) {
    next if $line =~ m/TITLE:/;
    chomp($line);
    $line =~ s/^\s*//;
    next unless length($line);  # Skips extra blank lines at start.
    $self->{_header}  = $self->_trim($line);



( run in 2.380 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )