*/ public static function score( string $text, string $language = '' ): array { $declared = strtolower( substr( '' !== $language ? $language : (string) get_locale(), 0, 2 ) ); $text = trim( (string) preg_replace( '/\s+/u', ' ', wp_strip_all_tags( $text ) ) ); // What WordPress calls the site language and what the page is written // in are different questions. A German shop on an en_US install is // ordinary, and scoring its copy with the English formula costs it // about fifteen points and the word "Hard" — so the text gets a vote, // and only a confident one overrides what was declared. $detected = self::detect( $text ); $language = '' !== $detected && $detected !== $declared ? $detected : $declared; $language_source = $language === $declared ? 'declared' : 'detected'; $empty = array( 'language' => $language, 'language_source' => $language_source, 'flesch' => 0, 'grade' => 0, 'label' => '', 'words' => 0, 'sentences' => 0, 'syllables' => 0, 'words_per_sentence' => 0.0, 'long_sentences' => 0, 'long_share' => 0, 'paragraphs' => 0, 'long_paragraphs' => 0, 'passive' => 0, 'passive_share' => 0, 'transitions' => 0, 'transition_share' => 0, ); if ( '' === $text ) { return $empty; } $sentences = self::sentences( $text ); $words = self::words( $text ); if ( empty( $sentences ) || empty( $words ) ) { return $empty; } $syllables = 0; foreach ( $words as $word ) { $syllables += self::syllables( $word, $language ); } $word_count = count( $words ); $sentence_count = count( $sentences ); $asl = $word_count / $sentence_count; // Average sentence length. $asw = $syllables / max( 1, $word_count ); // Average syllables per word. // Amstad's German adaptation; Flesch for everything else. $flesch = 'de' === $language ? 180 - $asl - ( 58.5 * $asw ) : 206.835 - ( 1.015 * $asl ) - ( 84.6 * $asw ); $flesch = (int) round( max( 0, min( 100, $flesch ) ) ); // Flesch–Kincaid grade level: the school year a reader needs. $grade = (float) round( max( 0, ( 0.39 * $asl ) + ( 11.8 * $asw ) - 15.59 ), 1 ); $long_sentences = 0; foreach ( $sentences as $sentence ) { if ( count( self::words( $sentence ) ) > self::LONG_SENTENCE ) { ++$long_sentences; } } $paragraphs = self::paragraphs( $text ); $long_paragraphs = 0; foreach ( $paragraphs as $paragraph ) { if ( count( self::words( $paragraph ) ) > self::LONG_PARAGRAPH ) { ++$long_paragraphs; } } $passive = self::passive( $sentences, $language ); $transitions = self::transitions( $sentences, $language ); return array( 'language' => $language, 'language_source' => $language_source, 'flesch' => $flesch, 'grade' => $grade, 'label' => self::label( $flesch ), 'words' => $word_count, 'sentences' => $sentence_count, 'syllables' => $syllables, 'words_per_sentence' => (float) round( $asl, 1 ), 'long_sentences' => $long_sentences, 'long_share' => (int) round( 100 * $long_sentences / $sentence_count ), 'paragraphs' => count( $paragraphs ), 'long_paragraphs' => $long_paragraphs, 'passive' => $passive, 'passive_share' => (int) round( 100 * $passive / $sentence_count ), 'transitions' => $transitions, 'transition_share' => (int) round( 100 * $transitions / $sentence_count ), ); } /** * Which language the text is written in, or '' when it is not clear. * * Two signals, both cheap and both hard to fake: the letters German uses * and English does not (ä ö ü ß), and the function words each language * cannot write a paragraph without. A page needs a clear majority before * this overrides what the site declared — a single "der" in an English * sentence about German coffee must not flip the formula. */ public static function detect( string $text ): string { $low = function_exists( 'mb_strtolower' ) ? mb_strtolower( $text, 'UTF-8' ) : strtolower( $text ); if ( mb_strlen( $low ) < 60 ) { return ''; } $de = (int) preg_match_all( '/(?= 5 && $de > $en * 2 ) { return 'de'; } if ( $en >= 5 && $en > $de * 2 ) { return 'en'; } return ''; } /** A word for the reader, not for a linguist: "very readable", "hard going". */ public static function label( int $flesch ): string { if ( $flesch >= 80 ) { return __( 'Very easy', 'ai-rank-suite' ); } if ( $flesch >= 60 ) { return __( 'Easy', 'ai-rank-suite' ); } if ( $flesch >= 50 ) { return __( 'Fairly hard', 'ai-rank-suite' ); } if ( $flesch >= 30 ) { return __( 'Hard', 'ai-rank-suite' ); } return __( 'Very hard', 'ai-rank-suite' ); } /** * @return array */ private static function sentences( string $text ): array { $parts = preg_split( '/(?<=[.!?…])\s+(?=[\p{Lu}\p{N}"“„«\'])|(?<=[.!?…])$/u', $text, -1, PREG_SPLIT_NO_EMPTY ); $out = array(); foreach ( (array) $parts as $part ) { $part = trim( (string) $part ); if ( '' !== $part && preg_match( '/\p{L}/u', $part ) ) { $out[] = $part; } } return $out; } /** * @return array */ private static function paragraphs( string $text ): array { $parts = preg_split( '/\n{2,}|(?<=[.!?])\s{2,}/u', $text, -1, PREG_SPLIT_NO_EMPTY ); $out = array(); foreach ( (array) $parts as $part ) { $part = trim( (string) $part ); if ( '' !== $part ) { $out[] = $part; } } return empty( $out ) ? array( $text ) : $out; } /** * @return array */ private static function words( string $text ): array { preg_match_all( '/[\p{L}\p{N}][\p{L}\p{N}\'’-]*/u', $text, $m ); return $m[0] ?? array(); } /** * Syllables by vowel groups — the only method that works without a * dictionary. German counts umlauts and its diphthongs; English drops the * silent final "e" that would otherwise make "house" two syllables. */ public static function syllables( string $word, string $language = 'en' ): int { $word = function_exists( 'mb_strtolower' ) ? mb_strtolower( $word, 'UTF-8' ) : strtolower( $word ); if ( '' === $word ) { return 0; } $vowels = 'de' === $language ? 'aeiouyäöü' : 'aeiouy'; $count = (int) preg_match_all( '/[' . $vowels . ']+/u', $word ); if ( 'en' === $language || 'de' !== $language ) { // Silent final e ("make", "house"), but never in a one-syllable word. if ( $count > 1 && preg_match( '/[^aeiou]e$/u', $word ) ) { --$count; } } else { // German diphthongs (au, eu, äu, ei, ie) are one sound, and the // vowel-group count already merges them — nothing to subtract. $count = (int) preg_match_all( '/(?:eu|äu|au|ei|ai|ie|[aeiouyäöü])/u', $word ); } return max( 1, $count ); } /** * Passive constructions. English: a form of "to be" (or "get") followed by * a past participle. German: a form of "werden" plus a participle, which * is the construction German style guides warn about. * * @param array $sentences */ private static function passive( array $sentences, string $language ): int { $count = 0; foreach ( $sentences as $sentence ) { // German participles: "geröstet", but also "erhitzt", "verkauft", // "besprochen" and "montiert" — the verbs with an inseparable // prefix take no ge-, and leaving them out misses half the passive // sentences on a German page. $hit = 'de' === $language ? preg_match( '/\b(wird|werden|wurde|wurden|worden|wurdest|wirst)\b[^.!?]{0,80}?\b((?:ge|be|er|ver|ent|zer|emp|miss|über|unter)\p{L}{2,}(?:t|en)|\p{L}{3,}iert)\b/ui', $sentence ) : preg_match( '/\b(is|are|was|were|be|been|being|am|got|gets|get)\b\s+(?:\w+ly\s+)?(\w+(?:ed|en|wn|ne|de|te|ung|ade| built|made|done|held|kept|sold|told|paid|found|given|taken|written|shown|known|seen))\b/ui', $sentence ); if ( $hit ) { ++$count; } } return $count; } /** * Sentences that open with, or contain, a word that tells the reader how * this sentence relates to the last one. * * @param array $sentences */ private static function transitions( array $sentences, string $language ): int { $words = self::transition_words( $language ); $count = 0; foreach ( $sentences as $sentence ) { $low = function_exists( 'mb_strtolower' ) ? mb_strtolower( $sentence, 'UTF-8' ) : strtolower( $sentence ); foreach ( $words as $word ) { if ( preg_match( '/(? */ public static function transition_words( string $language ): array { $en = array( 'also', 'and then', 'as a result', 'because', 'besides', 'but', 'consequently', 'finally', 'first', 'for example', 'for instance', 'furthermore', 'however', 'in addition', 'in contrast', 'in fact', 'instead', 'later', 'likewise', 'meanwhile', 'moreover', 'nevertheless', 'next', 'on the other hand', 'otherwise', 'second', 'similarly', 'since', 'so', 'still', 'that is', 'then', 'therefore', 'though', 'thus', 'to sum up', 'while', 'yet', ); $de = array( 'also', 'allerdings', 'andererseits', 'außerdem', 'beispielsweise', 'daher', 'danach', 'dann', 'darüber hinaus', 'dementsprechend', 'denn', 'deshalb', 'deswegen', 'doch', 'ebenso', 'endlich', 'erstens', 'folglich', 'genauso', 'hingegen', 'infolgedessen', 'insbesondere', 'jedoch', 'kurz gesagt', 'nämlich', 'schließlich', 'sobald', 'sondern', 'sowie', 'stattdessen', 'trotzdem', 'übrigens', 'weil', 'weiterhin', 'zudem', 'zum beispiel', 'zweitens', ); /** * Filter the transition words counted for a language. * * @param array $words Lower-case words and phrases. * @param string $language Two-letter code. */ return (array) apply_filters( 'airanksuite/readability/transition_words', 'de' === $language ? $de : $en, $language ); } }