reversePhonemes.frink

Download or view reversePhonemes.frink in plain text format


/** Find which words, when you reverse their letters, or their phonemes, make
    another word.

    This generates two lists:

    1. Words that are also words when spelled backwards
    2. Words that are also words when the phonemes are spoken in reverse order.

    This is in response to a Twitter query:
    https://x.com/itsnotmyfault01/status/1833641657348051377
*/


word2phon = new dict
phon2word = new dict

// This uses the CMU pronunciation dictionary
// http://www.speech.cs.cmu.edu/cgi-bin/cmudict

// Specifically this file which you'll want to download
// https://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b
// (you can replace the file: URL below with that but it'll fetch every time)

LINE:
for line = lines["file:/home/eliasen/prog/mobydict/mpron/cmupronunciation.txt"]
{
   if line =~ %r/^\s*;;;/   // Skip comments
      next LINE

   if [word, phonemes] = line =~ %r/(.*?)  (.*)/
   {
      word = trim[word]
      phonemes = trim[phonemes]
      phonemes =~ %s/\d//g       // Remove stresses (0,1,2)
      word2phon@word = phonemes
      phon2word@phonemes = word
   }
}

println["Words that are also words when spelled backwards:"]
for [word, phonemes] = sort[word2phon, byColumn[0]]
{
   revword = reverse[word]
   w2 = word2phon@revword
   if w2 != undef          // Is there a word spelled backward from this word?
   {
      revphonemes = word2phon@revword
      println["$word ($phonemes), $revword ($revphonemes)"]
   }
}

println[]

println["Words that are also words when the phonemes are spoken in reverse order."]
for [word, phonemes] = sort[word2phon, byColumn[0]]
{
   revphon = join[" ", reverse[split[%r/\s+/, phonemes]]]  // Reverse phonemes
   w2 = phon2word@revphon
   if w2 != undef    // Is there a word with phonemes reversed from this word?
      println["$word, $w2, $phonemes"]
}


Download or view reversePhonemes.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20152 days, 21 hours, 51 minutes ago.