Created
July 6, 2010 19:49
-
-
Save schwanksta/465830 to your computer and use it in GitHub Desktop.
Counts the # of ALL-CAPS words and blocks if > than a certain percent.
This file contains hidden or 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
# in toolbox/text.py: | |
def count_capital_words(str): | |
""" | |
Counts the number of capital words in a string and returns a % of ALL-CAPS WORDS. Issues with this: | |
"words" made up of non-letters get counted as all-caps (including emoticons: :(, :), 8===D, etc). Also, | |
CoMmEnTs lIkE tHiZ don't get counted. Perhaps a better solution would be to to use a regex to count all occurrences of | |
[A-Z]. This works decently though. | |
""" | |
count = 0 | |
words = str.split() | |
length = len(words) | |
for word in words: | |
if word == word.upper() and word != 'I': | |
count+=1 | |
return (float(count) / length) * 100 | |
# in comments/forms.py: | |
from toolbox.text import count_capital_words | |
from django.conf import settings | |
#... | |
def clean_comment(self): | |
#... | |
if getattr(settings, 'COMMENT_CAPS_BLOCKER', False) and count_capital_words(comment) > getattr(settings, 'COMMENT_CAPS_THRESHOLD', 70): | |
raise forms.ValidationError("Sorry, comments WRITTEN IN ALL CAPS will not be approved. Please re-write your comment (try hitting the caps lock key) and try again.") | |
return comment | |
# in settings.py: | |
COMMENT_CAPS_BLOCKER = True | |
COMMENT_CAPS_THRESHOLD = 70 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment