devo devox devo devox - 4 months ago 18
Python Question

Find similar word for word in the list

I'm trying to make filter bot that can find same characters or same words in message that's listed in block list

block = ["damn", "shit"]


if message count : daaaaammmmnnnn or sssssshit or da-mn or da.mn. like that catch it and return True

how can I do this.

thx.

Answer

Set flag value to True when message contains any items from the list block.

  1. Get block list and iterate every item from the list by for loop.
  2. Set value of flag to False by default means message not contains any block list item.
  3. Set value of flag to True when item from the block list in the message.
  4. Use break statement to stop for loop iteration.

e.g.

>>> block = ["damn", "shit"]
>>> msg = "test masf to check shit or damn"
>>> flag = False
>>> for i in block:
...   if i in msg:
...      flag = True
...      break
... 
>>> flag
True
>>> 

Updated:

  1. Set block list words.
  2. Create list of list of block list word characters.
  3. Remove punctuation from the input string.
  4. split string to words.
  5. split word and create sequence list of characters from the word.
  6. check sequence list of characters present in block list character list.

code:

import string
block = ["damn", "shit"]
block_char_list = [ list(i) for i in block ]

def  getbBlockListFlag(msg):
    #Remove punctuation
    tmp = msg.translate(string.maketrans("",""), string.punctuation)
    tmp1 = []
    for word in tmp.split():
        j = []
        for k in list(word):
            if k not in j:
                j.append(k)
            elif k != j[-1]:
                j.append(k)

        # word in block list
        if j in block_char_list:
            return True, j

    return False, []



msg = "True test case ? daaaammmmnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:  
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

msg = "True test case ? normal damn.,"
status, bloak_char = getbBlockListFlag(msg)
if status:  
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

msg = "False test case ? nothing when sequance aaammdddnnn.."
status, bloak_char = getbBlockListFlag(msg)
if status:  
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)


msg = "False test case ? nothing.."
status, bloak_char = getbBlockListFlag(msg)
if status:  
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(bloak_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)
msg = "True test case ? Handle  daaaammaaaammnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:  
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

Output:

vivek@vivek:~/Desktop/stackoverflow$ python 16.py 
The word `damn` from block list is presnt in input 'True test case ? daaaammmmnnnnn.,'
The word `damn` from block list is presnt in input 'True test case ? normal damn.,'
No word from block list is presnt in input 'False test case ? nothing when sequance aaammdddnnn..'
No word from block list is presnt in input 'False test case ? nothing..'
No word from block list is presnt in input 'True test case ? Handle  daaaammaaaammnnnnn.,'