当前位置:首页 > PHP

PHP 实现自动摘要

2026-03-13 04:34:31PHP

PHP 实现自动摘要的方法

基于字符串截取的方法

对于简单的摘要需求,可以直接截取字符串的前N个字符。这种方法适用于内容开头部分包含关键信息的情况。

function getSummary($text, $length = 100) {
    if (strlen($text) <= $length) {
        return $text;
    }
    return substr($text, 0, $length) . '...';
}

基于句子分割的方法

通过标点符号分割文本为句子,然后选择前几个句子作为摘要。这种方法能保持句子完整性。

function getSentenceSummary($text, $sentences = 2) {
    $sentencesArray = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
    $summary = array_slice($sentencesArray, 0, $sentences);
    return implode(' ', $summary) . '...';
}

基于关键词提取的方法

使用TF-IDF算法或简单词频统计提取关键词,然后选择包含关键词的句子作为摘要。

function getKeywordSummary($text, $keywordsCount = 3) {
    $words = str_word_count(strtolower($text), 1);
    $wordFrequencies = array_count_values($words);
    arsort($wordFrequencies);
    $keywords = array_slice(array_keys($wordFrequencies), 0, $keywordsCount);

    $sentences = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
    $summarySentences = [];

    foreach ($sentences as $sentence) {
        foreach ($keywords as $keyword) {
            if (stripos($sentence, $keyword) !== false && !in_array($sentence, $summarySentences)) {
                $summarySentences[] = $sentence;
                break;
            }
        }
        if (count($summarySentences) >= 3) break;
    }

    return implode(' ', $summarySentences) . '...';
}

使用外部库

对于更复杂的摘要需求,可以使用专门的PHP库:

PHP 实现自动摘要

  1. 安装PHP-ML库(需要Composer):

    composer require php-ai/php-ml
  2. 使用文本摘要算法:

    PHP 实现自动摘要

    
    require_once 'vendor/autoload.php';

use Phpml\FeatureExtraction\TfIdfTransformer; use Phpml\Tokenization\WordTokenizer;

function getTfIdfSummary($text, $sentences = 3) { $tokenizer = new WordTokenizer(); $sentencesArray = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);

$wordsPerSentence = array_map(function($sentence) use ($tokenizer) {
    return $tokenizer->tokenize($sentence);
}, $sentencesArray);

$tfIdf = new TfIdfTransformer();
$tfIdf->fit($wordsPerSentence);
$tfIdf->transform($wordsPerSentence);

$sentenceScores = array_map('array_sum', $wordsPerSentence);
arsort($sentenceScores);
$topSentences = array_slice(array_keys($sentenceScores), 0, $sentences);
sort($topSentences);

$summary = array_map(function($index) use ($sentencesArray) {
    return $sentencesArray[$index];
}, $topSentences);

return implode(' ', $summary) . '...';

}


#### 处理HTML内容的摘要
如果需要从HTML内容中提取纯文本并生成摘要:

```php
function getHtmlSummary($html, $length = 150) {
    $plainText = strip_tags($html);
    $plainText = html_entity_decode($plainText);

    if (strlen($plainText) <= $length) {
        return $plainText;
    }

    $summary = substr($plainText, 0, $length);
    $lastSpace = strrpos($summary, ' ');
    if ($lastSpace !== false) {
        $summary = substr($summary, 0, $lastSpace);
    }

    return $summary . '...';
}

这些方法可以根据具体需求选择使用,从简单到复杂提供了不同层次的摘要实现方案。对于内容管理系统或新闻网站,基于关键词或TF-IDF的方法通常能产生更好的摘要效果。

标签: 摘要PHP
分享给朋友:

相关文章

PHP实现图文聊天

PHP实现图文聊天

PHP实现图文聊天的方法 数据库设计 创建数据库表存储聊天消息,包含字段如消息ID、发送者ID、接收者ID、消息内容、消息类型(文本/图片)、发送时间等。使用MySQL或其他数据库管理系统。 CRE…

PHP审核流程实现

PHP审核流程实现

PHP审核流程实现方法 审核流程的实现通常涉及状态管理、权限控制和操作记录。以下是几种常见的实现方式: 状态机模式 定义审核状态常量,使用状态机控制流程流转: const STATUS_PENDI…

PHP中实现重定向

PHP中实现重定向

PHP重定向的实现方法 在PHP中,重定向是通过发送HTTP头信息来实现的。以下是几种常见的重定向方法: 使用header()函数 header("Location: https://exampl…

PHP接口防重推荐实现

PHP接口防重推荐实现

防重实现方法 使用唯一请求标识符(UUID或业务ID)作为防重依据,客户端每次请求生成唯一标识,服务端校验该标识是否已处理过。适合短时间内的重复请求拦截。 // 客户端生成唯一标识 $request…

域名转短域名PHP实现

域名转短域名PHP实现

短域名服务的基本原理 短域名服务通过将长URL映射到更短的唯一字符串实现跳转。核心步骤包括生成短码、存储原始URL与短码的对应关系、通过短码重定向到原始URL。 生成短码的常见方法 Base62编码…

PHP 实现aop

PHP 实现aop

PHP 实现 AOP(面向切面编程) AOP(Aspect-Oriented Programming)是一种编程范式,用于将横切关注点(如日志、事务、权限等)从业务逻辑中分离出来。PHP 本身不原生支…