Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/DaveChild/TextStatistics/Syllables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down
22 changes: 22 additions & 0 deletions tests/TextStatisticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down