Wednesday 10 October 2012

Multilanguage soundex algorithm and library

WIKI says:

Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling. The algorithm mainly encodes consonants; a vowel will not be encoded unless it is the first letter. Soundex is the most widely known of all phonetic algorithms (in part because it is a standard feature of popular database software such as PostgreSQL, MySQL, MS SQL Server and Oracle) and is often used (incorrectly) as a synonym for "phonetic algorithm". Improvements to Soundex are the basis for many modern phonetic algorithms.

But world is bigger then only English.

In my solution there is an implementation for Russian language and there is a simple method for another languages configuration. I will speak with Leonid, may be he will be so kind and  will add some European languages to this tool (Ukrainian, for example :-) )

Usage:

>>> import pysoundex
>>> print pysoundex.soundex("Приветище", lang='ru_RU')
п613
>>> print pysoundex.soundex("Hello")
h400   <--- the same as un mysql!
>>> print pysoundex.soundex("Halo")
h400


Configuration for Russian language is:


  "ru_RU": {
  "vowels": ['у','е','ы','а','о','э','я','и','ю','ь','ъ','й'],
  "consonants": {
   1: ['б','п','ф','в'], 
   2: ['с','ц','з','к','г','х'],  
   3: ['т','д'], 
   4: ['л','й'], 
   5: ['м','н'], 
   6: ['р'], 
  
  }

 },


All language rules are configurable in the soundconfig variable in the lang_soundconfig.py file.

Feel free to modify and add new languages.

You can download it here:


https://github.com/vk4arm/pysoundex

4 comments:

  1. Хей, гарни хропцi з Украiни,

    помогите сделать для Украинского конфиг пожалуйста. Я ж вижу вы бываете тут :-)

    ReplyDelete
  2. Здравствуйте.

    У вас пропущены символы: 'ж' 'ш' 'щ' 'ч'. Я выделил их в отдельный кластер, которому соответствует '7'.
    Зато есть замена 'й' -> '4', при том, что 'й' является одним из симфолов, которые фильтруются до этапа замены. Вы действительно считаете, что 'й' и 'л' схожи?
    Кластер '1' я бы разбил на два: 'б'+'п' и 'в'+'ф', но это спорно. Аналогично поступил бы с кластером '2'.

    У меня алгоритм работает неважнецки. Но я тестирую его на особом словаре. Пробовал первый символ слова не обрабатывать по-особенному (то есть не оставлять его, а разрешать сливать с соседними из того же кластера и заменять в дальнейшем). Не очень помогло.

    ReplyDelete
  3. Спасибо Игорь!

    На самом деле идея алгоритма - такая, я в принципе, согласен с Вашими изменениями в кластерах, буду благодарен, если вы закоммитите свою версию с новыми русскими кластерами.

    Еще раз спасибо!

    С уважением,
    Виктор

    ReplyDelete
  4. Игорь, в словах й и л действительно звучат одинаково часто. Надо ставить в один класс

    ReplyDelete