codeinc/query-tokens-extractor

1.0.0 2023-08-21 17:23 UTC

This package is auto-updated.

Last update: 2024-09-21 19:25:56 UTC


README

使用正则表达式定义的令牌从查询中提取令牌。该库是用 PHP 8.2 编写的。

安装

该软件包可在 Packagist 上获得,并可以使用 Composer 安装 [此处查看]

composer req codeinc/query-token-extractor

使用方法

use CodeInc\QueryTokenExtractor\QueryTokenExtractor;
use CodeInc\QueryTokensExtractor\Type\RegexType;
use CodeInc\QueryTokensExtractor\Type\WordType;
use CodeInc\QueryTokensExtractor\Type\FrenchPhoneNumberType;
use CodeInc\QueryTokensExtractor\Type\FrenchPostalCodeType;
use CodeInc\QueryTokensExtractor\Type\YearType;
use CodeInc\QueryTokensExtractor\Dto\QueryToken;

$tokensExtractor = new QueryTokensExtractor([
    new FrenchPhoneNumberType(),
    new FrenchPostalCodeType(),
    new YearType(),
    new RegexType('my_custom_token', '/^this a custom token/ui'),
    new WordType(),
]);

$tokens = $tokensExtractor->extract('paris (75001) these are words 01.00.00.00.00 this a custom token 2023');

/** @var QueryToken $token */
foreach ($tokens as $token) {
    echo "Position: " . $token->position . "\n"
        ."Class: " . get_class($token->type) . "\n"
        ."Name: " . $token->type->name . "\n"
        ."Value: " . $token->value . "\n";
}

上面的示例将生成以下输出

Position: 0
Class: CodeInc\QueryTokensExtractor\Type\WordType
Name: word
Value: paris

Position: 1
Class: CodeInc\QueryTokensExtractor\Type\FrenchPostalCodeType
Name: french_postal_code
Value: 75001

Position: 2
Class: CodeInc\QueryTokensExtractor\Type\WordType
Name: word
Value: these

Position: 3
Class: CodeInc\QueryTokensExtractor\Type\WordType
Name: word
Value: are

Position: 4
Class: CodeInc\QueryTokensExtractor\Type\WordType
Name: word
Value: words

Position: 5
Class: CodeInc\QueryTokensExtractor\Type\FrenchPhoneNumberType
Name: french_phone_number
Value: 01 00 00 00 00 (the original value without punctuation)

Position: 6
Class: CodeInc\QueryTokensExtractor\Type\CustomTokenType
Name: my_custom_token
Value: this a custom token

Position: 7
Class: CodeInc\QueryTokensExtractor\Type\YearType
Name: year
Value: 2023

令牌类型

可用的令牌类型

  • WordType:从查询中提取单词
  • YearType:从查询中提取年份
  • FrenchPhoneNumberType:从查询中提取法国电话号码
  • FrenchPostalCodeType:从查询中提取法国邮政编码
  • HashtagType:从查询中提取标签
  • RegexTokenType:使用正则表达式从查询中提取令牌

令牌类型优先级

令牌类型优先级由传递给 QueryTokensExtractor 构造函数的令牌类型顺序确定。

优先级用于确定提取令牌的顺序。优先级越高,令牌提取越早。

⚠️ WordType 应始终使用最后,因为它将匹配任何字符串。

use CodeInc\QueryTokenExtractor\QueryTokenExtractor;
use CodeInc\QueryTokensExtractor\Type\WordType;
use CodeInc\QueryTokensExtractor\Type\FrenchPhoneNumberType;
use CodeInc\QueryTokensExtractor\Type\FrenchPostalCodeType;
use CodeInc\QueryTokensExtractor\Type\YearType;

$tokensExtractor = new QueryTokensExtractor([
    new FrenchPhoneNumberType(), // highest priority
    new FrenchPostalCodeType(),
    new YearType(),
    new WordType(), // lowest priority
]);

创建自定义令牌类型

可以通过实例化或扩展 RegexTokenType 来创建自定义令牌类型。 RegexTokenType 的构造函数接受四个参数

  • string $name:令牌类型的名称
  • string $regex:用于提取令牌的正则表达式
  • \Closure $valueFormatter:用于格式化提取值的闭包(可选)

正则表达式中的 value 捕获组用作提取值(例如,HashtagType 类型使用正则表达式 '/^#(?<value>.[a-z0-9_]+)/ui')。如果没有定义名为 value 的组,则整个匹配将用作令牌值。

正则表达式应始终以 ^ 开头,并且不要使用 $ 限制字符串的末尾,因为查询是使用 preg_replace_callback() 函数分割成令牌的。

use CodeInc\QueryTokensExtractor\Type\RegexType;

class MyCustomTokenType extends RegexType
{
    public function __construct()
    {
        parent::__construct(
            name: 'my_custom_token',
            regexp: '/^this a custom token/ui'
        );
    }
}

// alternatively tokens can be defined directly using the RegexType class
$myCustomToken2 = new RegexType(
    name: 'my_custom_token',
    regexp: '/^this a custom token/ui'
);

令牌值格式化

可以使用 valueFormatter 闭包来格式化提取的令牌值。闭包接受提取的值作为参数,并必须返回格式化的值。

use CodeInc\QueryTokensExtractor\Type\RegexType;

$tokensExtractor = new QueryTokensExtractor([
    new RegexType(
        name: 'my_custom_token',
        regexp: '/^this a custom token/ui',
        // a simple closure called by QueryToken::getFormattedValue()
        valueFormatter: fn($value) => strtoupper($value)
    )
]);
$tokens = $tokensExtractor->extract('this a custom token');
$tokens->getByPosition(0)->getFormattedValue(); // THIS A CUSTOM TOKEN

许可证

该库在 MIT 许可证下发布(请参阅 LICENSE 文件)。