Skip to content

Commit

Permalink
Flesch Reading Ease check (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 11, 2023
1 parent 2d95789 commit d8e37ac
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions src/Checks/Content/FleschReadingEaseCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Vormkracht10\Seo\Checks\Content;

use Illuminate\Http\Client\Response;
use Symfony\Component\DomCrawler\Crawler;
use Vormkracht10\Seo\Interfaces\Check;
use Vormkracht10\Seo\Traits\PerformCheck;

class FleschReadingEaseCheck implements Check
{
use PerformCheck;

public string $title = 'Flesch Reading Ease';

public string $priority = 'medium';

public int $timeToFix = 45;

public int $scoreWeight = 5;

public bool $continueAfterFailure = true;

public ?string $failureReason;

public mixed $actualValue = null;

public mixed $expectedValue = null;

public function check(Response $response, Crawler $crawler): bool
{
if (! $this->validateContent($crawler)) {
return false;
}

return true;
}

public function validateContent(Crawler $crawler): bool
{
$sentences = $this->getSentencesFromCrawler($crawler);

$sentences = $this->separateSentencesByDot($sentences);

// Average word count per sentence
$averageWordCount = $this->getAverageWordCountPerSentence($sentences);

// Average syllable count per word
$averageSyllableCount = $this->getAverageSyllableCountPerWord($sentences);

dd($averageSyllableCount, $averageWordCount);

// return true;
}

private function getAverageWordCountPerSentence(array $sentences): int
{
$totalWordCount = 0;

foreach ($sentences as $sentence) {
$totalWordCount += str_word_count($sentence);
}

return round($totalWordCount / count($sentences), 0, PHP_ROUND_HALF_UP);

Check failure on line 64 in src/Checks/Content/FleschReadingEaseCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\Seo\Checks\Content\FleschReadingEaseCheck::getAverageWordCountPerSentence() should return int but returns float.
}

private function getAverageSyllableCountPerWord(array $sentences): int
{
$totalSyllableCount = 0;

foreach ($sentences as $sentence) {
$words = explode(' ', $sentence);

foreach ($words as $word) {
$totalSyllableCount += $this->averageSyllablesPerWord($word);
}
}

return round($totalSyllableCount / count($sentences), 0, PHP_ROUND_HALF_UP);

Check failure on line 79 in src/Checks/Content/FleschReadingEaseCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\Seo\Checks\Content\FleschReadingEaseCheck::getAverageSyllableCountPerWord() should return int but returns float.
}

private function countSyllables($word)
{
$vowels = array('a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y');
$syllables = 0;
$prevChar = null;

for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (in_array($char, $vowels) && ($prevChar === null || !in_array($prevChar, $vowels))) {
$syllables++;
}
$prevChar = $char;
}

return max(1, $syllables); // Ensure at least one syllable for non-empty word
}

private function averageSyllablesPerWord($text)
{
$words = preg_split('/\s+/', $text);
$totalSyllables = array_reduce($words, function ($carry, $word) {
return $carry + $this->countSyllables($word);
}, 0);
$totalWords = count($words);

if ($totalWords > 0) {
$averageSyllables = $totalSyllables / $totalWords;
return $averageSyllables;
} else {
return 0; // Handle case of empty input
}
}

private function separateSentencesByDot(array $sentences): array
{
$newSentences = [];

foreach ($sentences as $sentence) {
$sentence = explode('.', $sentence);
$newSentences = array_merge($newSentences, $sentence);
}

// Remove all sentences that are empty
$sentences = array_filter($newSentences, function ($sentence) {
return ! empty($sentence);
});

return $sentences;
}

private function getSentencesFromCrawler(Crawler $crawler): array
{
$content = $crawler->filterXPath('//body')->children();

// Get all elements that contain text
$content = $content->filterXPath('//*/text()[normalize-space()]');

$content = $content->each(function (Crawler $node, $i) {
return $node->text();
});

return $content;
}

private function calculateSentencesWithTooManyWords(array $sentences): array

Check failure on line 146 in src/Checks/Content/FleschReadingEaseCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\Seo\Checks\Content\FleschReadingEaseCheck::calculateSentencesWithTooManyWords() is unused.
{
$tooLongSentences = [];

foreach ($sentences as $sentence) {
if (str_word_count($sentence) > 20) {
$tooLongSentences[] = $sentence;
}
}

return $tooLongSentences;
}
}

0 comments on commit d8e37ac

Please sign in to comment.