diff --git a/src/DaveChild/TextStatistics/Syllables.php b/src/DaveChild/TextStatistics/Syllables.php index 0ae6edd..4c6d09e 100644 --- a/src/DaveChild/TextStatistics/Syllables.php +++ b/src/DaveChild/TextStatistics/Syllables.php @@ -356,6 +356,7 @@ public static function totalSyllables($strText, $strEncoding = '') public static function averageSyllablesPerWord($strText, $strEncoding = '') { $intSyllableCount = 0; + $strText = preg_replace('/\s+/', ' ', trim($strText)); $intWordCount = Text::wordCount($strText, $strEncoding); $arrWords = explode(' ', $strText); for ($i = 0; $i < $intWordCount; $i++) { @@ -375,6 +376,7 @@ public static function averageSyllablesPerWord($strText, $strEncoding = '') public static function wordsWithThreeSyllables($strText, $blnCountProperNouns = true, $strEncoding = '') { $intLongWordCount = 0; + $strText = preg_replace('/\s+/', ' ', trim($strText)); $intWordCount = Text::wordCount($strText, $strEncoding); $arrWords = explode(' ', $strText); for ($i = 0; $i < $intWordCount; $i++) { diff --git a/tests/TextStatisticsTest.php b/tests/TextStatisticsTest.php index 45b57dd..023f4bf 100644 --- a/tests/TextStatisticsTest.php +++ b/tests/TextStatisticsTest.php @@ -192,6 +192,28 @@ public function testAverageSyllablesPerWord() $this->assertEquals(1, $this->TextStatistics->averageSyllablesPerWord('and then there was one')); $this->assertEquals(2, $this->TextStatistics->averageSyllablesPerWord('because special ducklings deserve rainbows')); $this->assertEquals(1.5, $this->TextStatistics->averageSyllablesPerWord('and then there was one because special ducklings deserve rainbows')); + // Regression: non-space whitespace must not cause undefined array key warnings + $this->assertEquals( + $this->TextStatistics->averageSyllablesPerWord('and then'), + $this->TextStatistics->averageSyllablesPerWord("and\nthen") + ); + $this->assertEquals( + $this->TextStatistics->averageSyllablesPerWord('and then'), + $this->TextStatistics->averageSyllablesPerWord("and\tthen") + ); + $this->assertEquals( + $this->TextStatistics->averageSyllablesPerWord('and then'), + $this->TextStatistics->averageSyllablesPerWord("and\n\nthen") + ); + } + + public function testWordsWithThreeSyllablesWhitespace() + { + // Regression: non-space whitespace must produce the same result as spaces + $this->assertEquals( + DaveChild\TextStatistics\Syllables::wordsWithThreeSyllables('there is just one', true), + DaveChild\TextStatistics\Syllables::wordsWithThreeSyllables("there\nis\njust\none", true) + ); } /* Test Words