diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index f396570..9eb7e70 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,35 @@ Readability =========== -Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Liau +_Automated Readability Index, Flesch-Kincaid (ease/level), Gunning-Fog, SMOG Index, Coleman-Liau_ -Examples -======== -**Automated Readability Index** -``` javascript -automatedReadability("I immediately regret this decision."); -Characters: 30 -Words: 5 -Sentences: 1 -Automated Readability Index: 9.329999999999998 -``` +### **[Demo](http://brbcoding.github.io/Readability/)** -**Flesch Kincaid Ease** -``` javascript -fleschKincaidEase("I’ll have a Manhattan. And kick the vermouth to the side with a pair of steel-toed boots."); -Total Words: 17 -Total Sentences: 2 -Total Syllables: 21 -Flesch Kincaid Reading Ease: 93.70161764705884 +#### Examples ``` - -**Flesch Kincaid Grade Level** -``` javascript -fleschKincaidGradeLevel("He had a voice that could make a wolverine purr and suits so fine they made Sinatra look like a hobo."); -Total Words: 21 -Total Sentences: 1 -Total Syllables: 26 -Flesch Kincaid Grade Level: 7.209523809523812 +> const testPhrase = 'No kidding - Lorenzo called off his trip to visit Mexico City just because they told him the conquistadores were extinct.'; ``` - -**Gunning Fog Score** -``` javascript -gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); -Total Words: 14 -Total Sentences: 3 -Total Complex Words: 0 -Gunning Fog Score: 1.866666666666667 -``` - -**SMOG Index** -``` javascript -smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -Number of Sentences: 3 -Polysyllabic Words: 1 -SMOG index: 3.7792166259557014 +##### [Automated Readability Index](https://en.wikipedia.org/wiki/Automated_readability_index) +``` +> Readability.automatedReadability(testPhrase); +11.274285714285718 +``` +##### [SMOG Index](https://en.wikipedia.org/wiki/SMOG) +``` +> Readability.smog(testPhrase); +10.065306667255596 +``` +##### [Gunning Fog](https://en.wikipedia.org/wiki/Gunning_fog_index) +``` +> Readability.gunningFog(testPhrase); +14.114285714285714 +``` +##### [Coleman Liau](https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index) +``` +> Readability.colemanLiau(testPhrase); +11.952857142857141 +``` +##### [Flesch Kincaid](https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests) +``` +> Readability.fleschKincaid(testPhrase); +{ ease: 52.57714285714289, gradeLevel: 11.142857142857142 } ``` - -**Coleman-Liau Index** -``` javascript -colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."); -Total Characters: 86 -Total Words: 20 -Total Sentences: 1 -Coleman Liau Index: 9.511999999999997 -``` \ No newline at end of file diff --git a/build/index.js b/build/index.js new file mode 100644 index 0000000..514ae38 --- /dev/null +++ b/build/index.js @@ -0,0 +1,69 @@ +'use strict'; + +var Readability = { + getSyllableCount: function getSyllableCount(input) { + // lol @ this, obviously not 100% + return input.trim().split(' ').reduce(function (a, b) { + return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '').replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); + }, 0); + }, + getCharacterCount: function getCharacterCount(input) { + return input.trim().match(/\w/g).length; + }, + getWordCount: function getWordCount(input) { + return input.trim().match(/\S+/g).length; + }, + getSentenceCount: function getSentenceCount(input) { + return input.trim().match(/[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi).length; + }, + getComplexWords: function getComplexWords(input) { + var _this = this; + + return input.trim().replace(/[^\w\s]/ig, '').split(' ').reduce(function (a, b) { + return a + (_this.getSyllableCount(b) >= 3 ? 1 : 0); + }, 0); + }, + automatedReadability: function automatedReadability(input) { + // 4.71(characters / words) + 0.5(words / sentences) - 21.43 + var nChar = this.getCharacterCount(input); + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + var arIdx = 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + return arIdx > 0 ? arIdx : 0; + }, + smog: function smog(input) { + // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) + var nSent = this.getSentenceCount(input); + var nComp = this.getComplexWords(input); + + return 1.0430 * Math.sqrt(nComp * (30 / nSent) + 3.1291); + }, + gunningFog: function gunningFog(input) { + // 0.4[(words/sentences) + 100(complex words/words)] + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + var nComp = this.getComplexWords(input); + + return 0.4 * (nWord / nSent) + 100 * (nComp / nWord); + }, + colemanLiau: function colemanLiau(input) { + // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 + var nChar = this.getCharacterCount(input); + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + + return 5.89 * (nChar / nWord) - 0.3 * (nSent / nWord) - 15.8; + }, + fleschKincaid: function fleschKincaid(input) { + // ease - 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) + // grade level - 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + var nSyll = this.getSyllableCount(input); + + return { + ease: 206.835 - 1.015 * (nWord / nSent) - 84.6 * (nSyll / nWord), + gradeLevel: 0.39 * (nWord / nSent) + 11.8 * (nSyll / nWord) - 15.59 + }; + } +}; \ No newline at end of file diff --git a/gulpfile.babel.js b/gulpfile.babel.js new file mode 100644 index 0000000..705deaa --- /dev/null +++ b/gulpfile.babel.js @@ -0,0 +1,19 @@ +import gulp from 'gulp'; +import babel from 'gulp-babel'; +import path from 'path'; +import rename from 'gulp-rename'; + +const paths = { + src: path.join(__dirname, 'src'), + build: path.join(__dirname, 'build'), +}; + +gulp.task('default', ['build:js']); +gulp.task('build:js', () => { + gulp.src(path.join(paths.src, 'index.es6.js')) + .pipe(babel()) + .pipe(rename('index.js')) + .pipe(gulp.dest(paths.build)) +}); + +gulp.watch(path.join(paths.src, '**/*'), ['build:js']); diff --git a/index.html b/index.html new file mode 100644 index 0000000..649091e --- /dev/null +++ b/index.html @@ -0,0 +1,205 @@ + + + + + Example + + + + +
+

Readability

+ +
+
+ + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..ff06b76 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "readability", + "version": "0.0.1", + "description": "Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Liau", + "main": "./build/index.js", + "dependencies": { + "babel": "^5.8.23", + "gulp": "^3.9.0", + "gulp-babel": "^5.2.1", + "gulp-rename": "^1.2.2", + "path": "^0.12.7" + }, + "devDependencies": { + "gulp-rename": "^1.2.2", + "gulp-util": "^3.0.6", + "path": "^0.12.7" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/brbcoding/Readability.git" + }, + "author": "Cody Henshaw", + "license": "ISC", + "bugs": { + "url": "https://github.com/brbcoding/Readability/issues" + }, + "homepage": "https://github.com/brbcoding/Readability#readme" +} diff --git a/readability.js b/readability.js deleted file mode 100644 index 30e76f5..0000000 --- a/readability.js +++ /dev/null @@ -1,124 +0,0 @@ -var CHAR_REGEX = /\w/g; -var WORD_REGEX = /\S+/g; -var SENT_REGEX = /[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi; - -// syllables in word -var countSyllables = function(word) { - word = word.toLowerCase(); - if(word.length <= 3) { return 1; } - word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); - word = word.replace(/^y/, ''); - return word.match(/[aeiouy]{1,2}/g).length; -}; - -// Automated Readability Index -var automatedReadability = function(input) { - // get counts - var charCount = input.match(CHAR_REGEX).length; - var wordCount = input.match(WORD_REGEX).length; - var sentCount = input.match(SENT_REGEX).length; - // 4.71(characters/words) + 0.5(words/sentences) - 21.43 - var ariResult = 4.71 * (charCount / wordCount) + 0.5 * (wordCount / sentCount) - 21.43; - console.log("Characters: " + charCount); - console.log("Words: " + wordCount); - console.log("Sentences: " + sentCount); - console.log("Automated Readability Index: " + ariResult); - return ariResult; -}; - -// Flesch-Kincaid Reading Ease -var fleschKincaidEase = function(input) { - // 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) - // split input into individual words, then get the total syllables - var syllCount = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - syllCount += countSyllables(words[i]); - } - var fkeResult = 206.835 - 1.015 * (wordCount / sentCount) - 84.6 * (syllCount / wordCount); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Syllables: " + syllCount); - console.log("Flesch Kincaid Reading Ease: " + fkeResult); - return fkeResult; -}; - -// Flesch-Kincaid Grade Level -var fleschKincaidGradeLevel = function(input) { - // 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 - var syllCount = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - syllCount += countSyllables(words[i]); - } - var fkgResult = 0.39 * (wordCount / sentCount) + 11.8 * (syllCount / wordCount) - 15.59; - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Syllables: " + syllCount); - console.log("Flesch Kincaid Grade Level: " + fkgResult); - return fkgResult; - -}; - -// Gunning Fog Index -var gunningFog = function(input) { - // 0.4[(words/sentences) + 100(complex words/words)] - var compWords = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - if(countSyllables(words[i]) >= 3) { - compWords += 1; - } - } - var gfsResult = 0.4 * ((wordCount/sentCount) + 100 * (compWords/wordCount)); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Complex Words: " + compWords); - console.log("Gunning Fog Score: " + gfsResult); - return gfsResult; - -}; -// SMOG Index -var smogIndex = function(input) { - // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) - var polyWords = 0; - var sentCount = input.match(SENT_REGEX).length; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - for(i = 0; i < words.length; i++){ - if(countSyllables(words[i]) >= 3) { - polyWords += 1; - } - } - var smgResult = 1.0430 * Math.sqrt(polyWords * (30/sentCount) + 3.1291); - console.log("Total of Sentences: " + sentCount); - console.log("Polysyllabic Words: " + polyWords); - console.log("SMOG index: " + smgResult); - return smgResult; -}; - -// Coleman-Liau Index -var colemanLiau = function(input) { - // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 - var charCount = input.match(CHAR_REGEX).length; - var wordCount = input.match(WORD_REGEX).length; - var sentCount = input.match(SENT_REGEX).length; - var cliResult = 5.89 * (charCount / wordCount) - 0.3 * (sentCount / wordCount) - 15.8; - console.log("Total Characters: " + charCount); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Coleman Liau Index: " + cliResult); - return cliResult; -}; - -// automatedReadability("I immediately regret this decision."); -// fleschKincaidEase("I’ll have a Manhattan. And kick the vermouth to the side with a pair of steel-toed boots."); -fleschKincaidGradeLevel("Timmy's costume unboxing event is now LIVE! Join us in chat & get the chance to win mystery prizes"); -// gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); -// smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."); \ No newline at end of file diff --git a/src/index.es6.js b/src/index.es6.js new file mode 100644 index 0000000..962b9c6 --- /dev/null +++ b/src/index.es6.js @@ -0,0 +1,66 @@ +const Readability = { + getSyllableCount(input) { + // lol @ this, obviously not 100% + return input.trim().split(' ').reduce((a, b) => { + return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '') + .replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); + }, 0); + }, + getCharacterCount(input) { + return input.trim().match(/\w/g).length; + }, + getWordCount(input) { + return input.trim().match(/\S+/g).length; + }, + getSentenceCount(input) { + return input.trim().match(/[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi).length; + }, + getComplexWords(input) { + return input.trim().replace(/[^\w\s]/ig, '').split(' ').reduce((a, b) => { + return a + (this.getSyllableCount(b) >= 3 ? 1 : 0); + }, 0); + }, + automatedReadability(input) { + // 4.71(characters / words) + 0.5(words / sentences) - 21.43 + const nChar = this.getCharacterCount(input); + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + const arIdx = 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + return arIdx > 0 ? arIdx : 0; + }, + smog(input) { + // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) + const nSent = this.getSentenceCount(input); + const nComp = this.getComplexWords(input); + + return 1.0430 * Math.sqrt(nComp * (30 / nSent) + 3.1291); + }, + gunningFog(input) { + // 0.4[(words/sentences) + 100(complex words/words)] + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + const nComp = this.getComplexWords(input); + + return 0.4 * (nWord / nSent) + 100 * (nComp / nWord); + }, + colemanLiau(input) { + // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 + const nChar = this.getCharacterCount(input); + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + + return 5.89 * (nChar / nWord) - 0.3 * (nSent / nWord) - 15.8; + }, + fleschKincaid(input) { + // ease - 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) + // grade level - 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + const nSyll = this.getSyllableCount(input); + + return { + ease: 206.835 - 1.015 * (nWord / nSent) - 84.6 * (nSyll / nWord), + gradeLevel: 0.39 * (nWord / nSent) + 11.8 * (nSyll / nWord) - 15.59 + } + }, +};