Answer the question
In order to leave comments, you need to log in
What is the best way to check the text for the presence of words from the database in Django?
I'm trying to implement a check of downloaded texts for the presence of words from the black list. The list itself is stored in the database and will be updated promptly. I came up with something like this myself:
for i, wrd in enumerate(text.lower().split()):
if BadWords.objects.filter(bword=wrd ).exists():
return ....
Answer the question
In order to leave comments, you need to log in
If we abstract from the database, then the task looks a little easier. Consider that there are two sets: the set of bad words and the set of words in the text. It remains to determine whether these sets intersect. If they intersect, then there is at least one bad word :-)
>>> a = { 1, 2, 3 }
>>> b = { 2, 3, 4 }
>>> c = { 5, 6 }
>>>
>>> a & b
{2, 3}
>>> a & c
set()
# utils.py
from django.core.cache import cache
def get_bad_words():
return cache.get('bad_words')
# models.py
def set_bad_words(**kwargs):
from django.core.cache import cache
cache.set('bad_words', {w.bword for w in BadWords.objects.all()})
models.signals.post_save.connect(set_bad_words, sender=BadWords)
models.signals.post_delete.connect(set_bad_words, sender=BadWords)
# utils.py
def get_words_from_text(text_string):
return set([w for w in text_string.lower().split()])
# utils.py
def has_bad_words(text_string):
return bool(get_bad_words() & get_words_from_text(text_string))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question