From: Axel Etzold Date: 2008-09-04T01:01:14+09:00 Subject: Re: How to match words that rhyme? -------- Original-Nachricht -------- > Datum: Wed, 3 Sep 2008 23:09:05 +0900 > Von: Lloyd Linklater > An: ruby-talk@ruby-lang.org > Betreff: Re: How to match words that rhyme? > Here is a crude start based on an old version I wrote in Pascal. It > should give you a place to start. (Please do not be TOO brutal in > critique. I am new with writing Ruby algorithms. Elegance will come.) > > class Soundex > def soundsAlike word1, word2 > soundex(word1) == soundex(word2) > end > > def soundex w > w.upcase! > temp1 = w[0,1] > 1.upto(w.length - 1) { |i| > case w[i, 1] > when 'B','F','P','V' > temp1 += '1' > when 'C','G','J','K','Q','S','X','Z' > temp1 += '2' > when 'D','T' > temp1 += '3' > when 'L' > temp1 += '4' > when 'M', 'N' > temp1 += '5' > when 'R' > temp1 += '6' > end > } > i = 1 > while i < temp1.length - 1 do > if temp1[i] == temp1[i + 1] > temp1[i] = '' > else > i += 1 > end > end > temp1 > end > end > > s = Soundex.new > p s.soundsAlike('their', 'there') # returns true > -- > Posted via http://www.ruby-forum.com/. Another solution, which could help the problem of non-phonetic writing in English is to use the external text-to-speech program espeak: http://espeak.sourceforge.net/ It can produce a kind of phonetic writing for English like so: axel@alecrim:~$ espeak -v mb-en1 'dough' > res axel@alecrim:~$ cat res d 65 @U 294 0 103 80 77 100 77 _ 263 _ 1 axel@alecrim:~$ espeak -v mb-en1 'tough' > res2 axel@alecrim:~$ cat res2 t 100 V 129 0 103 80 77 100 77 f 142 _ 263 _ 1 axel@alecrim:~$ espeak -v mb-en1 'bow' > res3 axel@alecrim:~$ cat res3 b 65 @U 294 0 103 80 77 100 77 _ 263 _ 1 ... this works also in other languages for which the correspondence between pronounciation and spelling isn't one-to-one, e.g., French: axel@alecrim:~$ espeak -v mb-fr1 'eaux' > res4 axel@alecrim:~$ cat res4 o 202 0 103 80 77 100 77 _ 263 _ 1 axel@alecrim:~$ espeak -v mb-fr1 'o' > res5 axel@alecrim:~$ cat res5 o 202 0 103 80 77 100 77 _ 263 _ 1 espeak has many "voices" for different languages. For the above to work, you'll need to choose Mbrola voices, because the text-to-speech system Mbrola (http://tcts.fpms.ac.be/synthesis/mbrola.html) needs this phonetic text as input. Best regards, Axel -- Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten Browser-Versionen downloaden: http://www.gmx.net/de/go/browser