# Need to Create the Keyword Density

• 05-07-2014, 10:58 AM
gauravin20
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.
• 05-07-2014, 02:03 PM
mrhoo
'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%
*/

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.33435 seconds
• Memory Usage 2,345KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates