Advertisement
rg443

TextStatistics.js

Aug 22nd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(glob) {
  2.   function cleanText(text) {
  3.     var fullStopTags = ["li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dd"];
  4.     fullStopTags.forEach(function(tag) {
  5.       text = text.replace("</" + tag + ">", ".");
  6.     });
  7.     text = text.replace(/<[^>]+>/g, "").replace(/[,:;()\-]/, " ").replace(/[\.!?]/, ".").replace(/^\s+/, "").replace(/[ ]*(\n|\r\n|\r)[ ]*/, " ").replace(/([\.])[\. ]+/, ".").replace(/[ ]*([\.])/, ". ").replace(/\s+/, " ").replace(/\s+$/, "");
  8.     text += ".";
  9.     return text;
  10.   }
  11.   var TextStatistics = function TextStatistics(text) {
  12.     this.text = text ? cleanText(text) : this.text;
  13.   };
  14.   TextStatistics.prototype.fleschKincaidReadingEase = function(text) {
  15.     text = text ? cleanText(text) : this.text;
  16.     return Math.round((206.835 - 1.015 * this.averageWordsPerSentence(text) - 84.6 * this.averageSyllablesPerWord(text)) * 10) / 10;
  17.   };
  18.   TextStatistics.prototype.fleschKincaidGradeLevel = function(text) {
  19.     text = text ? cleanText(text) : this.text;
  20.     return Math.round((.39 * this.averageWordsPerSentence(text) + 11.8 * this.averageSyllablesPerWord(text) - 15.59) * 10) / 10;
  21.   };
  22.   TextStatistics.prototype.gunningFogScore = function(text) {
  23.     text = text ? cleanText(text) : this.text;
  24.     return Math.round((this.averageWordsPerSentence(text) + this.percentageWordsWithThreeSyllables(text, false)) * .4 * 10) / 10;
  25.   };
  26.   TextStatistics.prototype.colemanLiauIndex = function(text) {
  27.     text = text ? cleanText(text) : this.text;
  28.     return Math.round((5.89 * (this.letterCount(text) / this.wordCount(text)) - .3 * (this.sentenceCount(text) / this.wordCount(text)) - 15.8) * 10) / 10;
  29.   };
  30.   TextStatistics.prototype.smogIndex = function(text) {
  31.     text = text ? cleanText(text) : this.text;
  32.     return Math.round(1.043 * Math.sqrt(this.wordsWithThreeSyllables(text) * (30 / this.sentenceCount(text)) + 3.1291) * 10) / 10;
  33.   };
  34.   TextStatistics.prototype.automatedReadabilityIndex = function(text) {
  35.     text = text ? cleanText(text) : this.text;
  36.     return Math.round((4.71 * (this.letterCount(text) / this.wordCount(text)) + .5 * (this.wordCount(text) / this.sentenceCount(text)) - 21.43) * 10) / 10;
  37.   };
  38.   TextStatistics.prototype.textLength = function(text) {
  39.     text = text ? cleanText(text) : this.text;
  40.     return text.length;
  41.   };
  42.   TextStatistics.prototype.letterCount = function(text) {
  43.     text = text ? cleanText(text) : this.text;
  44.     text = text.replace(/[^a-z]+/ig, "");
  45.     return text.length;
  46.   };
  47.   TextStatistics.prototype.sentenceCount = function(text) {
  48.     text = text ? cleanText(text) : this.text;
  49.     return text.replace(/[^\.!?]/g, "").length || 1;
  50.   };
  51.   TextStatistics.prototype.wordCount = function(text) {
  52.     text = text ? cleanText(text) : this.text;
  53.     return text.split(/[^a-z0-9]+/i).length || 1;
  54.   };
  55.   TextStatistics.prototype.averageWordsPerSentence = function(text) {
  56.     text = text ? cleanText(text) : this.text;
  57.     return this.wordCount(text) / this.sentenceCount(text);
  58.   };
  59.   TextStatistics.prototype.averageSyllablesPerWord = function(text) {
  60.     text = text ? cleanText(text) : this.text;
  61.     var syllableCount = 0, wordCount = this.wordCount(text), self = this;
  62.     text.split(/\s+/).forEach(function(word) {
  63.       syllableCount += self.syllableCount(word);
  64.     });
  65.     return (syllableCount || 1) / (wordCount || 1);
  66.   };
  67.   TextStatistics.prototype.wordsWithThreeSyllables = function(text, countProperNouns) {
  68.     text = text ? cleanText(text) : this.text;
  69.     var longWordCount = 0, self = this;
  70.     countProperNouns = countProperNouns === false ? false : true;
  71.     text.split(/\s+/).forEach(function(word) {
  72.       if (!word.match(/^[A-Z]/) || countProperNouns) {
  73.         if (self.syllableCount(word) > 2) {
  74.           longWordCount++;
  75.         }
  76.       }
  77.     });
  78.     return longWordCount;
  79.   };
  80.   TextStatistics.prototype.percentageWordsWithThreeSyllables = function(text, countProperNouns) {
  81.     text = text ? cleanText(text) : this.text;
  82.     return this.wordsWithThreeSyllables(text, countProperNouns) / this.wordCount(text) * 100;
  83.   };
  84.   TextStatistics.prototype.syllableCount = function(word) {
  85.     var syllableCount = 0, prefixSuffixCount = 0, wordPartCount = 0;
  86.     word = word.toLowerCase().replace(/[^a-z]/g, "");
  87.     var problemWords = {"simile":3, "forever":3, "shoreline":2};
  88.     if (problemWords.hasOwnProperty(word)) {
  89.       return problemWords[word];
  90.     }
  91.     var subSyllables = [/cial/, /tia/, /cius/, /cious/, /giu/, /ion/, /iou/, /sia$/, /[^aeiuoyt]{2,}ed$/, /.ely$/, /[cg]h?e[rsd]?$/, /rved?$/, /[aeiouy][dt]es?$/, /[aeiouy][^aeiouydt]e[rsd]?$/, /^[dr]e[aeiou][^aeiou]+$/, /[aeiouy]rse$/];
  92.     var addSyllables = [/ia/, /riet/, /dien/, /iu/, /io/, /ii/, /[aeiouym]bl$/, /[aeiou]{3}/, /^mc/, /ism$/, /([^aeiouy])\1l$/, /[^l]lien/, /^coa[dglx]./, /[^gq]ua[^auieo]/, /dnt$/, /uity$/, /ie(r|st)$/];
  93.     var prefixSuffix = [/^un/, /^fore/, /ly$/, /less$/, /ful$/, /ers?$/, /ings?$/];
  94.     prefixSuffix.forEach(function(regex) {
  95.       if (word.match(regex)) {
  96.         word = word.replace(regex, "");
  97.         prefixSuffixCount++;
  98.       }
  99.     });
  100.     wordPartCount = word.split(/[^aeiouy]+/ig).filter(function(wordPart) {
  101.       return !!wordPart.replace(/\s+/ig, "").length;
  102.     }).length;
  103.     syllableCount = wordPartCount + prefixSuffixCount;
  104.     subSyllables.forEach(function(syllable) {
  105.       if (word.match(syllable)) {
  106.         syllableCount--;
  107.       }
  108.     });
  109.     addSyllables.forEach(function(syllable) {
  110.       if (word.match(syllable)) {
  111.         syllableCount++;
  112.       }
  113.     });
  114.     return syllableCount || 1;
  115.   };
  116.   function textStatistics(text) {
  117.     return new TextStatistics(text);
  118.   }
  119.   typeof module != "undefined" && module.exports ? module.exports = textStatistics : typeof define != "undefined" ? define("textstatistics", [], function() {
  120.     return textStatistics;
  121.   }) : glob.textstatistics = textStatistics;
  122. })(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement