Jump to content

Detecting swear words

- - - - -

  • Please log in to reply
No replies to this topic

#1
pedro3005

pedro3005

    Newbie

  • Members
  • PipPip
  • 14 posts
So I had to work on a function that detects if a word is a variant of another. This is part of a plugin for a bot that forbids words. It was really hard, and as always, these can't be perfect. I'd be interested to see how others would solve this problem. I ended up with:
def similar(word, check):
    '''Detects if a word is similar to another, to prevent people from tricking the bot. May find false positives.'''
    cWord, cCheck, pBuffer, chars = ([x for x in word.lower()], [x for x in check.lower()], 0, []) # cWord and cCheck are lists with each character in word and check, lowered. Gotta love python
    replace = {"1": "i", "3": "e", "4": "a", "0": "o", "5": "s", "7": "t", "!": "i", "@": "a", "6": "g", "(": "c"}

    if word.startswith("http://") or word.startswith("www."):
        return False

    for char in cWord:
        if char in replace.keys() and char not in cCheck: # may want to ban words with numbers
            cWord[cWord.index(char)] = replace[char]
            char = replace[char]
        if char in cCheck:
            chars.append(char)

    while chars:
        try:
            if chars[0] == cCheck[0]:
                pBuffer += 1
                del cCheck[0], chars[0]
                continue
            else:
                del chars[0]
        except: pass
        if not cCheck:
            break

    if pBuffer >= len(check)-1:
        return True
    elif "".join(cWord).find(check) != -1:
        return True

while True:
    recv = raw_input(">>> ")
    if similar(recv.split()[0], recv.split()[1]):
        print "Yes."
    else:
        print "No."





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users