[Zope-dev] Re: [Zope] Re: DirtyWordsFilter
Erik Enge
erik@esol.no
Sat, 24 Feb 2001 22:23:04 +0100 (CET)
[Jason, I'm CCing the Zope-dev, someone might have another solution]
On Sat, 24 Feb 2001, Jason wrote:
> hi I have a Script now with this:
>
> > aString = 'lots and lots of angry-customer-bad-words'
> >
> > for word in string.split(aString, ' '):
> > for catalog_brain in self.Catalog.searchResults(meta_type='bad_word'):
> > bad_word = self.Catalog.getobject(catalog_brain['data_record_id_'])
> > if word == bad_word:
> > aString = string.replace(aString, word, '*bleep!*')
> > return aString
>
> Also I have made a Catalog but not sure what you meant by
>
> > If you want to store all the words in ZCatalog you could just index a
> > bunch of words (bad words, with meta_type set to 'bad_word') and do
> > searches; (untested!)
>
> Could you explain it more clearly showing each step? and how
> I would call this script from dtml?
What I meant was that instead of having a list somewhere of bad words, you
could have an ZCatalog. Right at this moment I can't think of why, but
there you go.
Anyways, if this is the way you want to do it I'd suggest that you create
you own Python class or ZClass with the meta_type of 'bad_word'. Make
sure that you subclass CatalogAwareness, and that this is the first class
in you subclass list. This object should have an attribute called word
(or something) which contains the bad word. (Hint: it could also have a
'replace_with' attribute which is what you would substitute the bad word
with).
Now, create yourself a ZCatalog instance and create a search interface for
it. This could be a Python Method (Script) or a DTML Method -- I think
there is something called a Z Search Interface even.
Index all your bad_word objects. (Or index all of your objects.)
By now you should be able to search in you ZCatalog and find all objects
with the meta_type bad_word. This isn't enough however. You need to be
able to do some comparasins on the 'word' attribute (and to be
efficient) we add a (hope I get this right...) FieldIndex to your indexes
in the ZCatalog.
Reindex all yor bad_word objects -- or, again, let it find all objects.
(If I'm not mistaken, this is under the "Find objects" tab.)
>From a Python Method (Script) you can now do this:
# This will match all bad_word objects that have a 'word' attribute
# which is matched by something in the supplied 'words' variable.
#
# The 'words' variable is the string you want to check for bad words.
results = []
for my_word in string.split(words, ' '):
results.append(self.Catalog.searchResults(meta_type='bad_word',word=my_word))
for result in results:
bad_word = self.Catalog.getobject(result.data_record_id_)
words = string.replace(words, bad_word.word, bad_word.replace_with)
return words
All this is from the top of my head (it's worth mentioning that I've been
pulling looooong hours, so it's not a bright little head right now), and
untested.
Hope this helps!