5

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 
5
  • 2
    Sorting is easy; do you have something that counts syllables? Commented May 2, 2016 at 1:51
  • 2
    perl has a module called Lingua::EN::Syllable which "estimates the number of syllables in the word passed to it." search.cpan.org/~neilb/Lingua-EN-Syllable-0.30 (packaged for debian as liblingua-en-syllable-perl) Commented May 2, 2016 at 1:57
  • @JeffSchaller I didn't have anything to count the syllables. I think the issue was that I was searching for the counting and sorting at the same time. Commented May 2, 2016 at 2:18
  • @cas Thank you very much, your solution appears to work wonderfully. Commented May 2, 2016 at 2:18
  • 1
    also FYI, I just spotted this: stackoverflow.com/questions/405161/… Commented May 2, 2016 at 2:42

1 Answer 1

14

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.

4
  • 1
    you can accept this answer and mark it as "answered" by clicking on the big tick mark if it answered your question. Commented May 2, 2016 at 2:20
  • I have a tremendous amount of words to analyse and really, I'm looking to trim the most burdensome words to say from the list, so +/-1 syllable is just fine. Commented May 2, 2016 at 2:22
  • 4
    BTW, this script highlights one of the benefits of learning 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 Commented May 2, 2016 at 2:23
  • 2
    FYI, a CPAN search says there are 993 modules matching Lingua::, some of them may be of use to you: search.cpan.org/… Commented May 2, 2016 at 2:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.