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.

  1. #!/usr/bin/perl  
  2.   
  3. # */15 8-17 * * 1-5 ~/bin/curr_time.pl | /usr/bin/festival --tts &> /dev/null  
  4.   
  5. use 5.010 ;  
  6. use strict ;  
  7. use warnings ;  
  8. use Data::Dumper ;  
  9. use subs qw( hour minute ampm ) ;  
  10. my @time = localtime ;  
  11. my @output ;  
  12.   
  13. my @hour =  qw{  
  14.     Twelve One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve  
  15.     } ;  
  16.   
  17. push @output , hour(@time) ;  
  18. push @output , minute(@time) ;  
  19. push @output , ampm(@time) ;  
  20. say join ' ' , @output ;  
  21.   
  22. # -- hour -------------------  
  23. sub hour {  
  24.     my @time = @_ ;  
  25.     my $hour ;  
  26.     if    ( $time[2] % 12 == 0 ) { $hour = 12 ; }  
  27.     elsif ( $time[2] > 12 )      { $hour = $time[2] - 12 ; }  
  28.     else                         { $hour = $time[2] ; }  
  29.     return $hour$hour ];  
  30.     }  
  31.   
  32. # -- minute -----------------  
  33. sub minute {  
  34.     my @time = @_ ;  
  35.     my $minute = sprintf '%d' , $time[1] ;  
  36.     if    ( $time[1] < 10 ) { $minute = 'oh ' . $minute ; }  
  37.     if    ( $time[1] == 0 ) { $minute = '' ; }  
  38.     return $minute;  
  39.     }  
  40.   
  41. # -- a.m. - p.m. ------------  
  42. sub ampm {  
  43.     my @time = @_ ;  
  44.     my $m ;  
  45.     if    ( $time[2] == 0  && $time[1] == 0 )  { $m = 'Midnight' ; }  
  46.     elsif ( $time[2] == 12  && $time[1] == 0 ) { $m = 'Noon' ; }  
  47.     elsif ( $time[2] <  12   )                 { $m = 'A M' ; }  
  48.     else                                       { $m = 'P M' ; }  
  49.     return $m;  
  50.     }  

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