donatello-za/rake-php-plus

又是另一个Rapid Automatic Keyword Extraction算法(RAKE)的PHP实现。

v1.0.19 2024-06-26 12:29 UTC

README

基于Rapid Automatic Keyword Extraction算法(RAKE)的关键词和短语提取库。

Latest Stable Version Total Downloads License

简介

关键词描述文档/文本中表达的主要主题。关键词提取则允许从文本中提取重要的单词和短语。

提取的关键词可用于以下方面

  • 从更大的文本中构建有用的标签列表
  • 构建搜索索引和搜索引擎
  • 按主题分组相似内容。

提取的短语可用于以下方面

  • 突出显示更大文本中的重要区域
  • 语言或文档分析
  • 基于上下文术语构建智能搜索

此库为PHP开发者提供了一个简单的方法,可以从文本字符串中获取关键词和短语列表,并基于Richard Filipčík的名为RAKE-PHP的较小且未维护的项目,该项目是将一个名为RAKE的Python实现翻译成PHP。

如Rose, S., Engel, D., Cramer, N., & Cowley, W. (2010)在Automatic Keyword Extraction from Individual Documents中所述。在M. W. Berry & J. Kogan (Eds.), Text Mining: Theory and Applications: John Wiley & Sons。

这个特定的软件包旨在包含以下优点,优于原始的RAKE-PHP软件包

  1. PSR-2编码标准。
  2. PSR-4以便通过Composer安装。
  3. 其他功能,如方法链。
  4. 提供源停用词的多种方式。
  5. 完整的单元测试覆盖。
  6. 性能改进。
  7. 改进的文档。
  8. 易于语言集成和多字节字符串支持。

当前支持的语言

  • 南非荷兰语(af_ZA)
  • 阿拉伯语(阿拉伯联合酋长国)/阿联酋(ar_AE)
  • 巴西葡萄牙语/葡萄牙语(pt_BR)
  • 美国英语(en_US)
  • 欧洲葡萄牙语/葡萄牙语(pt_PT)
  • 法语/法语(fr_FR)
  • 德语(德国)/德语(Deutschland)(de_DE)
  • 意大利语/意大利语(it_IT)
  • 波兰语/波兰语(pl_PL)
  • 俄语/俄语(ru_RU)
  • 索拉尼库尔德语/索拉尼(ckb_IQ)
  • 西班牙语/西班牙语(es_AR)
  • 泰米尔语/泰米尔语(ta_TA)
  • 土耳其语/土耳其语(tr_TR)
  • 波斯语/波斯语(fa_IR)
  • 荷兰语/荷兰语(nl_NL)
  • 瑞典语/瑞典语(sv_SE)

如果您的语言未在此列出,可以添加,请参阅页面底部的“如何添加额外语言”部分。

版本

v1.0.19

特别感谢

安装

使用 Composer

$ composer require donatello-za/rake-php-plus
{
    "require": {
        "donatello-za/rake-php-plus": "^1.0"
    }
}
<?php
require 'vendor/autoload.php';

use DonatelloZa\RakePlus\RakePlus;

不使用 Composer

<?php

require 'path/to/AbstractStopwordProvider.php';
require 'path/to/ILangParseOptions.php';
require 'path/to/LangParseOptions.php';
require 'path/to/StopwordArray.php';
require 'path/to/StopwordsPatternFile.php';
require 'path/to/StopwordsPHP.php';
require 'path/to/RakePlus.php';

use DonatelloZa\RakePlus\RakePlus;

示例 1

创建 RakePlus 的新实例,提取短语并返回结果。假设指定的文本为美国英语。

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.";

$phrases = RakePlus::create($text)->get();

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

创建 RakePlus 的新实例,以不同的顺序提取短语,并展示如何获取短语分数。

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.";

// Note: en_US is the default language.
$rake = RakePlus::create($text, 'en_US');

// 'asc' is optional and is the default sort order
$phrases = $rake->sort('asc')->get();
print_r($phrases);
Array
(
    [0] => algorithms
    [1] => compatibility
    [2] => components
    [3] => considered
    [4] => construction
    [5] => criteria
    [6] => linear diophantine equations
    [7] => minimal generating sets
    [8] => minimal set
    [9] => nonstrict inequations
    [10] => solutions
    [11] => strict inequations
    [12] => system
    [13] => systems
    [14] => types
    [15] => upper bounds
)
// Sort in descending order
$phrases = $rake->sort('desc')->get();
print_r($phrases);
Array
(
    [0] => upper bounds
    [1] => types
    [2] => systems
    [3] => system
    [4] => strict inequations
    [5] => solutions
    [6] => nonstrict inequations
    [7] => minimal set
    [8] => minimal generating sets
    [9] => linear diophantine equations
    [10] => criteria
    [11] => construction
    [12] => considered
    [13] => components
    [14] => compatibility
    [15] => algorithms
)
// Sort the phrases by score and return the scores
$phrase_scores = $rake->sortByScore('desc')->scores();
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
)
// Extract phrases from a new string on the same RakePlus instance. Using the
// same RakePlus instance is faster than creating a new instance as the
// language files do not have to be re-loaded and parsed.

$text = "A fast Fourier transform (FFT) algorithm computes...";
$phrases = $rake->extract($text)->sort()->get();
print_r($phrases);
Array
(
    [0] => algorithm computes
    [1] => fast fourier transform
    [2] => fft
)

示例 3

创建 RakePlus 的新实例并从短语中提取唯一关键词。

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.";

$keywords = RakePlus::create($text)->keywords();
print_r($keywords);
Array
(
    [0] => criteria
    [1] => compatibility
    [2] => system
    [3] => linear
    [4] => diophantine
    [5] => equations
    [6] => strict
    [7] => inequations
    [8] => nonstrict
    [9] => considered
    [10] => upper
    [11] => bounds
    [12] => components
    [13] => minimal
    [14] => set
    [15] => solutions
    [16] => algorithms
    [17] => construction
    [18] => generating
    [19] => sets
    [20] => types
    [21] => systems
)

示例 4

不使用静态 RakePlus::create 方法创建 RakePlus 的新实例。

use DonatelloZa\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.";

$rake = new RakePlus();
$phrases = $rake->extract()->get();

// Alternative method:
$phrases = (new RakePlus($text))->get();

示例 5

您可以通过四种不同的方式提供自定义停用词

use DonatelloZa\RakePlus\RakePlus;

// 1: The standard way (provide a language code)
//    RakePlus will first look for ./lang/en_US.pattern, if
//    not found, it will look for ./lang/en_US.php.
$rake = RakePlus::create($text, 'en_US');

// 2: Pass an array containing stopwords
$rake = RakePlus::create($text, ['a', 'able', 'about', 'above', ...]);

// 3: Pass the name of a PHP or pattern file,
//    see lang/en_US.php and lang/en_US.pattern for examples.
$rake = RakePlus::create($text, '/path/to/my/stopwords.pattern');

// 4: Create an instance of one of the stopword provider classes (or
//    create your own) and pass that to RakePlus:
$stopwords = StopwordArray::create(['a', 'able', 'about', 'above', ...]);
$rake = RakePlus::create($text, $stopwords);

示例 6

您可以指定短语/关键词必须具有的最小字符数,如果少于最小值,则会被过滤掉。默认值为 0(没有最小值)。

use DonatelloZa\RakePlus\RakePlus;

$text = '6462 Little Crest Suite, 413 Lake Carlietown, WA 12643';

// Without a minimum
$phrases = RakePlus::create($text, 'en_US', 0)->get();
print_r($phrases);
Array
(
    [0] => crest suite
    [1] => 413 lake carlietown
    [2] => wa 12643
)
// With a minimum
$phrases = RakePlus::create($text, 'en_US', 10)->get();

print_r($phrases);
Array
(
    [0] => crest suite
    [1] => 413 lake carlietown
)

示例 7

您可以指定是否过滤出只由数字组成的短语/关键词。默认值为过滤数字。

use DonatelloZa\RakePlus\RakePlus;

$text = '6462 Little Crest Suite, 413 Lake Carlietown, WA 12643';

// Filter out numerics
$phrases = RakePlus::create($text, 'en_US', 0, true)->get();
print_r($phrases);
(
    [0] => crest suite
    [1] => 413 lake carlietown
    [2] => wa 12643
)
// Do not filter out numerics
$phrases = RakePlus::create($text, 'en_US', 0, false)->get();

print_r($phrases);
Array
(
    [0] => 6462
    [1] => crest suite
    [2] => 413 lake carlietown
    [3] => wa 12643
)

如何添加额外的语言

库需要为每种语言提供一个“停用词”列表。停用词是该语言中常用的词,例如“和”、“是”、“或”等。

50 种语言的停用词(包括已支持的语言)以 JSON 格式提供。如果您幸运地有您语言在列表中,则可以轻松将其导入库中。要这样做,请阅读下面的部分

使用停用词提取工具

注意:以下说明假设您正在使用 Linux

我们将以希腊语为例

  1. 检查您的操作系统是否具有希腊本地化文件,您必须查找的希腊区域代码是:el_GR。因此,运行命令 $ locale -a 以查看是否已列出。
  2. 如果未列出,则需要创建它,因此请运行
sudo locale-gen el_GR
sudo locale-gen el_GR.utf8
  1. 转到 停用词文件列表,找到希腊语,文件将命名为 el.json,其中包含 75 个停用词。
  2. 下载 el.json 文件并将其存储在系统上的某个位置。
  3. 在终端中,转到 rake-php-plus 库的目录,如果使用 Composer 安装,则位于 vendor/donatello-za/rake-php-plus

现在我们需要使用 JSON 文件创建两个新文件,一个将是包含停用词的 PHP 数组的 .php 文件,另一个将是包含停用词的正则表达式的 .pattern 文件。

  1. 通过运行提取并转换 .json 文件到 PHP 文件
$ php ./console/extractor.php path/to/el.json --locale=el_GR --output=php > ./some/dir/el_GR.php
  1. 通过运行提取并转换 .json 文件到 .pattern 文件
$ php ./console/extractor.php path/to/el.json --locale=el_GR --output=pattern > ./some/dir/el_GR.pattern

就是这样!您现在可以通过在创建 RakePlus 类的实例时指定它来使用新的停用词,例如

$rake = RakePlus::create($text, '/some/dir/el_GR.pattern');

$rake = RakePlus::create($text, '/some/dir/el_GR.php');

通过添加语言来贡献

如果您希望您的语言获得官方支持,您可以分叉这个库,按照上述说明生成 .pattern.php 停用词文件,将其放置在 ./rake-php-plus/lang/ 目录中,并提交为 pull request。

一旦您的语言获得官方支持,您就可以指定语言而无需指定要使用的文件,例如

$rake = RakePlus::create($text, 'el_GR');

RakePHP 将首先查找 .pattern 文件,如果没有找到,它将在 ./lang/ 目录中查找 .php 文件。

我没有我语言的停用词文件,怎么办?

如果您的语言不包含在此处列出的 50 种语言中 列表,您可能需要尝试在其他地方寻找,尝试搜索 "yourlanguage stopwords"。如果您找到列表或决定创建自己的列表,您也可以将其放在标准文本文件中,而不是 .json 文件,并使用提取工具提取停用词,例如

$ php ./console/extractor.php path/to/mystopwords.txt --locale=LOCAL_CODE --output=php > ./some/dir/LOCAL_CODE.php
$ php ./console/extractor.php path/to/mystopwords.txt --locale=LOCAL_CODE --output=php > ./some/dir/LOCAL_CODE.php

请记住用正确的本地代码替换 LOCAL_CODE

以下是一个示例文本文件,其中包含从网站复制的停用词:stopwords_en_US

要运行测试

使用 PHPUnit v11.2 进行单元测试,运行在 PHP v8.3.0+ 上。

./vendor/bin/phpunit tests

许可证

在 MIT 许可证下发布(阅读 LICENSE)。