# Censor Search Queries

**URL:** <https://www.ruby-forum.com/t/censor-search-queries/89664>\
**Category:** Rails\
**Created:** [March 22, 2007, 12:21am UTC](https://www.ruby-forum.com/t/censor-search-queries/89664 "2007-03-22T00:21:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 12:21am UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/1 "2007-03-22T00:21:01Z")

</div>

No, I don’t want to censor what people are querying, I want to censor  
the searches I display.

I display the last 5 queries entered in the search box on my website.  
I do not want to display offensive terms - such as, the '7 words you  
can’t say on TV. I am already censoring the words themselves, but I  
want to censor terms that contain those words.

In case you need a reason why: the webpage is on the website of a  
major University. I want to provide students with search topic tips,  
but do not want to display any offensive words.

This is what I am doing:  
censor = get\_censor # returns an array of offensive words  
unless censor.include?(query)  
mod.update\_search

How do I extend this to filter out queries that contain the censored  
terms, I am thinking of MYSQL pattern matching with like but am not  
sure how to do it.

Thanks in advance, K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/2 "2007-03-22T17:55:04Z")

</div>

Come on - this is an interesting problem. Too easy? Too hard? Too  
controversial?

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/3 "2007-03-22T21:39:27Z")

</div>

Kim wrote:

> Come on - this is an interesting problem. Too easy? Too hard? Too  
> controversial?

Well it certainly is interesting and can be a can of worms as it will  
require regular maintenance. This is because there are many patterns  
can be used to work around your censor.

I’d suggest you get to know regex pattern matching and see how  
it can be done using MySQL functions. I think performance may  
be an issue if you have a lot of content to filter.

If you can try to filter as you store content to DB or run an  
independent  
background process that cleans up the DB content while you sleep.

– Long  
[http://MeandmyCity.com/](http://MeandmyCity.com/) - Free online business directory for local  
communities  
[http://edgesoft.ca/blog/read/2](http://edgesoft.ca/blog/read/2) - No-Cookie Session Support plugin for  
Rails

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/4 "2007-03-22T22:22:38Z")

</div>

Am I correct in assuming you just want to match any censored phrase in  
the query?

if so, it should be as simple as:

/#{censor.join(’|’)}/i.match(query)

-Shawn

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/5 "2007-03-22T22:57:44Z")

</div>

Kim G. wrote:

> Yes my idea is to filter as I store in the DB. It seems like I should be  
> able to use wildcards around a value to match it to a list of words. For  
> example, if a user enters ‘ass’ the query will get filtered out and not  
> displayed, but if they enter ‘assmonkey’ then it will not get filtered. If  
> wildcards were used then it should filter it.
> 
> I do not expect to be able to filter every offensive query, but if I can get  
> some of them I would be happier.

I agree. Though you may want to consider if the following is acceptable.  
a s s  
a # s # s  
a.s.s  
etc.

I hope you get the idea.

> I am familiar with reg ex, but how would I apply it? See my first post.
> 
> Any other takers?

I think Shawn may have eluded to the start of a possible solution…

– Long

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/6 "2007-03-22T23:04:54Z")

</div>

I did a little searching, looks like there is a plugin that does much  
of what you need:

[http://locusfoc.us/2007/2/13/name-nanny-plugin](http://locusfoc.us/2007/2/13/name-nanny-plugin)

-Shawn

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/7 "2007-03-22T22:12:03Z")

</div>

Yes my idea is to filter as I store in the DB. It seems like I should be  
able to use wildcards around a value to match it to a list of words. For  
example, if a user enters ‘ass’ the query will get filtered out and not  
displayed, but if they enter ‘assmonkey’ then it will not get filtered.  
If  
wildcards were used then it should filter it.

I do not expect to be able to filter every offensive query, but if I can  
get  
some of them I would be happier.

I am familiar with reg ex, but how would I apply it? See my first post.

Any other takers?

On 3/22/07, Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://edgesoft.ca/blog/read/2](http://edgesoft.ca/blog/read/2) - No-Cookie Session Support plugin for  
> Rails
> 
> >

–  
Kim G.  
removed\_email\_address@domain.invalid

“We are all stakeholders in the Karma economy.”

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/8 "2007-03-22T23:13:22Z")

</div>

Shawn Roske wrote:

> I did a little searching, looks like there is a plugin that does much  
> of what you need:
> 
> [http://locusfoc.us/2007/2/13/name-nanny-plugin](http://locusfoc.us/2007/2/13/name-nanny-plugin)

Good find Shawn!

– Long

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [March 22, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/censor-search-queries/89664/9 "2007-03-22T23:24:23Z")

</div>

looks good - I will try it out. Thanks to all.

On 3/22/07, Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> – Long
> 
> >

–  
Kim G.  
removed\_email\_address@domain.invalid

“We are all stakeholders in the Karma economy.”
