由于 Django 不处理过滤脏话 - 有人对在 django 中实现某种自然语言处理/过滤脏话的简单方法有任何建议吗?
问问题
1749 次
2 回答
2
我个人说...不要打扰。如果您创建更好的过滤器,他们只会以不同的方式输入...
但是,这里有一个简单的例子:
import re
bad_words = ['spam', 'eggs']
# The \b gives a word boundary so you don't have the Scunthorpe problem: http://en.wikipedia.org/wiki/Scunthorpe_problem
pattern = re.compile(
r'\b(%s)\b' % '|'.join(bad_words),
re.IGNORECASE,
)
some_text = 'This text contains some profane words like spam and eggs. But it wont match spammy stuff.'
print some_text
# This text contains some profane words like spam and eggs. But it wont match spammy stuff.
clean_text = pattern.sub('XXX', some_text)
print clean_text
# This text contains some profane words like XXX and XXX. But it wont match spammy stuff.
于 2012-09-15T17:12:14.013 回答