1

I am trying to implement a word counter in textbox. I am using the links below:

JS Fiddle
Second link

<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea>
<script type=""text/javascript"">
    var cnt;
    function wordcount(count) {
        var words = count.split(/\s/);
        cnt = words.length;
        var ele = document.getElementById('w_count');
        ele.value = cnt;
    }
    document.write("<input type=text id=w_count size=4 readonly>");
</script>

Word counter is working fine. But my scenario is like as below:

  1. for word "Most Suitable Match" if user type short form as "MSM" then MSM also shall be counted as 3 words.
  2. In the same way if there is name of college like "DAV" then it shall also be counted as 3 words.

Please suggest !!

16
  • 5
    You'll need a dictionary of permitted acronyms and then loop over each "word" to find acronyms :) Commented Jul 25, 2015 at 16:43
  • any examples please?? it would be of great help... Commented Jul 25, 2015 at 16:44
  • 2
    If you know all the acronyms/shorthands which will be used by the users, you can store them in a javascript object as key, value pairs. (key: acronym, value: word_count) (eg: {"MSM" : 3} )Then inside the wordcount function you can iterate through the 'words' array and check if it is in the javascript object and if so make it count as its value.. Commented Jul 25, 2015 at 16:47
  • 1
    yup... i shall do that then... :) and why dwonvotes... what is wrong in ques Commented Jul 25, 2015 at 16:52
  • 1
    People just like to downvote on this community @Gags :sss They are just too picky.. Commented Jul 25, 2015 at 16:54

2 Answers 2

1

I have make a simple function:

var regex = [/DAV/g, /MAC/g];

function countWords() {
  var count = [];
  regex.forEach(function(reg) {
    var m = text.match(reg);

    if (m) {
      count = count.concat(m);
    }
  });

  // the number of known acronym wrote in the text 
  var acronyms = count.length;

  // how much words generated from an acronym (e.g. DAV === 3 words; AB === 2 words and so on)
  var wordsFromAcronyms = count.join().replace(/,/g,'').length;

  // how many words wrote (this is equal to your code)
  var rawWords = text.match(/\S+/g).length;

  // compute the real number
  return rawWords - acronyms + wordsFromAcronyms;
}

It counts the number of the wrote acronym (the list of the known acronyms is stored in regex array), then count how much words are generated by the acronyms (wordsFromAcronym), and then substract the number of acronyms (acronyms) from the total words (rawWords) and then add the wordsFromAcronym.

Here is a PLNKR.

Sign up to request clarification or add additional context in comments.

3 Comments

yup... this way i have to find out all acronyms :)
Yes, but this is the only effective solution cause you can't be sure about user's casing. If a user SHOUT IN UPPERCASE you will reach the maximum number of words quite earlly....
yup... i liked this approach but firstly let me figure out acronyms
1

Try this. I am on my mobile so I cannot make an example easily This will count all words in all uppercase as acronyms

<textarea name="myMessage" onkeyup="wordcount(this.value)"></textarea>
<input type=text id=w_count size=4 readonly>
<script type=""text/javascript"">
      function wordcount(message) {
        var words = message.split(/\s/);
        var cnt = words.length;
        for (var i=0;i<cnt;i++) {
          if (words[i].length>1 && words[i].match(/^[A-Z]*$/)) cnt += words[i].length-1)
        }
        var ele = document.getElementById('w_count');
        ele.value = cnt;
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.