Last active
August 29, 2015 14:13
-
-
Save panchr/0ecf09a9dcec75c55558 to your computer and use it in GitHub Desktop.
Python string filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
FILTER_WORDS = ["spam", "morespam", "blahblah"] # and so forth | |
FILTER = re.compile("|".join(FILTER_WORDS)) | |
def phrasePassesFilter(word): | |
'''Checks if the phrase passes the filter''' | |
return not bool(FILTER.match(word)) # if there are no matches, FILTER.match --> None, so it passes the filter | |
# otherwise, it's True so it contains a filtered word, so mark as spam | |
def isSpam(word): | |
'''Same as above, but just checks if spam and not if it passes the filter''' | |
return bool(FILTER.match(word)) | |
print isSpam("my spam is so sneaky") # --> True | |
print phrasePassesFilter("my spam is so sneaky") # --> False | |
print isSpam("rushy") # --> False | |
print phrasePassesFilter("rushy") # --> True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment