🗣 Wd提问: 关键词自动采集php
🤖 Ai回答: 在开发网页应用时,关键词自动采集(Keyword Extraction/Automatic Keyword Collection)通常用于从文本中提取重要词汇或短语。使用 PHP 实现关键词自动采集,可以结合自然语言处理技术、正则表达式、第三方库或 API 来完成。
以下是一个简单的 PHP 关键词自动采集实现方案,适用于中文和英文文本:
✅ 方法一:基于 TF-IDF 算法的关键词提取(适合中文/英文)
使用 `PHP` + `scws`(简易中文分词系统)进行中文关键词提取。
步骤 1:安装 scws 扩展(仅支持 Linux)
bash
下载并编译 scws
wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
tar -xjf scws-1.2.3.tar.bz2
cd scws-1.2.3
./configure && make && make install
安装 PHP 扩展
pecl install scws
然后在 `php.ini` 中添加:
ini
extension=scws.so
步骤 2:PHP 代码示例(中文关键词提取)
php
<?php
function extractKeywords($text, $topK = 10) {
// 创建 scws 实例
$scws = scws_new();
$scws-set_charset('utf8');
$scws-add_dict('/usr/local/etc/dict.utf8.xdb'); // 词典路径
$scws-set_rule('/usr/local/etc/rules.utf8.ini');
$scws-set_ignore(true); // 忽略标点
$scws-send_text($text);
$words = [];
while ($res = $scws-get_result()) {
while ($res) {
$word = substr($text, $res['off'], $res['len']);
if (!isset($words[$word])) {
$words[$word] = 0;
}
$words[$word] += 1;
$res = $res['next'];
}
}
$scws-close();
// 按频率排序,取前 topK
arsort($words);
return array_slice(array_keys($words), 0, $topK);
}
// 示例文本
$text = 人工智能是未来的趋势,机器学习和深度学习推动了AI的发展。;
$keywords = extractKeywords($text, 5);
print_r($keywords);
?
✅ 方法二:简单英文关键词提取(无需扩展)
php
<?php
function extractEnglishKeywords($text, $topK = 10) {
// 转为小写,去除标点
$text = strtolower(preg_replace('/[^a-z\s]/', '', $text));
// 分割成单词
$words = array_filter(explode(' ', $text));
// 常见停用词(stop words)
$stopWords = ['the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were'];
// 过滤停用词
$filteredWords = array_filter($words, function($word) use ($stopWords) {
return strlen($word) 2 && !in_array($word, $stopWords);
});
// 统计频率
$wordCount = array_count_values($filteredWords);
// 排序取前 N 个
arsort($wordCount);
return array_keys(array_slice($wordCount, 0, $topK));
}
// 示例
$text = Artificial intelligence and machine learning are the future of technology and innovation.;
$keywords = extractEnglishKeywords($text, 5);
print_r($keywords);
?
✅ 方法三:调用第三方 API(推荐用于生产环境)
例如使用百度 AI、阿里云 NLP、腾讯云等关键词提取 API。
示例:调用百度 AI 文本分析 API(需申请 AK/SK)
php
<?php
function baiduExtractKeywords($title, $content, $apiKey, $secretKey) {
// 获取 access token
$tokenUrl = https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$apiKey}&client_secret={$secretKey};
$tokenResp = json_decode(file_get_contents($tokenUrl), true);
$token = $tokenResp['access_token'];
// 调用关键词提取接口
$url = https://aip.baidubce.com/rpc/2.0/nlp/v1/keyword?access_token= 、$token;
$data = [
'title' = $title,
'content' = $content,
'max_keywords' = 5
];
$options = [
'http' = [
'header' = Content-Type: application/json\r\n,
'method' = 'POST',
'content' = json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$json = json_decode($result, true);
if (isset($json['items'])) {
return array_column($json['items'], 'keyword');
}
return [];
}
// 使用示例(替换为你的密钥)
// $keywords = baiduExtractKeywords(AI 技术, 人工智能正在改变世界..., your_api_key, your_secret_key);
// print_r($keywords);
?
📌 总结
| 方法 | 适用场景 | 是否需要扩展 | 准确度 |
-
| scws 分词 | 中文文本关键词提取 | 需要安装 scws | 高 |
| 纯 PHP 统计 | 英文简单提取 | 无依赖 | 中 |
| 第三方 API | 生产环境、高精度 | 需网络请求 | 非常高 |
🔐 注意事项
不要用于非法爬虫或侵犯版权的内容采集。
自动采集应遵守网站 `robots.txt` 和相关法律法规。
对于大规模采集,建议使用队列、缓存和限流机制。
如你有具体需求(如从网页中提取关键词、批量采集文章关键词等),欢迎补充场景,我可以提供更完整的采集+分析脚本。
📣 商家广告 :
👍
0
👎
IP地址: 79.5.47.221
搜索次数: 0
提问时间: 2025-11-10 20:40:39
🛒 域名购买 :
32.cx ftp.gs yrnz.cn pai.kim che.gs sbnjt.net zd.gs 723.net.cn tui.men 2223.com.cn
❓️ 热门提问 :
手机如何注册网址 卖服务器图片 便宜的网站开发服务 海外节点云服务器哪个好 淮安SEO推广靠谱 香港进口鞋子 网站建设JavaScript vps搭建矿池 国外vps免费 河南 云主机 豌豆Ai站群搜索引擎系统
🌐 域名评估 :
allone.ac.cn fxtp.cn 358.net.cn vance.org.cn deepseek.pet w-f.com.cn dgsq.cn aaa.yn.cn hlk.cn kx.gs
⛏ 最新挖掘 :
凯里酸汤鱼 贵阳烤脑花 都匀毛尖茶 花溪牛肉粉 贵阳烤生蚝 贵阳烤豆腐 贵阳纸包鱼 贵阳特色菜
🖌 热门作画 :
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊 :
🔗 友情链接 :
网站搜索引擎入口
网站搜索
ai提问
🧰 站长工具 :Ai工具 whois查询 搜索
📢 温馨提示 :本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持 :本站由豌豆Ai 提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。