Forum Moderators: coopster

Message Too Old, No Replies

Keyword density in string

keyword density php function

         

vcvetic

4:14 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Hello,

Does anyone know where I can find php function or class that determens keyword density for given keyword on string?

jatar_k

4:46 pm on Aug 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't know of a function but I guess you would do it by splitting up the text into individual words and then counting them.

jd01

7:28 am on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know of one that comes pre-written, because you not only have to split the string into words, then count and compare, you also have to stem and remove 'stop words' and/or code to get any accuracy.

A good starting point is 'word stemming algorythm' (or some variation thereof) in your favorite SE.

I also recommend looking into preg_split, array_count_values, strlen, array_walk and preg_replace...

Justin

Jaunty Edward

7:43 am on Aug 27, 2005 (gmt 0)

10+ Year Member



Hi,

If only I knew how to split:

$text = "this is one small para";

into something like:

this
is
one
small
para

I could probably stop people from using that famous controversial 4 letter F word on the content they post on my site.

My research is on, can anyone help.

Thanks
Bye

jd01

8:52 am on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If that's all you are looking to do there are multiple ways to get the answer depending on the results you would like...

preg_replace - will allow you to replace words in a string - powerful takes more time to run

preg_match - will allow you to check for words in a string - powerful takes more time to run

stristr - will allow you to check for words in a string - stristr is case insensitive, strstr is not. - faster, than preg, but no regex options

stripos - will allow you to check for a word in a string - stripos is case insensitive, strpos is not. - faster, than preg, but no regex options

in_array - will allow you to check and see if a word is in an array - the least powerful solution & case sensitive, so not recommended.

Either:

$changetowords = explode(" ", $string); (recommended, faster) OR

$changetowords = preg_split("/[\s,]+/", $string);

Will allow you to break the string as you requested.

There are some other ways, but these are the most used.

Wish I could be more help, but am not sure other than finding a word how you would like to handle things from there. How many words or variations of words you are looking for will also impact which one you should use. - these should give you a start.

For a single word I would probably use stristr:

$string = 'This is a string with a badword.';
if(stristr($string, 'badword')) { echo 'found a bad word'; }

Justin

Jaunty Edward

9:13 am on Aug 27, 2005 (gmt 0)

10+ Year Member



Oh Justin, thats exactly what I wanted.... a start.

Thanks and if I ever come up with the script I will send you.

Bye