Advertisement
Guest User

Counter-filter-escaping

a guest
Apr 27th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import re
  2. import itertools
  3.  
  4. def inject_substring(string, substring):
  5.     sections = []
  6.     for char in string:
  7.         sections.append(char)
  8.         sections.append(substring)
  9.     sections.pop()
  10.     return "".join(sections)
  11.  
  12. def permutations_with_repetition(iterable, r=None):
  13.     for unique in set(itertools.permutations(iterable, r)):
  14.         yield unique
  15.        
  16. def anchored_permutation(string):
  17.     if len(string) < 3:
  18.         yield [string]
  19.     else:
  20.         first = string[0]
  21.         last = string[-1]
  22.         for permutation in permutations_with_repetition(string[1:-1]):
  23.             permutation_str = "".join(permutation)
  24.             yield "".join((first, permutation_str, last))
  25.        
  26. def regex_permutations(string, delims, max_delims):
  27.     sub_pattern = "[{d}]{{0,{m}}}".format(d=delims,m=max_delims)
  28.     for string_permutation in anchored_permutation(string):
  29.         yield inject_substring(string_permutation, sub_pattern)
  30.  
  31. def compiled_permutations(string, delims, max_delims):
  32.     permutations = regex_permutations(string, delims, max_delims)
  33.     for permutation in permutations:
  34.         yield re.compile(permutation)
  35.  
  36. def generate_pattern_list(wordlist, delims, max_delims):
  37.     patternlist = []
  38.     for word in wordlist:
  39.         for pattern in compiled_permutations(word, delims, max_delims):
  40.             patternlist.append(pattern)
  41.     return patternlist
  42.  
  43. def is_profane(msg, patternlist):
  44.     for pattern in patternlist:
  45.         matches = pattern.search(msg)
  46.         if matches is not None:
  47.             return True
  48.     return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement