hugsbrugs / php-keywords
PHP 文本工具
Requires
- html2text/html2text: dev-master
- hugsbrugs/php-xpath: dev-master
- patrickschur/language-detection: ^5.3.0
- voku/stop-words: dev-master
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-18 09:38:16 UTC
README
测试我的免费在线 关键词密度检查器
此库提供了从 HTML 和文本中提取关键词的 PHP 函数。阅读 PHP DOC
安装
使用 composer 安装包
composer require hugsbrugs/php-keywords
在您的 PHP 代码中,加载库
require_once __DIR__ . '/vendor/autoload.php'; use Hug\Keywords\Keywords as Keywords;
用法
如果您有 HTML 作为输入,首先从 HTML 中提取文本(它还返回标题和元描述)
$text = Keywords::get_text_from_html($html);
然后调用 Keyword 类,将文本作为仅必需参数。在这种情况下,库 patrickschur/language-detection 将用于自动检测语言。
$Keywords = new Keywords($text); $kws = $Keywords->keywords;
如果您知道您的文本中使用的语言,请将参数 lang 作为第二个参数传递。它将允许从 voku/stop-words 库加载停用词列表。此库支持的语言代码包括:ar, bg, ca, cz, da, de, el, en, eo, es, et, fi, fr, hi, hr, hu, id, it, ka, lt, lv, nl, no, pl, pt, ro, ru, sk, sv, tr, uk, vi。
$Keywords = new Keywords($text, $lang); $kws = $Keywords->keywords;
如果您的语言不受 voku/stop-words 库支持,或者您想使用自己的停用词列表,则将第二个参数设置为 null,并传递您自己的停用词数组作为第三个参数。
$Keywords = new Keywords($text, 'auto', ['my custom stop word array']); $kws = $Keywords->keywords;
您可以可选地传递一个第四个参数作为要返回的关键词的最大数量。默认设置为 20。如果您想返回所有关键词,请传递 0。在所有情况下,它只返回出现次数超过 1 的关键词。
$Keywords = new Keywords($text, 'fr', [], 10); $kws = $Keywords->keywords;
您还可以传递一个可选的第五个参数数组,其中包含您想要从分析文本中删除的字符列表。默认列表是:| / & : , ; ! ? _ * - - ... → – « » + ✔ # ¿ < > [ ] { }
$Keywords = new Keywords($text, 'fr', [], 10, ['my custom chars list']); $kws = $Keywords->keywords;
可选的第六个参数表示要返回的最小关键词出现次数。默认值设置为 2,因此只有出现次数至少为 2 的关键词才会被返回。如果您想获取包括仅出现 1 次的关键词在内的所有关键词,请将此参数设置为 1。
$Keywords = new Keywords($text, 'fr', [], 10, ['my custom chars list'], 1); $kws = $Keywords->keywords;
对于网址 https://naturo-paca.fr/definition-naturopathie,库输出
[
{
"1": {
"naturopathe": [
12,
"0.61"
],
"m\u00e9decines": [
11,
"0.56"
],
"naturopathie": [
9,
"0.46"
],
"techniques": [
9,
"0.46"
],
"m\u00e9decine": [
9,
"0.46"
],
...
},
"2": {
"marie maugey": [
5,
"0.26"
],
"maugey naturopathe": [
4,
"0.20"
],
"\u2013 hippocrate": [
3,
"0.15"
],
"m\u00e9decines alternatives": [
2,
"0.10"
],
"m\u00e9decine conventionnelle": [
2,
"0.10"
],
...
},
"3": {
"marie maugey naturopathe": [
4,
"0.20"
],
"utilisation de techniques": [
3,
"0.15"
],
"associe cette technique": [
3,
"0.15"
],
"technique \u00e0 l\u2019\u00e9l\u00e9ment": [
3,
"0.15"
],
"s'adresse la naturopathie": [
2,
"0.10"
],
...
},
"4": {
"on associe cette technique": [
3,
"0.15"
],
"associe cette technique \u00e0": [
3,
"0.15"
],
"cette technique \u00e0 l\u2019\u00e9l\u00e9ment": [
3,
"0.15"
],
"qui s'adresse la naturopathie": [
2,
"0.10"
],
"la prise en charge": [
2,
"0.10"
],
...
}
}
]
单元测试
https://github.com/php-coveralls/php-coveralls
vendor/phpunit/phpunit/phpunit --configuration phpunit.xml
作者
Hugo Maugey 访问我的网站 ;)
在线工具
https://copywritely.com/keyword-density-checker/
依赖项
https://github.com/voku/stop-words https://github.com/mtibben/html2text