Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created July 26, 2015 16:13
Show Gist options
  • Save JavaScript-Packer/ce46cee59e8aa1d0dc1f to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/ce46cee59e8aa1d0dc1f to your computer and use it in GitHub Desktop.
Paste some text and get statistics on the words found in text. Demo on http://jsfiddle.net/dktvn13L/
<div id="wct_embed_counts">
<textarea id="wct_embed_input_text" rows="12">www.WHAK.com</textarea>
<div id="wct_embed_result">www.WHAK.com</div>
<div id="wct_powered_by">www.WHAK.com</div>
<script type="text/javascript">function wct_getWordStats(e) {
var r, a, _, c, d, s, u, t = [], n = e.trim();
t.num_character = e.trim().length;
r = n.split(/\s+/).join("");
t.num_character_wo_spaces = r.length, t.num_paragraph = 0, n.length > 0 && (t.num_paragraph = n.split(/\n+/).length);
a = n.replace(/(\w)[-_'](\w)/gi, "$10$2");
a = a.replace(/(\d)[,.](\d)/gi, "$10$2"), t.num_sentence = 0, a.length > 0 && (t.num_sentence = a.replace(/"/gi, "").split(/[.?!:\n]+/).length);
_ = a.split(/[\s\n]+/);
if (t.num_word = 0, a.length > 0 && (t.num_word = _.length), t.avg_sentence_length = 0 != t.num_sentence ? Math.round(10 * (t.num_word / t.num_sentence)) / 10 :0,
0 != t.num_word) {
c = r.length / t.num_word;
t.avg_word_length = Math.round(10 * c) / 10;
} else t.avg_word_length = 0;
d = [];
t.num_unique_word = 0, t.percentage_num_unique_word = 0;
for (s = 0; s < _.length; s++) d[_[s]] = 1;
u = Object.keys(d).length;
return t.num_unique_word = Object.keys(d).length, percentage_unique_words = Math.round(100 * u / t.num_word),
t.percentage_num_unique_word = percentage_unique_words, t;
}
function wct_display_counts() {
var e = document.getElementById("wct_embed_input_text").value;
wordStats = wct_getWordStats(e), document.getElementById("wct_embed_result").innerHTML = "<em>" + wordStats.num_word + "</em> words | <em>" + wordStats.num_character + "</em> characters | <em>" + wordStats.num_character_wo_spaces + "</em> characters w/o spaces | <em>" + wordStats.num_unique_word + "</em> unique words | <em>" + wordStats.num_sentence + "</em> sentences | <em>" + wordStats.num_paragraph + "</em> paragraphs";
}
var wct_embed_result, wct_embed_input_text, wct_textarea, wct_powered_by = document.getElementById("wct_powered_by");
wct_powered_by.setAttribute("style", "font-size:small;float:right;");
wct_embed_result = document.getElementById("wct_embed_result");
wct_embed_result.setAttribute("style", "margin:5px;text-align:center;");
wct_embed_input_text = document.getElementById("wct_embed_input_text");
wct_embed_input_text.setAttribute("style", "width: 100%;-webkit-box-sizing: border-box;-moz-box-sizing: border-box; box-sizing: border-box;");
wct_embed_result = document.getElementById("wct_embed_result");
wct_embed_result.innerHTML = "<em>0</em> words | <em>0</em> characters | <em>0</em> characters w/o spaces | <em>0</em> unique words | <em>0</em> sentences | <em>0</em> paragraphs";
wct_textarea = document.getElementById("wct_embed_input_text");
wct_textarea.value = "", wct_textarea.addEventListener && (wct_textarea.addEventListener("input", wct_display_counts, !1),
wct_textarea.addEventListener("textInput", wct_display_counts, !1), wct_textarea.addEventListener("textinput", wct_display_counts, !1)),
wct_textarea.attachEvent && wct_textarea.attachEvent("onpropertychange", wct_display_counts);</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment