Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
58 changes: 58 additions & 0 deletions src/DaveChild/TextStatistics/Syllables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
54 changes: 53 additions & 1 deletion src/DaveChild/TextStatistics/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
60 changes: 59 additions & 1 deletion src/DaveChild/TextStatistics/TextStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -610,4 +668,4 @@ public function spache_readability_score($strText = false)
{
return $this->spacheReadabilityScore($strText);
}
}
}