Results 1 to 2 of 2

Thread: Need to Create the Keyword Density

  1. #1
    Join Date
    Mar 2014

    Question Need to Create the Keyword Density


    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. #2
    Join Date
    Feb 2006
    '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.

    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)
    Last edited by mrhoo; 05-07-2014 at 03:14 PM. Reason: added code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center