
作为开发者,我们经常需要处理各种形式的文本数据。想象一下,你负责一个电商平台,每天有成千上万的用户评论涌入;或者你维护一个新闻聚合网站,需要为每篇文章自动生成标签;再或者你需要构建一个智能搜索系统,希望用户输入简短查询就能找到最相关的文档。在这些场景下,一个核心的需求就是:如何从复杂的文本中“挖掘”出最有价值的关键词和关键短语?
手动操作显然不现实,不仅耗时耗力,而且不同的人对“关键词”的理解可能不同,导致结果不一致。传统的字符串匹配和简单的词频统计也往往力不从心,因为它们无法理解词语的上下文和语义关联。
在尝试解决这些问题时,我曾陷入过一些困境。最初,我试图通过简单的 PHP 函数(如 str_word_count、preg_match)结合自定义停用词列表来提取关键词。但很快就发现:
我需要一个更智能、更高效的解决方案,能够自动化地完成这项任务,并且具备良好的扩展性和多语言支持。
立即学习“PHP免费学习笔记(深入)”;
就在我为这些问题焦头烂额之际,我发现了 donatello-za/rake-php-plus 这个 Composer 包。它是一个基于 Rapid Automatic Keyword Extraction (RAKE) 算法的 PHP 实现。RAKE 算法是一种高效的无监督关键词提取方法,它通过分析文本中词语的共现频率和词性来识别重要的关键词和短语。
donatello-za/rake-php-plus 不仅仅是 RAKE 算法的简单移植,它还带来了许多现代 PHP 开发的优势:
它彻底解决了我在文本关键词提取方面遇到的所有痛点。
使用 donatello-za/rake-php-plus 非常简单,通过 Composer 即可快速安装:
<pre class="brush:php;toolbar:false;"># 推荐安装最新版本,支持 PHP 7.4 到 8.3 composer require donatello-za/rake-php-plus:^2.0 # 如果需要支持更老的 PHP 版本 (5.4 到 8.3) # composer require donatello-za/rake-php-plus:^1.0
安装完成后,你就可以在代码中引入并使用了:
<pre class="brush:php;toolbar:false;"><?php
require 'vendor/autoload.php';
use DonatelloZa\RakePlus\RakePlus;
// 示例文本
$text = "Criteria of compatibility of a system of linear Diophantine equations, " .
"strict inequations, and nonstrict inequations are considered. Upper bounds " .
"for components of a minimal set of solutions and algorithms of construction " .
"of minimal generating sets of solutions for all types of systems are given.";
// 1. 提取关键短语(默认英语)
$phrases = RakePlus::create($text)->get();
echo "--- 提取的关键短语 ---\n";
print_r($phrases);
/*
Array
(
[0] => criteria
[1] => compatibility
[2] => system
[3] => linear diophantine equations
[4] => strict inequations
[5] => nonstrict inequations
[6] => considered
[7] => upper bounds
[8] => components
[9] => minimal set
[10] => solutions
[11] => algorithms
[12] => construction
[13] => minimal generating sets
[14] => types
[15] => systems
)
*/
// 2. 提取独立关键词
$keywords = RakePlus::create($text)->keywords();
echo "\n--- 提取的独立关键词 ---\n";
print_r($keywords);
/*
Array
(
[0] => criteria
[1] => compatibility
[2] => system
[3] => linear
[4] => diophantine
[5] => equations
...
)
*/
// 3. 按分数降序排列短语,并获取分数
$rake = RakePlus::create($text, 'en_US'); // 明确指定语言
$phrase_scores = $rake->sortByScore('desc')->scores();
echo "\n--- 按分数排序的关键短语及分数 ---\n";
print_r($phrase_scores);
/*
Array
(
[linear diophantine equations] => 9
[minimal generating sets] => 8.5
[minimal set] => 4.5
[strict inequations] => 4
[nonstrict inequations] => 4
[upper bounds] => 4
[criteria] => 1
[compatibility] => 1
[system] => 1
[considered] => 1
[components] => 1
[solutions] => 1
[algorithms] => 1
[construction] => 1
[types] => 1
[systems] => 1
)
*/
// 4. 处理新文本,复用 RakePlus 实例更高效
$newText = "A fast Fourier transform (FFT) algorithm computes...";
$newPhrases = $rake->extract($newText)->sort()->get();
echo "\n--- 处理新文本后的关键短语 ---\n";
print_r($newPhrases);
/*
Array
(
[0] => algorithm computes
[1] => fast fourier transform
[2] => fft
)
*/
// 5. 自定义停用词或指定语言
// 你可以直接传递一个停用词数组
$customStopwords = ['a', 'an', 'the', 'of', 'for'];
$rakeWithCustomStopwords = RakePlus::create("This is a test sentence for custom stopwords.", $customStopwords);
print_r($rakeWithCustomStopwords->get());
// 或者指定支持的语言代码,例如中文(如果它被官方支持或你添加了停用词文件)
// $chineseText = "这是一个关于人工智能的中文文本。";
// $rakeChinese = RakePlus::create($chineseText, 'zh_CN'); // 假设 zh_CN 已被支持或配置
// print_r($rakeChinese->get());donatello-za/rake-php-plus 支持通过语言代码(如 en_US, fr_FR, de_DE, zh_CN 等,具体支持列表请查阅其文档)自动加载相应的停用词文件,极大地简化了多语言内容的关键词提取工作。如果你需要支持的语言不在其内置列表中,它还提供了工具来从 JSON 或文本文件中生成自定义的停用词文件,并集成到库中。
donatello-za/rake-php-plus 带来的不仅仅是代码层面的便利,更重要的是它在实际应用中产生的巨大价值:
donatello-za/rake-php-plus 是一个功能强大、易于使用且性能优异的 PHP 关键词和关键短语提取库。它将复杂的 RAKE 算法封装成简洁的 API,让开发者能够轻松应对多语言、大规模文本处理的挑战。如果你正被海量文本中的信息过载问题所困扰,或者希望为你的应用增添智能化的文本分析能力,那么 donatello-za/rake-php-plus 绝对值得你尝试。它将帮助你从文本的“泥潭”中解脱出来,让你的应用更加智能、高效。
以上就是如何从海量文本中快速提炼核心信息?donatello-za/rake-php-plus助你实现智能关键词提取的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号