nullform/fuzzio 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nullform/fuzzio

Composer 安装命令:

composer require nullform/fuzzio

包简介

Fuzzy search using similar_text() and levenshtein() functions. Easy to use and safe for multibyte encodings (UTF-8).

README 文档

README

The PHP package for calculate similarity and Levenshtein distance between strings. Realizes fuzzy search using similar_text() and levenshtein() functions. Easy to use and safe for multibyte encodings (UTF-8).

Requirements

  • PHP >= 5.6

Installation

composer require nullform/fuzzio

Usage examples

use \Nullform\Fuzzio\Fuzzio;
use \Nullform\Fuzzio\FuzzioString;

$needle = 'john'; // Reference string
$haystack = ['jon', 'johns', 'jane', 'janie']; // Array of strings

$fuzzio = new Fuzzio($needle, $haystack);

// Get all strings from $haystack with calculated similarity and Levenshtein distance
$all = $fuzzio->get(); // FuzzioString[]

// Get strings with similarity >= 80% and Levenshtein distance <= 1
$filtered = $fuzzio->get(80, 1); // FuzzioString[]

// Get strings with Levenshtein distance <= 1
$filtered = $fuzzio->get(null, 1); // FuzzioString[]

// With max similarity value
$allClosest = $fuzzio->getClosest(); // FuzzioString[]

// One (first) with max similarity value
$closestOne = $fuzzio->getClosestOne(); // FuzzioString

echo $closestOne; // johns
echo $closestOne->getString(); // johns
echo $closestOne->getSimilarity(); // 88.888888888889
echo $closestOne->getLevenshteinDistance(); // 1

echo $fuzzio->getMaxSimilarity(); // 88.888888888889

// Is there an exact match in the collection
$fuzzio->hasExactMatch(); // false

// Add string to haystack
$fuzzio->addToHaystack(['julie']);

// Remove strings from haystack
$fuzzio->removeFromHaystack(['janie', 'julie']);

You can set the similarity threshold and the Levenshtein distance threshold for filtering results:

use \Nullform\Fuzzio\Fuzzio;

$needle = 'john'; // Reference string
$haystack = ['jon', 'johns', 'jane', 'janie']; // Array of strings

$fuzzio = new Fuzzio($needle);

// Set max Levenshtein distance threshold
$fuzzio->setMaxLevenshteinDistanceThreshold(1);

// Set min similarity threshold
$fuzzio->setMinSimilarityThreshold(80);

// Set array of strings to calculate similarity
$fuzzio->setHaystack($haystack);

// Get strings with similarity >= 80% and Levenshtein distance <= 1
$collection = $fuzzio->get();

You can set a normalizer for more predictable calculations:

use \Nullform\Fuzzio\Fuzzio;

$needle = 'John'; // Reference string
$haystack = ['Jon ', 'Johns', 'JANE', 'Janie']; // Array of strings

$normalizer = function ($string) {
    return trim(strtolower($string));
};

$fuzzio = new Fuzzio($needle);

// Normalizer for $needle and $haystack
$fuzzio->setNormalizer($normalizer);
// Set array of strings to calculate similarity
$fuzzio->setHaystack($haystack);

// Or like this
$fuzzio = new Fuzzio($needle, $haystack, $normalizer);

echo $fuzzio->getNeedle(); // John
echo $fuzzio->getNormalizedNeedle(); // john

// One with max similarity
$closest = $fuzzio->getClosestOne();

echo $closest; // Johns
echo $closest->getString(); // Johns
echo $closest->getNormalizedString(); // johns
echo $closest->getSimilarity(); // 88.888888888889
echo $closest->getLevenshteinDistance(); // 1

Methods

Fuzzio

  • Fuzzio::__construct(string $needle, string[]|null $haystack = null, callable|null $normalizer = null)
  • Fuzzio::getNeedle(): string
  • Fuzzio::getNormalizedNeedle(): string
  • Fuzzio::setHaystack(string[] $haystack): Fuzzio
  • Fuzzio::getHaystack(): string[]
  • Fuzzio::getNormalizedHaystack(): string[]
  • Fuzzio::addToHaystack(string[] $strings): Fuzzio
  • Fuzzio::removeFromHaystack(string[] $strings): Fuzzio
  • Fuzzio::hasExactMatch(): bool
  • Fuzzio::get(float|null $minSimilarity = null, int|null $maxLevenshteinDistance = null): FuzzioString[]
  • Fuzzio::getClosest(): FuzzioString[]
  • Fuzzio::getClosestOne(): FuzzioString
  • Fuzzio::getMaxLevenshteinDistanceThreshold(): int|null
  • Fuzzio::setMaxLevenshteinDistanceThreshold(int $threshold): Fuzzio
  • Fuzzio::getMinSimilarityThreshold(): float|null
  • Fuzzio::setMinSimilarityThreshold(float $threshold): Fuzzio
  • Fuzzio::getMaxSimilarity(): float|null
  • Fuzzio::getMinLevenshteinDistance(): int|null
  • Fuzzio::setNormalizer(callable|null $normalizer): Fuzzio

FuzzioString

  • FuzzioString::getString(): string
  • FuzzioString::getNormalizedString(): string
  • FuzzioString::getSimilarity(): float
  • FuzzioString::getLevenshteinDistance(): int
  • FuzzioString::__toString(): string

nullform/fuzzio 适用场景与选型建议

nullform/fuzzio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 683 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「search」 「levenshtein」 「similarity」 「fuzzy」 「similar_text」 「levenshtein distance」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nullform/fuzzio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nullform/fuzzio 我们能提供哪些服务?
定制开发 / 二次开发

基于 nullform/fuzzio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 683
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-02