Ella has asked for the wisdom of the Perl Monks concerning the following question:
So I'm being asked to generate a fairly large number of small text files (about 1 sentence long). The words can be the same every time, but each sentence has to contain a different bunch of numbers. It would look a bit like this:
"The lucky numbers of the day are _1, _2, _3, _4, _5 and don't forget to play again next week."The aim of this is to test the performance of a recently developed text to speech synthesizer.
Anyway, this is what I've come up with so far (with some help from a friend). It kind of works a bit anyway (well the 2 number generator works, I'm still 'debugging' a.k.a. 'fixing my bad code' on the 5 number generator). I'd appreciate it if the monks have any suggestions for me.
use strict; my $sentence_file = shift (@ARGV); my $numbers_file = shift (@ARGV); open SENTENCE, "$sentence_file" || die "cannot open $sentence_file for + reading: $!"; open NUMBERS, "$numbers_file" || die "cannot open $numbers_file for re +ading: $!"; my $sentence = (<SENTENCE>)[0]; print "sentence: $sentence\n"; my @numbers = <NUMBERS>; foreach my $number (@numbers) { my $working_sentence = $sentence; chomp $number; $number =~ s/\.$//g; my $output_file; if ($working_sentence =~ /_5/) { my $second_number = shift @numbers; my $third_number = shift @numbers; my $fourth_number = shift @numbers; my $fifth_number = shift @numbers; $working_sentence =~ s/_5/$number $second_number $third_number $four +th_number $fifth_number/; $output_file = "number.test5.".$number."_".$fifth_number.".txt"; } elsif ($working_sentence =~ /_2/) { my $second_number = shift @numbers; $working_sentence =~ s/_2/$number $second_number/; $output_file = "number.test2.".$number."_".$second_number.".txt"; } open OUTPUT, ">$output_file" || die "cannot open $output_file for wr +iting: $!"; print OUTPUT $working_sentence."\n"; close OUTPUT; }
I'm sorry - this could be formatted better, (and be a less stupid question) I'm still feeling my way a bit...ok, a lot! :p
'share and enjoy'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sentence generation
by blokhead (Monsignor) on Jul 22, 2003 at 23:32 UTC | |
|
Re: sentence generation
by LAI (Hermit) on Jul 23, 2003 at 13:27 UTC |