From 8a6d63fbc13a0c0cc74661f9a5ec0bcb06d6e14b Mon Sep 17 00:00:00 2001 From: Nio-av Date: Thu, 22 Jun 2017 13:42:17 +0200 Subject: [PATCH 1/3] Ad Laesbarhetsindex. Please check Text::wordsWithSevenLetters Not quite shure wether calculation of wordsWithSevenLetters works correct. Results seams to be good. --- README.md | 1 + src/DaveChild/TextStatistics/Text.php | 54 ++++++++++++++++++- .../TextStatistics/TextStatistics.php | 29 +++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c91db64..adac673 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ It allows you to measure the readability of text using common scoring systems, i * 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/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..2efd0e7 100644 --- a/src/DaveChild/TextStatistics/TextStatistics.php +++ b/src/DaveChild/TextStatistics/TextStatistics.php @@ -397,6 +397,33 @@ 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); + } + } + /** * Returns the number of words NOT on the Dale-Chall easy word list * @param boolean|string $strText Text to be measured @@ -610,4 +637,4 @@ public function spache_readability_score($strText = false) { return $this->spacheReadabilityScore($strText); } -} +} \ No newline at end of file From 29dba63aee90a623d182f87ae8a7863ad8091414 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 7 Aug 2017 12:01:41 +0200 Subject: [PATCH 2/3] Include Forcast readability --- README.md | 1 + src/DaveChild/TextStatistics/Syllables.php | 29 +++++++++++++++++ .../TextStatistics/TextStatistics.php | 31 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/README.md b/README.md index adac673..041b1ca 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ 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 diff --git a/src/DaveChild/TextStatistics/Syllables.php b/src/DaveChild/TextStatistics/Syllables.php index 5686188..fea635c 100644 --- a/src/DaveChild/TextStatistics/Syllables.php +++ b/src/DaveChild/TextStatistics/Syllables.php @@ -407,4 +407,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/TextStatistics.php b/src/DaveChild/TextStatistics/TextStatistics.php index 2efd0e7..948d8c4 100644 --- a/src/DaveChild/TextStatistics/TextStatistics.php +++ b/src/DaveChild/TextStatistics/TextStatistics.php @@ -424,6 +424,37 @@ public function laesbarhetsindex($strText = false) } } + /** + * 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 From 65c48e0127943c585fccb04251ace5facdc07a9f Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 7 Aug 2017 12:04:39 +0200 Subject: [PATCH 3/3] Include polysyllabicWords --- src/DaveChild/TextStatistics/Syllables.php | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/DaveChild/TextStatistics/Syllables.php b/src/DaveChild/TextStatistics/Syllables.php index fea635c..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