Re: Sounds-like algorithm?

Akkana Peck <[email protected]> Thu, 16 Jun 2022 10:26:06 -0600
Newsgroups gmane.org.user-groups.linux.svlug
Message-ID <[email protected]>
Steve Litt writes:
> The only thing I don't know how to do is the sounds-alike algorithm to
> suggest other words. Has anybody made one of those? Or do you know of
> source code that's sound-alike algorithm and nothing but sounds-alike
> algorith without all sorts of X calls and other krap mixed in to
> obfuscate the actual algorithm?

I don't, but I once experimented with a python program to find
rhymes for a word. One thing that I found was
https://pypi.org/project/pronouncing/ (which bills itself as a
simple interface for the CMU Pronouncing Dictionary), and before
that, I tried to use NLTK (the Python National Language ToolKit)
to get phonemes from the CMU dictionary, but I realized it would
take a lot more work, and then something else shiny caught my eye.
Anyway, here's a code snippet I had for working with NLTK:

from nltk.corpus import cmudict
cdict = cmudict.dict()

def get_syllables(word):
    try:
        cmumatches = cdict[word]
    except KeyError:
        return []

    if not cmumatches:
        return []

    pronunciations = []
    for phonemes in cmumatches:
        syls = []
        cursyl = ""
        for phoneme in phonemes:
            if phoneme[-1].isdigit():
                syls.append(cursyl + phoneme)
                cursyl = ""
            else:
                cursyl += phoneme
        if cursyl:
            syls.append(cursyl)
        pronunciations.append(syls)

    return pronunciations

If I recall correctly, the first time you import cmudict it has to
download the whole corpus, which takes a while, but then I think it
caches it somewhere.

I also tried the code recommended in https://stackoverflow.com/a/25714769
but it didn't work for me at all.

So no solutions here, but I do think NLTK and the CMU dictionary (or
something based on it) is a likely place to start looking.

Of course, since your problem with aspell was that it flagged the
html tags, and you already know how to limit it to text and leave
out the tags, you could just use aspell on the result. But I don't
want to spoil your fun. :-)

        ...Akkana

        ...Akkana

        ...Akkana