]*>.*?#is', "\n", $content );
$content = preg_replace( '#]*>.*?#is', "\n", $content );
$content = preg_replace( '#<(script|style)[^>]*>.*?\1\s*>#is', "\n", $content );
return $content;
}
/**
* Whether a sentence is structurally capable of being true or false.
*
* This runs before scoring because the failure it prevents is expensive:
* every heading, list intro and rhetorical question that slips through is a
* paid API call whose only possible answer is "unverifiable".
*
* @param string $sentence Candidate sentence.
* @return bool
*/
private static function is_checkable_sentence( $sentence ) {
// A question asks; it does not assert.
if ( preg_match( '/\?\s*$/u', $sentence ) ) {
return false;
}
// A trailing colon introduces a list — the claim, if any, is in the list.
if ( preg_match( '/:\s*$/u', $sentence ) ) {
return false;
}
// Statements finish. Headings and bullet fragments do not.
if ( ! preg_match( '/[.!][""\')\]]?\s*$/u', $sentence ) ) {
return false;
}
// Must open like prose: not an emoji, bullet, arrow or bare number.
if ( ! preg_match( '/^["\'(\[]?\p{L}/u', $sentence ) ) {
return false;
}
// Needs enough words to carry a proposition.
$words = preg_split( '/\s+/u', trim( $sentence ), -1, PREG_SPLIT_NO_EMPTY );
if ( count( $words ) < self::MIN_WORDS ) {
return false;
}
// A sentence that is almost entirely a quotation with a stub attached
// asserts nothing on its own. Real example, sent and rejected as an
// "incomplete sentence": '"Certified to ISO 9001:2015, serving ...
// manufacturers in 14 states" is.'
$outside = preg_replace( '/"[^"]*"/u', ' ', $sentence );
$outside = preg_split( '/\s+/u', trim( $outside ), -1, PREG_SPLIT_NO_EMPTY );
if ( count( $outside ) < 4 ) {
return false;
}
return true;
}
/**
* Extract ranked candidate claims.
*
* @param string $raw_content Post content.
* @param int $limit Maximum claims to return.
* @return array
*/
public static function extract( $raw_content, $limit = 12 ) {
$text = AISC_Scanner::to_plain_text( self::strip_non_prose( $raw_content ) );
$len = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );
$sents = self::split_sentences( $text );
$candidates = array();
$offset = 0;
foreach ( $sents as $sentence ) {
$sentence = trim( $sentence );
$s_len = function_exists( 'mb_strlen' ) ? mb_strlen( $sentence ) : strlen( $sentence );
$position = $len > 0 ? (int) round( ( $offset / $len ) * 100 ) : 0;
$offset += $s_len + 1;
if ( $s_len < self::MIN_CHARS || $s_len > self::MAX_CHARS ) {
continue;
}
// Structure first: no amount of factual-looking vocabulary makes a
// heading or a question checkable, and each one sent is paid for.
if ( ! self::is_checkable_sentence( $sentence ) ) {
continue;
}
$scored = self::score_sentence( $sentence );
if ( $scored['score'] < self::MIN_SCORE ) {
continue;
}
$candidates[] = array(
'text' => $sentence,
'score' => $scored['score'],
'signals' => $scored['signals'],
'position' => $position,
);
}
usort(
$candidates,
static function ( $a, $b ) {
return $b['score'] <=> $a['score'];
}
);
return array_slice( $candidates, 0, max( 1, (int) $limit ) );
}
/**
* Score a sentence on how verifiable it is.
*
* @param string $sentence Sentence text.
* @return array{score:int, signals:array}
*/
private static function score_sentence( $sentence ) {
$score = 0;
$signals = array();
$tests = array(
// Time-anchored statements are the ones that rot.
array(
'regex' => '/\b(19|20)\d{2}\b/u',
'points' => 25,
'signal' => 'year',
),
array(
'regex' => '/\b(as\s+of|since|until|through)\s+(early\s+|mid[\s-]|late\s+)?((19|20)\d{2}|january|february|march|april|may|june|july|august|september|october|november|december)/iu',
'points' => 20,
'signal' => 'date-anchored',
),
array(
'regex' => '/\b(currently|today|at\s+present|right\s+now|these\s+days|nowadays)\b/iu',
'points' => 18,
'signal' => 'present-tense claim',
),
// Superlatives and rankings invite a correction.
array(
'regex' => '/\b(latest|newest|most\s+recent|current|largest|biggest|smallest|fastest|first|only|leading|top|best[\s-]selling|world\'s)\b/iu',
'points' => 18,
'signal' => 'superlative',
),
// Quantities.
array(
'regex' => '/\b\d+(\.\d+)?\s*(%|percent|million|billion|trillion|thousand|k\b|m\b|bn\b)/iu',
'points' => 22,
'signal' => 'statistic',
),
array(
'regex' => '/(\$|£|€|¥)\s?\d/u',
'points' => 20,
'signal' => 'price',
),
// Version numbers age fast.
array(
'regex' => '/\bv?\d+\.\d+(\.\d+)?\b/u',
'points' => 16,
'signal' => 'version number',
),
// Attribution / definitional statements.
array(
'regex' => '/\b(according\s+to|reported\s+by|study\s+(found|shows)|research\s+(shows|found)|data\s+from|survey)\b/iu',
'points' => 18,
'signal' => 'sourced claim',
),
array(
'regex' => '/\b(is|was|are|were|has|have|became|acquired|launched|released|announced|founded|merged|discontinued|renamed)\b/iu',
'points' => 6,
'signal' => 'assertion',
),
// Causal and comparative statements are checkable even with no
// numbers, dates or names in them. Gemini verified several of these
// against real sources while the scorer was rating them near zero.
array(
'regex' => '/\b(because|since|due\s+to|as\s+a\s+result|therefore|which\s+means|driven\s+by|caused\s+by)\b/iu',
'points' => 10,
'signal' => 'causal claim',
),
array(
'regex' => '/\b(more|less|higher|lower|cheaper|faster|slower|greater|fewer|larger|smaller|bigger)\s+[\w-]+\s+(than|at|when|as|for)\b|\b(increase|decrease|rise|fall|grow|drop|scale)\w*\s+(with|as|when)\b|\bcost[\s-]effective\b/iu',
'points' => 12,
'signal' => 'comparative claim',
),
// Proper nouns mid-sentence suggest named entities.
array(
'regex' => '/(?<=[a-z]\s)[A-Z][a-zA-Z]{2,}(\s[A-Z][a-zA-Z]{2,})?/u',
'points' => 10,
'signal' => 'named entity',
),
);
foreach ( $tests as $test ) {
if ( preg_match( $test['regex'], $sentence ) ) {
$score += $test['points'];
$signals[] = $test['signal'];
}
}
// Hedged or subjective sentences have nothing checkable in them.
$soft = array(
'/\b(i\s+think|in\s+my\s+opinion|arguably|perhaps|maybe|might\s+be|could\s+be|some\s+say|many\s+believe)\b/iu',
'/\b(should|ought\s+to|consider|try|remember\s+to|make\s+sure)\b/iu',
'/\?\s*$/u',
// Ranking language with no stated metric. A fact-checker can only
// answer "depends what you mean by value", which costs a call to
// learn nothing.
'/\b(one|some|any)\s+of\s+the\s+(most|best|biggest|highest|largest|top|worst|greatest)\b/iu',
'/\b(high|low|best|worst|greatest)[\s-]?(est)?[\s-]value\b/iu',
// Advice framed at the reader rather than a claim about the world.
'/\b(you|your)\b.*\b(can|will|need|want)\b/iu',
);
foreach ( $soft as $regex ) {
if ( preg_match( $regex, $sentence ) ) {
$score -= 20;
}
}
return array(
'score' => max( 0, $score ),
'signals' => array_values( array_unique( $signals ) ),
);
}
/**
* Split text into sentences.
*
* Deliberately conservative: it avoids breaking on common abbreviations and
* decimals, which would otherwise shred statistics into fragments.
*
* @param string $text Plain text.
* @return array
*/
private static function split_sentences( $text ) {
// Protect decimals and common abbreviations from the splitter.
$guard = array(
'/(\d)\.(\d)/u' => '$1$2',
'/\b(Mr|Mrs|Ms|Dr|Prof|Sr|Jr|St|vs|etc|Inc|Ltd|Co|Corp|approx|e\.g|i\.e|U\.S|U\.K)\./iu' => '$1',
);
$text = preg_replace( array_keys( $guard ), array_values( $guard ), $text );
$parts = preg_split( '/(?<=[.!?])\s+|\n+/u', $text, -1, PREG_SPLIT_NO_EMPTY );
if ( ! is_array( $parts ) ) {
return array();
}
return array_map(
static function ( $part ) {
return str_replace( '', '.', $part );
},
$parts
);
}
}