| @@ -202,11 +202,12 @@ | ||
| 202 | 202 | // Trim and limit length for AI processing |
| 203 | 203 | $content = trim($content); |
| 204 | 204 | $max_length = 4000; // Reasonable limit for AI processing |
| 205 | 205 | |
| 206 | - if (strlen($content) > $max_length) { | |
| 207 | - $content = substr($content, 0, $max_length) . '...'; | |
| 208 | - } | |
| 206 | + // strlen()/substr() count BYTES: on Thai or CJK this handed the model a | |
| 207 | + // third of the intended content, and cut the last character in half so | |
| 208 | + // the payload carried an invalid UTF-8 sequence (#687). | |
| 209 | + $content = \ThinkRank\Core\Seo_Text::trim_to_length($content, $max_length); | |
| 209 | 210 | |
| 210 | 211 | return $content; |
| 211 | 212 | } |
| 212 | 213 | |
| @@ -358,9 +359,27 @@ | ||
| 358 | 359 | $words = str_word_count($text, 1); |
| 359 | 360 | $syllables = 0; |
| 360 | 361 | |
| 361 | 362 | foreach ($words as $word) { |
| 362 | - $syllables += max(1, preg_match_all('/[aeiouy]+/i', $word)); | |
| 363 | + $word = preg_replace('/[^a-z]/', '', strtolower($word)); | |
| 364 | + if ($word === '') { | |
| 365 | + continue; | |
| 366 | + } | |
| 367 | + | |
| 368 | + $groups = preg_match_all('/[aeiouy]+/', $word); | |
| 369 | + | |
| 370 | + // Standard Flesch heuristic: a trailing silent e does not form a | |
| 371 | + // syllable ("make", "time", "these") — but only when a consonant | |
| 372 | + // precedes it (a vowel+e ending like "movie" already shares its | |
| 373 | + // group) and never for consonant-le ("table"), which does count. | |
| 374 | + // Without this the counter inflated syllables/word by ~0.2-0.3 on | |
| 375 | + // ordinary prose, driving raw Flesch negative and the UI to a | |
| 376 | + // clamped "Very Difficult (0)" (#407). | |
| 377 | + if ($groups > 1 && preg_match('/[^aeiouy]e$/', $word) && !str_ends_with($word, 'le')) { | |
| 378 | + $groups--; | |
| 379 | + } | |
| 380 | + | |
| 381 | + $syllables += max(1, $groups); | |
| 363 | 382 | } |
| 364 | 383 | |
| 365 | 384 | return $syllables; |
| 366 | 385 | } |