diff --git a/README.md b/README.md index c91db64..041b1ca 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,14 @@ The PHP Text Statistics class will help you to indentify issues with your websit It allows you to measure the readability of text using common scoring systems, including: * Flesch Kincaid Reading Ease * Flesch Kincaid Grade Level +* Forcast * Gunning Fog Score * Coleman Liau Index * SMOG Index * Automated Reability Index * Dale-Chall Readability Score * Spache Readability Score +* Läsbarhetsindex (Calculation might not be correct. Feel free to correct. See: Text::wordsWithSevenLetters) One of the biggest challenges with measuring text readability is the counting of syllables, which can be tricky to work out. There are rules in the Statistics class for working out the syllable count of words, and a large list of words to test these rules against. diff --git a/src/DaveChild/TextStatistics/Syllables.php b/src/DaveChild/TextStatistics/Syllables.php index 5686188..7c28ef8 100644 --- a/src/DaveChild/TextStatistics/Syllables.php +++ b/src/DaveChild/TextStatistics/Syllables.php @@ -393,6 +393,35 @@ public static function wordsWithThreeSyllables($strText, $blnCountProperNouns = return $intLongWordCount; } + /** + * Returns the number of polysyllabic Words + * @param string $strText Text to be measured + * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count + * @param string $strEncoding Encoding of text + * @return int + */ + public static function polysyllabicWords($strText, $blnCountProperNouns = true, $strEncoding = '') + { + $intLongWordCount = 0; + $intWordCount = Text::wordCount($strText, $strEncoding); + $arrWords = explode(' ', $strText); + for ($i = 0; $i < $intWordCount; $i++) { + if (Syllables::syllableCount($arrWords[$i], $strEncoding) > 1) { + if ($blnCountProperNouns) { + $intLongWordCount++; + } else { + $strFirstLetter = Text::substring($arrWords[$i], 0, 1, $strEncoding); + if ($strFirstLetter !== Text::upperCase($strFirstLetter, $strEncoding)) { + // First letter is lower case. Count it. + $intLongWordCount++; + } + } + } + } + + return $intLongWordCount; + } + /** * Returns the percentage of words with more than three syllables * @param string $strText Text to be measured @@ -407,4 +436,33 @@ public static function percentageWordsWithThreeSyllables($strText, $blnCountProp return $intPercentage; } + + /** + * Returns the number of words with one syllables + * @param string $strText Text to be measured + * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count + * @param string $strEncoding Encoding of text + * @return int + */ + public static function wordsWithOneSyllable($strText, $blnCountProperNouns = true, $strEncoding = '') + { + $intMonosyllabicWordCount = 0; + $intWordCount = Text::wordCount($strText, $strEncoding); + $arrWords = explode(' ', $strText); + for ($i = 0; $i < $intWordCount; $i++) { + if (self::syllableCount($arrWords[$i], $strEncoding) == 1) { + if ($blnCountProperNouns) { + $intMonosyllabicWordCount++; + } else { + $strFirstLetter = Text::substring($arrWords[$i], 0, 1, $strEncoding); + if ($strFirstLetter !== Text::upperCase($strFirstLetter, $strEncoding)) { + // First letter is lower case. Count it. + $intMonosyllabicWordCount++; + } + } + } + } + + return $intMonosyllabicWordCount; + } } diff --git a/src/DaveChild/TextStatistics/Text.php b/src/DaveChild/TextStatistics/Text.php index 303582d..92a395c 100644 --- a/src/DaveChild/TextStatistics/Text.php +++ b/src/DaveChild/TextStatistics/Text.php @@ -325,4 +325,56 @@ public static function averageWordsPerSentence($strText, $strEncoding = '') $averageWords = (Maths::bcCalc($intWordCount, '/', $intSentenceCount)); return $averageWords; } -} + + /** + * Returns the number of words with more than six letters + * @param string $strText Text to be measured + * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count + * @param string $strEncoding Encoding of text + * @return int + */ + public static function wordsWithSevenLetters($strText, $blnCountProperNouns = true, $strEncoding = '') + { + $intLongWordCount = 0; + $intWordCount = self::wordCount($strText, $strEncoding); + $arrWords = explode(' ', $strText); + for ($i = 0; $i < $intWordCount; $i++) { + if (self::letterCount($arrWords[$i], $strEncoding) > 6) { + //ToDo: Please check "Syllables::wordsWithThreeSyllables": Is there a need for "$blnCountProperNouns"? + //ToDo: If so: Uncomment the following; If not: Clean Up code (delete "$blnCountProperNouns") + /* + if ($blnCountProperNouns) { + $intLongWordCount++; + } else { + $strFirstLetter = self::substring($arrWords[$i], 0, 1, $strEncoding); + if ($strFirstLetter !== Text::upperCase($strFirstLetter, $strEncoding)) { + // First letter is lower case. Count it. + $intLongWordCount++; + } + } + */ + $intLongWordCount++; + + } + } + return $intLongWordCount; + } + + + /** + * Returns the percentage of words with more than six letters + * @param string $strText Text to be measured + * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count + * @return int|float + */ + public static function percentageWordsWithSevenLetters($strText, $blnCountProperNouns = true, $strEncoding = '') + { + $intWordCount = self::wordCount($strText, $strEncoding); + $intLongWordCount = self::wordsWithSevenLetters($strText, $blnCountProperNouns, $strEncoding); + $intPercentage = Maths::bcCalc( + $intLongWordCount, + '/', + $intWordCount); + return $intPercentage; + } +} \ No newline at end of file diff --git a/src/DaveChild/TextStatistics/TextStatistics.php b/src/DaveChild/TextStatistics/TextStatistics.php index a32e225..948d8c4 100644 --- a/src/DaveChild/TextStatistics/TextStatistics.php +++ b/src/DaveChild/TextStatistics/TextStatistics.php @@ -397,6 +397,64 @@ public function spacheReadabilityScore($strText = false) } } + /** + * Gives the Laesbarhetsindex of text entered + * @param boolean|string $strText Text to be checked + * @return int|float + */ + public function laesbarhetsindex($strText = false) + { + $strText = $this->setText($strText); + + $score = Maths::bcCalc( + Maths::bcCalc( + 100, + '*', + Text::percentageWordsWithSevenLetters($strText, false, $this->strEncoding) + ), + '+', + Text::averageWordsPerSentence($strText, $this->strEncoding) + ); + + //ToDo: Does this work correct? Please Test and fix. + if ($this->normalise) { + return Maths::normaliseScore($score, 0, 100, $this->dps); + } else { + return Maths::bcCalc($score, '+', 0, true, $this->dps); + } + } + + /** + * Gives the FORCAST of text entered + * @param boolean|string $strText Text to be checked + * @return int|float + */ + public function forcast($strText = false) + { + $strText = $this->setText($strText); + + $score = Maths::bcCalc( + 20, + '-', + Maths::bcCalc( + 15, + '*', + Maths::bcCalc( + Syllables::wordsWithOneSyllable($strText, false, $this->strEncoding), + '/', + Text::wordCount($strText , $this->strEncoding) + ) + ) + ); + +//TODO: Does this work correct? Please Test and fix. + if ($this->normalise) { + return Maths::normaliseScore($score, 0, 12, $this->dps); + } else { + return Maths::bcCalc($score, '+', 0, true, $this->dps); + } + } + /** * Returns the number of words NOT on the Dale-Chall easy word list * @param boolean|string $strText Text to be measured @@ -610,4 +668,4 @@ public function spache_readability_score($strText = false) { return $this->spacheReadabilityScore($strText); } -} +} \ No newline at end of file