-3

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.

devo devox
  • 11
  • 2

3 Answers3

0

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.,'
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
0
from collections import OrderedDict

string = "test this string to check for sshh-iitit and dammm.mn"
new_string = string.replace('-','')
final_string = new_string.replace('.','')
words = final_string.split()
for word in words:
    check = "".join(OrderedDict.fromkeys(word))
    if check == "damn" or check == "shit":
        print "true"
Waqar Bin Kalim
  • 321
  • 1
  • 7
-1

You should have a look at the Levenshtein distance! PHP has this nice function called: leventhstein(). And for Phython is minimum one C implementation: https://pypi.python.org/pypi/python-Levenshtein/0.12.0

niyasc
  • 4,440
  • 1
  • 23
  • 50
row
  • 131
  • 2
  • 11