# Thread: Need to Create the Keyword Density

1. Registered User
Join Date
Mar 2014
Posts
2

## Need to Create the Keyword Density

Hi,

I am looking for a javascript that shows the % of the keyword density when the text is written.

For example :

I have a Keyword : "This is a day"

My Text is " This is a day that is really important for us so this is the day we are waiting for a long time now something. "

Here the Total Words is 24 and the Keyword used is 2wice

So the keyword density would be 10%.

Can anyone help me with the javascript that takes the total count of words from the textarea and then calculates it according to the keywords used and show the percentage.

Any help would be highly appriciated. Thanks all in advance.

2. Registered User+
Join Date
Feb 2006
Posts
2,930
'This is a day' is only used once in your example.

It is unclear how you are measuring density- 2 is 8 and a third percent of 24, but the density should take into account the size of your keyword,
so if you did match This is a day and 'this is the day', perhaps the density would be 33 and a third percent, since yoy are using eight words out of twenty four.

Code:
```function densewords(keyword, text){
var m1= text.match(/\b[a-zA-Z']+\b/g).length,
m2= text.match(RegExp('\\b'+keyword+'\\b', 'gi')).length,
m3= keyword.split(' ').length;
return (100*(m2*m3/m1)).toFixed(2)+ '%';
}

var k= "[tT]his is a|the day", m1, m2, percent,
t= "This is a day that is really important for us so this is the day we are waiting for a long time now something.";
densewords(k, t)```
/* returned value: (String)
33.33%
*/
Last edited by mrhoo; 05-07-2014 at 02:14 PM. Reason: added code