Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need Help with Strings and Substrings

    I'm new at JavaScript and am trying to figure out a simple text calculator as seen on a science project website.

    The project says the final program should calculate:
    1. the number of sentences contained in the text,
    2. the number of words in each sentence,
    3. the number of letters in each word,
    4. the average number of words per sentence, and
    5. the average word length.

    I have pretty much everything (I think) but am being completely stumped by item number 2 and item number 4. The code is below.

    Can anyone help me understand what I should be doing for point number 2 and 4? I understand how to calculate and display the length of an item in an array and return it's values as 5,6,7,8 etc where the value is the length of the word, but i can't grasp how to calculate the number of items in an array to read 2,5,9 where the values are the number of words per each sentence...so confused!

    Below is my code...apoligies if it's sloppy...it's my first javascript code experience
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Count</title>
    
    Code:
    <script type="text/javascript">
    
    function callText(mainText, stnCount, wdsPerStn, letPerWd, avgWdPerStn, avgWdLen, testFieldWds, testFieldStnc){
    	var textWords = mainText.value.split(/\s/);//splits into words by each space.
    	var textSent = mainText.value.split('.');//splits into sentences by each period.
    	
    	var nWords = new Array(textWords.length);
    		var totalLetters = 0;
    		for (i = 0; i < textWords.length; i++)
    			{
    				nWords[i] = textWords[i].length;
    				totalLetters += textWords[i].length;	
    			}
    			
    	var nSent = new Array(textSent.length);
    	var totalSent = 0;
    		for (i = 0; i < textSent.length; i++)
    			{
    				nSent[i] = textSent[i].length;
    				totalSent += textSent[i].length;	
    			}
    /*	var b,a=0;
    	var my_array = new Array(nSent);
    	for(b=0;b<my_array.length ;b++)
    		{
    		if(my_array[b]!="")
    		{
    			a++;
    		}
    		} 
    */	
    	var newStr = nWords.substr(0).length;
    	stnCount.value = textSent.length - 1;
    	wdsPerStn.value = 'wdsPerStn';
    	letPerWd.value = nWords.join(', ')
    	avgWdPerStn.value = 'avgWdPerStn'
    	avgWdLen.value = totalLetters / textWords.length;
    	testFieldWds.value = 'testFieldWds';
    	testFieldStnc.value = 'testFieldStnc'
    	
    }
    	
    function clearForm(form){
    	form.inputArea.value = '';
    	form.cnStn.value = '';	
    	form.cnWdsStn.value = '';	
    	form.cnLtrWds.value = '';	
    	form.avWdsStn.value = '';	
    	form.avWdLen.value = '';	
    	form.wrds.value = '';
    	form.stces.value = '';
    	
    }
    </script>
    <form action="" method="post" name="textForm"> <p><textarea name="inputArea" cols="80" rows="5"></textarea></p> <p><input name="calculate" type="button" value="Calculate" onclick="callText(inputArea, cnStn, cnWdsStn, cnLtrWds, avWdsStn, avWdLen, wrds, stces )" /> <input name="clearform" type="button" value="Clear" onclick="clearForm(form)" /></p> <table width="325" border="0"> <tr> <td align="right">#of Sent</td> <td><input name="cnStn" type="text" /></td> </tr> <tr> <td align="right"># of Words in Sent</td> <td><input name="cnWdsStn" type="text" /></td> </tr> <tr> <td align="right"># of Letters Per Word</td> <td><input name="cnLtrWds" type="text" /></td> </tr> <tr> <td align="right">Avg Words Per Sentence</td> <td><input name="avWdsStn" type="text" /></td> </tr> <tr> <td align="right">Average Word Length</td> <td><input name="avWdLen" type="text" /></td> </tr> <tr> <td align="right">Word Array</td> <td><input name="wrds" type="text" /></td> </tr> </tr> <tr> <td align="right">Sentence Array</td> <td><input name="stces" type="text" /></td> </tr> </table> </form> </head> <body> </body> </html>
    Last edited by yaultB; 11-16-2010 at 07:22 PM. Reason: wrapping code in code tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Counting words and sentences is not as as easy as that - you must allow for punctuation marks and multiple spaces.

    This may help you but I do not see the project as being suitable for a beginner. You ought not to try to run before you can walk!

    Code:
    <script type = "text/javascript">
    
    var sentences = [];
    var sentsplit = [];
    var totalWords = 0;
    var txt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum. Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc. Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna mauris, pellentesque at ultricies vitae, fermentum vitae dolor. "
    txt = txt.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    txt = txt.replace(/\.|\?|\!/g,"~");  // Replace . ? ! by tilde
    txt = txt.replace(/\,|\;|\:/g,"");  // Strip comma, colon,semi-colon
    txt = txt.replace(/(\s)(A-Z)/g,"$2");   // Strip space before capital letter
    txt = " " + txt;  // add space at start of text
    
    sentences = txt.split("~");
    var len = sentences.length-1;
    alert ("There are " + len + " sentences");
    
    for (var i = 0; i < len; i++) {
    var sent = sentences[i];
    sentsplit[i] = sent.split(" ");
    words = sentsplit[i].length-1;
    totalWords += words;
    }
    alert ("The total number of words is " + totalWords)
    
    var averageWords = (totalWords/len).toFixed(2);
    alert ("The average number of words in a sentence is " +  averageWords);
    </script>
    BTW, when posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    So - did anyone dare tell George Stephenson, "It's not Rocket science"?

  • Users who have thanked Philip M for this post:

    yaultB (11-16-2010)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Philip. Yes, I have taken on a bit more than I expected.
    However, I was able to trudge through and get most of what I needed with the intent to go back after the initial calculations were set up and then add the required code to replace punctuation as you show above. Thanks for that code by the way. It helps me understand where to put specific code.

    However, my main problem up front before I move on to replacing punctuation, double spaces and the like, is returning the number of words in each sentence and then the average number of words per sentence. Basically, to return the fields "# of Words in Sent" and "Avg Words Per Sentence" in the same fashion as the "# of letters Per Word" where the values are relative to word count, not letter count.

    Like the pic below which is the form I created to handle the script:




    That's the stopping point right now which has me in gridlock.

    also...apologies on not wrapping my code properly. I think I edited and updated correctly.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    A sentence terminates with a full stop (period), ! or ?. You need to count the number of sentences, the number of words, and the number of individual letters. A space or a punctuation mark is not a word, nor do they count towards the number of letters.

    If you can establish these correctly then obviously the average number of words per sentence, the average length of a word etc. is simple maths.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Hey, Philip, I follow all of that except this one:
    Code:
    txt = txt.replace(/(\s)(A-Z)/g,"$2");   // Strip space before capital letter
    Why???
    The summers are hot in Africa.
    produces
    The summers are hot inAfrica~
    and the word count is now off.
    ???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Hey, Philip, I follow all of that except this one:
    Code:
    txt = txt.replace(/(\s)(A-Z)/g,"$2");   // Strip space before capital letter
    Why???

    produces
    and the word count is now off.
    ???
    Oh dear! I don't know how that got in - copy and paste went wrong.
    Just delete that line.
    Last edited by Philip M; 11-16-2010 at 08:41 PM.


  •  

    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
    •