Skip to content

Commit fbd33bd

Browse files
committed
Fixes, demo
1 parent 9d32430 commit fbd33bd

15 files changed

Lines changed: 115 additions & 44 deletions

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@
5959
}
6060
},
6161
"scripts": {
62-
"test": "phpunit",
63-
"test:coverage": "phpunit --coverage-html=build/coverage",
64-
"analyse": "phpstan analyse",
65-
"cs:check": "php-cs-fixer check",
66-
"cs:fix": "php-cs-fixer fix",
62+
"test": "vendor/bin/phpunit",
63+
"test:coverage": "vendor/bin/phpunit --coverage-html=build/coverage",
64+
"analyse": "php -d memory_limit=512M vendor/bin/phpstan analyse",
65+
"analyse:no-mem": "phpstan analyse",
66+
"cs:check": "vendor/bin/php-cs-fixer check",
67+
"cs:fix": "vendor/bin/php-cs-fixer fix",
6768
"check": [
6869
"@cs:check",
6970
"@analyse",

examples/demo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function analyzeSingle(Engine $engine, string $text): void
8585
{
8686
// Syllable breakdown
8787
section('Syllable Analysis');
88-
$words = ['banana', 'extraordinary', 'university', 'communication', 'readability'];
88+
$words = ['banana', 'character', 'incredible', 'recognition', 'automatic'];
8989
echo " Word Syllables Hyphenation\n";
9090
echo " ──── ───────── ───────────\n";
9191
foreach ($words as $word) {

src/Formula/ColemanLiau.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
4040
languageCode: $language->code,
4141
score: \round($score, 1),
4242
gradeLevel: \min(\max(\round($score, 1), 0.0), 18.0),
43-
interpretation: '',
43+
interpretation: $this->interpret($score),
4444
gradeLabel: null,
4545
inputs: [
4646
'L' => \round($L, 2),
@@ -51,4 +51,25 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
5151
],
5252
);
5353
}
54+
55+
private function interpret(float $score): string
56+
{
57+
return match (true) {
58+
$score <= 1.0 => 'Kindergarten',
59+
$score <= 2.0 => '1st Grade',
60+
$score <= 3.0 => '2nd Grade',
61+
$score <= 4.0 => '3rd Grade',
62+
$score <= 5.0 => '4th Grade',
63+
$score <= 6.0 => '5th Grade',
64+
$score <= 7.0 => '6th Grade',
65+
$score <= 8.0 => '7th Grade',
66+
$score <= 9.0 => '8th Grade',
67+
$score <= 10.0 => '9th Grade',
68+
$score <= 11.0 => '10th Grade',
69+
$score <= 12.0 => '11th Grade',
70+
$score <= 13.0 => '12th Grade',
71+
$score <= 16.0 => 'College',
72+
default => 'Graduate',
73+
};
74+
}
5475
}

src/Formula/Crawford.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
4040
languageCode: $language->code,
4141
score: \round($score, 1),
4242
gradeLevel: null,
43-
interpretation: '',
43+
interpretation: $this->interpret($score),
4444
gradeLabel: null,
4545
inputs: [
4646
'avgLettersPerWord' => \round($averageLetters, 2),
4747
'sentencesPer100Words' => \round($sentencesPer100, 2),
4848
],
4949
);
5050
}
51+
52+
private function interpret(float $score): string
53+
{
54+
return match (true) {
55+
$score >= 9.0 => 'Very Easy',
56+
$score >= 7.0 => 'Easy',
57+
$score >= 5.0 => 'Standard',
58+
$score >= 3.0 => 'Difficult',
59+
default => 'Very Difficult',
60+
};
61+
}
5162
}

src/Formula/DaleChall.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,7 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
4747

4848
private function estimateDifficultPercentage(TextStatistics $stats): float
4949
{
50-
if ($stats->wordCount === 0) {
51-
return 0.0;
52-
}
53-
54-
$easyWordCount = $stats->syllableHistogram[1] ?? 0;
55-
$difficultCount = $stats->wordCount - $easyWordCount;
56-
57-
if ($difficultCount < 0) {
58-
$difficultCount = 0;
59-
}
60-
61-
return ($difficultCount / $stats->wordCount) * 100.0;
50+
return TextStatisticsHelper::estimateDifficultPercentage($stats);
6251
}
6352

6453
private function interpret(float $score): string

src/Formula/FleschReadingEase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
5050
languageCode: $language->code,
5151
score: \round($score, 1),
5252
gradeLevel: null,
53-
interpretation: '',
54-
gradeLabel: $this->interpret($score),
53+
interpretation: $this->interpret($score),
54+
gradeLabel: null,
5555
inputs: [
5656
'asl' => $stats->averageWordsPerSentence,
5757
'asw' => $stats->averageSyllablesPerWord,

src/Formula/GutierrezPolini.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,23 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
3636
languageCode: $language->code,
3737
score: \round($score, 1),
3838
gradeLevel: null,
39-
interpretation: '',
39+
interpretation: $this->interpret($score),
4040
gradeLabel: null,
4141
inputs: [
4242
'lettersPerWord' => \round($stats->letterCount / $wordCount, 2),
4343
'wordsPerSentence' => \round($stats->averageWordsPerSentence, 2),
4444
],
4545
);
4646
}
47+
48+
private function interpret(float $score): string
49+
{
50+
return match (true) {
51+
$score >= 80.0 => 'Very Easy',
52+
$score >= 70.0 => 'Easy',
53+
$score >= 50.0 => 'Standard',
54+
$score >= 30.0 => 'Difficult',
55+
default => 'Very Difficult',
56+
};
57+
}
4758
}

src/Formula/Osman.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
3434
$avgLetters = $stats->letterCount / $wordCount;
3535
$hardWordsPct = ($stats->polysyllableCount / $wordCount) * 100.0;
3636

37-
$score = 200.0 - 2.0 * $asl - 1.5 * $avgLetters * 100.0 - 0.4 * $hardWordsPct;
37+
$score = 200.0 - 2.0 * $asl - 1.5 * $avgLetters - 0.4 * $hardWordsPct;
3838

3939
return new FormulaResult(
4040
formulaName: $this->name(),

src/Formula/SmogIndex.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,33 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
3636
languageCode: $language->code,
3737
score: \round($score, 1),
3838
gradeLevel: \min(\max(\round($score, 1), 0.0), 18.0),
39-
interpretation: '',
39+
interpretation: $this->interpret($score),
4040
gradeLabel: null,
4141
inputs: [
4242
'polysyllableCount' => $stats->polysyllableCount,
4343
'sentenceCount' => $stats->sentenceCount,
4444
],
4545
);
4646
}
47+
48+
private function interpret(float $score): string
49+
{
50+
return match (true) {
51+
$score <= 1.0 => 'Kindergarten',
52+
$score <= 2.0 => '1st Grade',
53+
$score <= 3.0 => '2nd Grade',
54+
$score <= 4.0 => '3rd Grade',
55+
$score <= 5.0 => '4th Grade',
56+
$score <= 6.0 => '5th Grade',
57+
$score <= 7.0 => '6th Grade',
58+
$score <= 8.0 => '7th Grade',
59+
$score <= 9.0 => '8th Grade',
60+
$score <= 10.0 => '9th Grade',
61+
$score <= 11.0 => '10th Grade',
62+
$score <= 12.0 => '11th Grade',
63+
$score <= 13.0 => '12th Grade',
64+
$score <= 16.0 => 'College',
65+
default => 'Graduate',
66+
};
67+
}
4768
}

src/Formula/Spache.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,7 @@ public function calculate(TextStatistics $stats, Language $language): FormulaRes
4545

4646
private function estimateDifficultPercentage(TextStatistics $stats): float
4747
{
48-
if ($stats->wordCount === 0) {
49-
return 0.0;
50-
}
51-
52-
$easyWordCount = $stats->syllableHistogram[1] ?? 0;
53-
$difficultCount = $stats->wordCount - $easyWordCount;
54-
55-
if ($difficultCount < 0) {
56-
$difficultCount = 0;
57-
}
58-
59-
return ($difficultCount / $stats->wordCount) * 100.0;
48+
return TextStatisticsHelper::estimateDifficultPercentage($stats);
6049
}
6150

6251
private function interpret(float $score): string

0 commit comments

Comments
 (0)