Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Before I re-invent something - I figure I should ask first.
I want to find code that will "speak" a large number.
112,458 -> "one hundred twelve thousand, four hundred and fifty eight"
I'll likely record the individual sound bites for "one", "two", "three", "hundred", "thousand", etc. and string them together for full effect.
Cheers,
TLC
Re: Number to Speech
by Kozz (Friar) on Aug 22, 2000 at 09:13 UTC
|
| [reply] |
|
Number::Spell looks like a good module for this sort of thing. But do you know of any modules that will handle ordinals?
Number::Spell spells out cardinals.
(For the numerically impaired... cardinals count items (as in
"one, two, three"), ordinals order items (as in "first, second,
third"). You can tell cardinals by their red robes...)
UPDATE: Not that I'm complaining, but... I just noticed that this comment has a fairly decent reputation, higher than most of my comments, and equal to what I feel is a deserving comment posted on another thread Re: Search for dupe && append char if found.
This comment was a smart-assed, throwaway comment (or at least, I intended it to be). Why is it so highly rated?
I'm not trolling for votes, just curious.
| [reply] |
|
If you've got the ordinal then the cardinal is relatively easy, just roll your own. something like:
#!/usr/bin/perl -w
use strict;
my @examples = ("one", "two", "three", "nine", "twenty",
"one hundred and three", "one thousand",
"one thousand two hundred",
"eighth thousand seven hundred and fifty seven");
for my $cardinal (@examples) {
my $ordinal = ordinalise($cardinal);
print "$ordinal\n";
};
sub ordinalise {
my $str = shift;
my @arr = split " ", $str;
my $word = pop @arr;
my %ord = ("one" => "first", "two" => "second",
"three" => "third", "four" => "fourth",
"five" => "fifth", "six" => "sixth",
"seven" => "seventh", "eight" => "eighth",
"nine" => "ninth", "ten" => "tenth",
"eleven" => "eleventh", "twelve" => "twelfth");
{
$word =~ s/ty$/tieth/ and next;
$word = $ord{$word} and next if defined $ord{$word};
$word .= "th";
}
push @arr, $word;
return join(" ", @arr);
};
Nuance
| [reply] [d/l] |
RE: Number to Speech
by merlyn (Sage) on Aug 22, 2000 at 15:29 UTC
|
use Lingua::EN::Nums2Words;
print num2word_ordinal(1251), "\n";
which yields 'ONE THOUSAND, TWO HUNDRED FIFTY-FIRST'.
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
|
Look at the categorization of those modules. How easy would they have been to find? Regardless of what the actual names for them were, it would have been ideal for them to have the following logical names to facilitate searching:
- Number::Spell::Cardinal
- Number::Spell::Ordinal
I am always keeping track of the difficulties in finding things in the Perl community and appreciate this post by merlyn, which adds yet another rung to my soapbox...
Of course, the bigger question is how best to solve this quandry, and I think the best thing is to simply keep observing the difficulties and eventually flesh out an answer as more detail develops.
| [reply] |
|
This is a bigger problem that probably deserves either
a full front page posting of it's own or ever it's own
website.
At the very least a keyword table for every module
to give the searcher a better chance of finding the right
one would help. Maybe a keywords.txt added to every module
tar that CPAN websites and scripts could parse out. Or a
POD "=begin keyword" section.
What you mention is a more appealing and yet dangerous
approach, actually listing modules under aliases in CPAN.
It pollutes the namespace horribly but requires only a
minor change in current behavior.
Of course the ideal fix would be to start a site where
you could ask other experts what modules might best fit
your needs... =P
--
$you = new YOU;
honk() if $you->love(perl)
| [reply] |
Re: Number to Speech
by lhoward (Vicar) on Aug 22, 2000 at 23:12 UTC
|
As the author of Number::Spell
I figure I should put my 2 cents in.
When I created Number::Spell it was
only after searching CPAN and posting a
description of my idea to comp.lang.perl.modules.
Nowhere in this process did I discover
Lingua::EN::Nums2Words or any module
that even touched on the problem.
Though Number::Spell is fairly basic as
as far as options go; I have been working
on a number of features including:
- multilanguage support
- support for ordinal vs cardinal numbers
- support (in English anyway) for optional "and" in
the spelling at the apropriate spot
(i.e. "five hundred thriry two" vs. "five hundred and thirty two")
Number::Spell already supports "European English"
formatting of large numbers (i.e. 2e+11 is "twenty billion"
in American English, but "twenty thousand million"
in European English). | [reply] |
|
lhoward deserves many extra reputation points for realising that not all "english" speakers drop important conjunctions. More recognition to the forgotten "and".
| [reply] |
(jcwren) RE: Number to Speech
by jcwren (Prior) on Aug 22, 2000 at 17:33 UTC
|
You didn't mention how you might record the sound bites. As a suggestion, check out Festival/MBROLA. I use it to convert my weather data to a .WAV and .MP3 file, so that it may be played out over an Amateur Radio transciever on request.
If you'd like to hear the current weather, click here for a .WAV file (about 580K), or here for a .MP3 file (about 170K). And finally, here is a link to a page that will let you type in your own phrases and return them as a .WAV file.
And this is a link the Festival/MBROLA software.
--Chris
e-mail jcwren | [reply] |
|
| [reply] |
|
This sounds like an excellent candidate for CUFP. (IMHO)
cheers,
ybiC
| [reply] |
Re: Number to Speech
by elwarren (Priest) on Aug 22, 2000 at 22:45 UTC
|
When running perl under a windows platform you can use the Win32::Sound code to play a wav file. It's very simple, here's a snip from the docs:
use Win32::Sound;
Win32::Sound::Volume('100%');
Win32::Sound::Play("file.wav");
Win32::Sound::Stop();
I've also seen code to insert commas into a number, which could help you to distinguish between saying hundred and thousand. I can't seem to locate it right now, but I'll post it if I find it.
HTH | [reply] [d/l] |
RE: Number to Speech
by spacemunky (Initiate) on Aug 22, 2000 at 23:36 UTC
|
A program called Festival already does this, you can probably find it on freshmeat | [reply] |
|
|