How do I perform an ascending sort of a word list, based upon how many syllables each word contains?
Example Input:
something important dog cat bookshelf Example Output:
dog cat something bookshelf important This perl script builds a hash with words (read one per line from stdin, and/or from any filenames listed on the command line) as keys, and syllable counts as the values.
Then it prints the hash keys, sorted by the syllable counts.
#! /usr/bin/perl use strict; use Lingua::EN::Syllable; my %words = (); while(<>) { chomp; $words{$_} = syllable($_); }; print join("\n",sort { $words{$a} <=> $words{$b} } keys(%words)), "\n"; Output:
cat dog bookshelf something important If you want to print the syllable count along with each word, change the last line to something like this:
foreach my $word (sort { $words{$a} <=> $words{$b} } keys(%words)) { printf "%2i: %s\n", $words{$word}, $word; }; Output:
1: cat 1: dog 2: bookshelf 3: something 3: important This version highlights the fact that, as the module itself claims, it only estimates the syllable count. "bookshelf" is correctly counted as having only two syllables but "something" should also be two.
I haven't examined the module code closely, but it's probably getting confused by the e after the m. In many (most?) words, that wouldn't be a silent e and would count as an extra syllable.
perl - there are CPAN modules to do pretty nearly anything you can think of, so many programs that would be very complicated to write in other languages are as trivial as this to write in perl - because much of the work has already been done by the module authors....all you have to do is use the tool(s) they provide. Often, the hardest part is finding the module that does what you need - in this case, it took about 30 seconds to do a google search for perl count syllables Lingua::, some of them may be of use to you: search.cpan.org/…
perlhas a module calledLingua::EN::Syllablewhich "estimates the number of syllables in the word passed to it." search.cpan.org/~neilb/Lingua-EN-Syllable-0.30 (packaged for debian asliblingua-en-syllable-perl)