= $tail_start ); $in_quote = self::in_ranges( $char_offset, $quotes ); $weight = (float) $pattern['weight']; if ( $in_tail ) { $weight *= self::TAIL_MULTIPLIER; } if ( $in_quote ) { $weight *= self::QUOTE_MULTIPLIER; } // Repeats of the same pattern taper off, so a habit repeated // twenty times cannot outrank a single piece of real evidence. $seen++; if ( $seen > self::REPEAT_FULL ) { $weight *= self::REPEAT_DECAY; } $raw_score += $weight; if ( ! isset( $categories[ $pattern['category'] ] ) ) { $categories[ $pattern['category'] ] = array( 'count' => 0, 'quoted' => 0, 'score' => 0.0, ); } $categories[ $pattern['category'] ]['count']++; $categories[ $pattern['category'] ]['score'] += $weight; if ( $in_quote ) { $categories[ $pattern['category'] ]['quoted']++; } $findings[] = array( 'id' => $pattern['id'], 'category' => $pattern['category'], 'weight' => round( $weight, 1 ), 'note' => $pattern['note'], 'match' => self::truncate( $matched_text, 120 ), 'context' => self::context( $text, $char_offset, $matched_text ), 'position' => $len > 0 ? round( ( $char_offset / $len ) * 100 ) : 0, 'in_tail' => $in_tail, 'in_quote' => $in_quote, ); } } // Document shape, which no regex can see. $structure = self::structure_signal( $raw_content ); if ( $structure['trips'] ) { $raw_score += $structure['weight']; $categories['thin'] = array( 'count' => 1, 'quoted' => 0, 'score' => $structure['weight'], ); $findings[] = array( 'id' => 'thin_list_structure', 'category' => 'thin', 'weight' => round( $structure['weight'], 1 ), 'note' => __( 'Mostly short bullet lists with little connecting prose.', 'ai-slop-checker' ), 'match' => sprintf( /* translators: 1: number of lists, 2: number of items, 3: average words per item, 4: percentage of the page. */ __( '%1$d lists, %2$d items averaging %3$s words — %4$d%% of the page', 'ai-slop-checker' ), $structure['metrics']['lists'], $structure['metrics']['items'], $structure['metrics']['item_words'], (int) round( $structure['metrics']['list_share'] * 100 ) ), 'context' => __( 'Measured across the whole post rather than found at one spot.', 'ai-slop-checker' ), 'position' => 0, 'in_tail' => false, 'in_quote' => false, // A measurement of the whole post, not a hit at an offset. Without // this the UI reports its position as "0% in", which is meaningless. 'whole_post' => true, ); } // Diminishing returns: 20 stylistic hits should not read the same as // two dangling "would you like me to" offers. $score = self::compress( $raw_score ); usort( $findings, static function ( $a, $b ) { return $b['weight'] <=> $a['weight']; } ); return array( 'score' => $score, 'verdict' => self::verdict( $score, $categories ), 'findings' => $findings, 'categories' => $categories, 'text_len' => $len, 'scanned_at' => time(), ); } /** * Measure how much of the page is short bullet lists rather than prose. * * Some generated content leaves no lexical trace at all — no chat residue, * no markdown, no stock phrasing — and gives itself away by shape instead: * heading, two lines of setup, a list of four-word fragments, repeated down * the page. Older models in particular answered by listing rather than * explaining. * * The thresholds are deliberately conservative, because plenty of legitimate * pages are list-heavy: spec sheets, catalogues, recipes, step-by-step docs. * What separates those is that the list *is* the content. Here the tell is * many separate short lists interleaved with thin paragraphs. * * @param string $raw Raw post content. * @return array{trips:bool, weight:float, metrics:array} */ public static function structure_signal( $raw ) { $raw = (string) $raw; $items = array(); // Real list markup first. if ( preg_match_all( '#]*>(.*?)#is', $raw, $m ) ) { $items = $m[1]; $lists = max( 1, (int) preg_match_all( '#<(ul|ol)\b#i', $raw ) ); } else { // Plain-text bullets, for content stored without list markup. $plain = self::to_plain_text( $raw ); preg_match_all( '/^[ \t]*[\*\-\x{2022}]\s+(.+)$/mu', $plain, $m2 ); $items = $m2[1] ?? array(); // Consecutive bullet lines form one list; a gap starts another. $lists = 0; $prev = false; foreach ( preg_split( '/\R/u', $plain ) as $line ) { $is_item = (bool) preg_match( '/^[ \t]*[\*\-\x{2022}]\s+\S/u', $line ); if ( $is_item && ! $prev ) { $lists++; } $prev = $is_item; } $lists = max( 1, $lists ); } $metrics = array( 'lists' => (int) $lists, 'items' => count( $items ), 'item_words' => 0.0, 'list_share' => 0.0, ); if ( count( $items ) < 12 || $lists < 4 ) { return array( 'trips' => false, 'weight' => 0.0, 'metrics' => $metrics, ); } $item_words = 0; foreach ( $items as $item ) { $item_words += count( preg_split( '/\s+/u', trim( wp_strip_all_tags( $item ) ), -1, PREG_SPLIT_NO_EMPTY ) ); } $total_words = count( preg_split( '/\s+/u', self::to_plain_text( $raw ), -1, PREG_SPLIT_NO_EMPTY ) ); $metrics['item_words'] = round( $item_words / max( 1, count( $items ) ), 1 ); $metrics['list_share'] = $total_words > 0 ? round( $item_words / $total_words, 3 ) : 0.0; // Short fragments, and enough of the page given over to them. if ( $metrics['item_words'] > 9 || $metrics['list_share'] < 0.25 ) { return array( 'trips' => false, 'weight' => 0.0, 'metrics' => $metrics, ); } $weight = 24.0; if ( $metrics['list_share'] >= 0.40 ) { $weight += 8.0; } if ( $lists >= 8 ) { $weight += 8.0; } return array( 'trips' => true, 'weight' => $weight, 'metrics' => $metrics, ); } /** * Compress a raw additive score onto a 0-100 scale. * * Saturating curve: the first serious tell moves the needle a lot, the * twentieth stylistic quibble barely moves it at all. * * @param float $raw Raw additive score. * @return int */ private static function compress( $raw ) { if ( $raw <= 0 ) { return 0; } $score = 100 * ( 1 - exp( -$raw / 55 ) ); return (int) min( 100, round( $score ) ); } /** * Human-readable verdict. * * @param int $score Compressed score. * @param array $categories Category tallies. * @return array{level:string, label:string, summary:string} */ private static function verdict( $score, $categories ) { // Quoted hits do not count towards proof — see QUOTE_MULTIPLIER. $hard = 0; foreach ( AISC_Patterns::hard_categories() as $cat ) { if ( ! isset( $categories[ $cat ] ) ) { continue; } $hard += $categories[ $cat ]['count'] - ( $categories[ $cat ]['quoted'] ?? 0 ); } $hard = max( 0, $hard ); // Proof and pattern-density are different findings and must not be // described in the same words. A post can score high purely on repeated // stylistic habits, and calling that "unambiguous tells" is a claim the // evidence does not support. if ( $hard >= 2 ) { return array( 'level' => 'high', 'label' => __( 'Pasted chat output', 'ai-slop-checker' ), 'summary' => __( 'Multiple unambiguous tells. This reads as a chat reply pasted in without editing.', 'ai-slop-checker' ), ); } if ( 1 === $hard ) { return array( 'level' => 'medium', 'label' => __( 'Likely unedited', 'ai-slop-checker' ), 'summary' => __( 'At least one clear artifact of a chat session survived into the published text.', 'ai-slop-checker' ), ); } if ( $score >= 70 ) { return array( 'level' => 'high', 'label' => __( 'Formulaic throughout', 'ai-slop-checker' ), 'summary' => __( 'No single leftover artifact, but the habits pile up across the whole piece. Taken together this reads as generated — closer to a rewrite than a spot fix.', 'ai-slop-checker' ), ); } if ( $score >= 40 ) { return array( 'level' => 'medium', 'label' => __( 'Formulaic style', 'ai-slop-checker' ), 'summary' => __( 'Generated-sounding phrasing and formatting, but nothing that proves how it was written.', 'ai-slop-checker' ), ); } if ( $score >= 15 ) { return array( 'level' => 'low', 'label' => __( 'Some AI house style', 'ai-slop-checker' ), 'summary' => __( 'Stock phrasing only. Suggestive, but not proof of anything on its own.', 'ai-slop-checker' ), ); } return array( 'level' => 'clean', 'label' => __( 'No residue found', 'ai-slop-checker' ), 'summary' => __( 'Nothing in the pattern catalogue matched.', 'ai-slop-checker' ), ); } /** * Empty report shell. * * @return array */ private static function empty_report() { return array( 'score' => 0, 'verdict' => array( 'level' => 'clean', 'label' => __( 'Nothing to scan', 'ai-slop-checker' ), 'summary' => __( 'The post has no body text.', 'ai-slop-checker' ), ), 'findings' => array(), 'categories' => array(), 'text_len' => 0, 'scanned_at' => time(), ); } /** * Character offset where the tail begins, or null if the post is too short. * * @param int $len Text length in characters. * @return int|null */ private static function tail_start_offset( $len ) { if ( $len < self::TAIL_MIN_CHARS ) { return null; } return (int) floor( $len * ( 1 - self::TAIL_FRACTION ) ); } /** * Convert a byte offset from preg_match_all into a character offset. * * @param string $text Subject text. * @param int $byte_offset Byte offset. * @return int */ private static function char_offset( $text, $byte_offset ) { if ( ! function_exists( 'mb_strlen' ) ) { return $byte_offset; } return mb_strlen( substr( $text, 0, $byte_offset ) ); } /** * Reduce post content to comparable plain text. * * Block comments are stripped, but markdown characters are deliberately * preserved — leftover asterisks and hashes are themselves evidence. * * @param string $content Raw content. * @return string */ public static function to_plain_text( $content, &$quote_ranges = null ) { $content = (string) $content; $track = ( null !== $quote_ranges ); // Drop Gutenberg block delimiters without touching their inner text. $content = preg_replace( '//s', "\n", $content ); // Mark quotation boundaries with control characters before the tags are // stripped, so the ranges survive into the plain text. \x02 and \x03 // cannot occur in real post content. if ( $track ) { $content = preg_replace( '#<(blockquote|q)\b[^>]*>#i', "\n\x02", $content ); $content = preg_replace( '##i', "\x03\n", $content ); } // Drop shortcodes and their attributes. $content = preg_replace( '/\[\/?[a-z0-9_-]+[^\]]*\]/i', ' ', $content ); // Turn block-level tags into newlines so line-anchored patterns still work. // // Cells matter as much as rows: without td/th, adjacent cells fuse into // one string and invent tokens that were never in the text. A table of // manufacturer names where "**" marks a footnote produced runs like // "Lidkoping**All TypesSeiko Seiki**", which reads as markdown bold and // is nothing of the sort. $content = preg_replace( '#]*>#i', "\n", $content ); $content = wp_strip_all_tags( $content ); $content = html_entity_decode( $content, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); // Fold typographic punctuation down to ASCII. This has to happen after // entity decoding, or ’ simply becomes a curly apostrophe again // and slips past. // // It matters more than it looks: WordPress runs wptexturize on output // and many editors store curly punctuation directly, so "I'd" routinely // arrives as "I’d" — which silently defeats every pattern containing an // apostrophe, and those are the most valuable ones in the catalogue. // Non-breaking and zero-width spaces get the same treatment, since \s // does not match them and they would break the \s+ joins between words. $content = strtr( $content, array( "\xE2\x80\x98" => "'", // left single quote "\xE2\x80\x99" => "'", // right single quote / apostrophe "\xE2\x80\x9A" => "'", // single low quote "\xE2\x80\x9B" => "'", // reversed single quote "\xE2\x80\xB2" => "'", // prime "\xE2\x80\x9C" => '"', // left double quote "\xE2\x80\x9D" => '"', // right double quote "\xE2\x80\x9E" => '"', // double low quote "\xE2\x80\x9F" => '"', // reversed double quote "\xC2\xA0" => ' ', // non-breaking space "\xE2\x80\x8B" => '', // zero-width space "\xE2\x80\x8C" => '', // zero-width non-joiner "\xEF\xBB\xBF" => '', // byte order mark ) ); // Normalise whitespace but keep line breaks. $content = preg_replace( '/[ \t]+/u', ' ', $content ); $content = preg_replace( '/\n{3,}/u', "\n\n", $content ); if ( ! $track ) { return trim( $content ); } $quote_ranges = array(); return self::extract_quote_markers( $content, $quote_ranges ); } /** * Pull the quotation markers back out, recording where they were. * * Ranges are character offsets into the returned string, matching what the * scanner compares match positions against. * * @param string $content Text still containing \x02 / \x03 markers. * @param array $ranges Receives a list of [start, end] character offsets. * @return string Clean text. */ private static function extract_quote_markers( $content, array &$ranges ) { $parts = preg_split( '/([\x02\x03])/u', $content, -1, PREG_SPLIT_DELIM_CAPTURE ); if ( ! is_array( $parts ) ) { $ranges = array(); return trim( str_replace( array( "\x02", "\x03" ), '', $content ) ); } $text = ''; $pos = 0; $depth = 0; $open_at = 0; foreach ( $parts as $part ) { if ( "\x02" === $part ) { if ( 0 === $depth ) { $open_at = $pos; } $depth++; continue; } if ( "\x03" === $part ) { $depth--; if ( $depth <= 0 ) { $depth = 0; if ( $pos > $open_at ) { $ranges[] = array( $open_at, $pos ); } } continue; } $text .= $part; $pos += function_exists( 'mb_strlen' ) ? mb_strlen( $part ) : strlen( $part ); } // An unclosed blockquote runs to the end of the content. if ( $depth > 0 && $pos > $open_at ) { $ranges[] = array( $open_at, $pos ); } // trim() would shift every offset, so account for what it removes. $ltrimmed = ltrim( $text ); $shift = ( function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text ) ) - ( function_exists( 'mb_strlen' ) ? mb_strlen( $ltrimmed ) : strlen( $ltrimmed ) ); $text = rtrim( $ltrimmed ); $end = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text ); foreach ( $ranges as $i => $range ) { $start = max( 0, $range[0] - $shift ); $stop = min( $end, $range[1] - $shift ); if ( $stop <= $start ) { unset( $ranges[ $i ] ); continue; } $ranges[ $i ] = array( $start, $stop ); } $ranges = array_values( $ranges ); return $text; } /** * Whether an offset falls inside any of the given ranges. * * @param int $offset Character offset. * @param array $ranges List of [start, end] pairs. * @return bool */ private static function in_ranges( $offset, array $ranges ) { foreach ( $ranges as $range ) { if ( $offset >= $range[0] && $offset < $range[1] ) { return true; } } return false; } /** * Grab surrounding text for a match so the editor can see it in situ. * * @param string $text Full text. * @param int $char_offset Character offset of the match. * @param string $matched The matched substring. * @return string */ private static function context( $text, $char_offset, $matched ) { $pad = 90; $start = max( 0, $char_offset - $pad ); if ( function_exists( 'mb_substr' ) ) { $match_len = mb_strlen( $matched ); $snippet = mb_substr( $text, $start, $match_len + ( $pad * 2 ) ); } else { $match_len = strlen( $matched ); $snippet = substr( $text, $start, $match_len + ( $pad * 2 ) ); } $snippet = preg_replace( '/\s+/u', ' ', trim( $snippet ) ); if ( $start > 0 ) { $snippet = '…' . $snippet; } return $snippet . '…'; } /** * Truncate a string for display. * * @param string $str Input. * @param int $max Max characters. * @return string */ private static function truncate( $str, $max ) { $str = preg_replace( '/\s+/u', ' ', trim( $str ) ); if ( function_exists( 'mb_strlen' ) && mb_strlen( $str ) > $max ) { return mb_substr( $str, 0, $max ) . '…'; } if ( strlen( $str ) > $max ) { return substr( $str, 0, $max ) . '…'; } return $str; } }