Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/07/30

A Cute Bit Of Talking Code

As I've previously mentioned, I have problems talking to computers but have few problems having them talk to me. I have a crontab on my work machine that runs every 15 minutes to tell me the time of day. ( Yes, my computer will give me the time of day. )

I found out quickly that just piping date into Festival won't make you happy. I wrote this to force a pronunciation. It's simple Perl, and it doesn't deal with Festival directly. But it gives me the pronunciation I want.

If it's useful to you, use in good health.

#!/usr/bin/perl

# */15 8-17 * * 1-5 ~/bin/curr_time.pl | /usr/bin/festival --tts &> /dev/null

use 5.010 ;
use strict ;
use warnings ;
use Data::Dumper ;
use subs qw( hour minute ampm ) ;
my @time = localtime ;
my @output ;

my @hour =  qw{
    Twelve One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve
    } ;

push @output , hour(@time) ;
push @output , minute(@time) ;
push @output , ampm(@time) ;
say join ' ' , @output ;

# -- hour -------------------
sub hour {
    my @time = @_ ;
    my $hour ;
    if    ( $time[2] % 12 == 0 ) { $hour = 12 ; }
    elsif ( $time[2] > 12 )      { $hour = $time[2] - 12 ; }
    else                         { $hour = $time[2] ; }
    return $hour[ $hour ];
    }

# -- minute -----------------
sub minute {
    my @time = @_ ;
    my $minute = sprintf '%d' , $time[1] ;
    if    ( $time[1] < 10 ) { $minute = 'oh ' . $minute ; }
    if    ( $time[1] == 0 ) { $minute = '' ; }
    return $minute;
    }

# -- a.m. - p.m. ------------
sub ampm {
    my @time = @_ ;
    my $m ;
    if    ( $time[2] == 0  && $time[1] == 0 )  { $m = 'Midnight' ; }
    elsif ( $time[2] == 12  && $time[1] == 0 ) { $m = 'Noon' ; }
    elsif ( $time[2] <  12   )                 { $m = 'A M' ; }
    else                                       { $m = 'P M' ; }
    return $m;
    }

3 comments:

  1. Sounds like a similar idea to DateTime::Format::Human (http://search.cpan.org/~jhoblitt/DateTime-Format-Human-0.01/)

    ReplyDelete
  2. I'll have to look into that.

    ReplyDelete
  3. Steve,

    having tested it, no. I want it to say "Two 27 P M" and not "a little after twenty-five past two in the afternoon". I just want to be sure it says "A.M." and not "am".

    ReplyDelete