/
profanity.py
42 lines (38 loc) · 1.05 KB
/
profanity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import re
import random
def readProfanities():
profanities = []
file = open('profanity-list.txt','r')
for line in file:
line = line.strip()
profanities.append(line)
return profanities
profanities = readProfanities()
def replaceProfanity( text ):
text = text.lower()
tmp = {}
for w in profanities:
w = w.split(':')
for m in re.finditer(re.escape(w[0]), text):
key_str = m.start()
if len(w) > 1:
for wl in w[1].split(','):
if wl not in text:
tmp[key_str] = m
else:
tmp[key_str] = m
def compare(a, b):
return cmp(int(a.end()), int(b.end()))
results = tmp.values()
results.sort(compare)
results.reverse()
return results
if __name__ == '__main__':
w = """
Shit So you're an utter pussy, you turd, not saturday. <unt, [ock. So
dick what the fuck are you gonna fucking do. You dickheads suck cock
and I think you're a twat.
"""
print w.strip()
for m in replaceProfanity(w):
print m.group(0)[0] + '*'*(len(m.group(0))-2) + m.group(0)[len(m.group(0))-1]