designbycode/fuzzy-search 问题修复 & 功能扩展

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

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

designbycode/fuzzy-search

最新稳定版本:v1.0.3

Composer 安装命令:

composer require designbycode/fuzzy-search

包简介

The Fuzzy Search package provides a simple and efficient way to perform fuzzy searches on a collection of texts using the Levenshtein distance algorithm. This package is useful when you need to search for texts that may contain typos or slight variations.

README 文档

README

Latest Version on Packagist Tests Total Downloads

Introduction

The Fuzzy Search package provides a simple and efficient way to perform fuzzy searches on a collection of texts using the Levenshtein distance algorithm. This package is useful when you need to search for texts that may contain typos or slight variations.

Installation

To install the Fuzzy Search package, simply require it in your PHP project using Composer:

composer require designbycode/fuzzy-search

Usage

Creating a Fuzzy Search Instance

To create a Fuzzy Search instance, you need to pass an array of texts to search and an optional flag for case-insensitive search:

use Designbycode\FuzzySearch\FuzzySearch;

$texts = ['apple', 'banana', 'orange', 'grape'];
$fuzzySearch = new FuzzySearch($texts, true); // Case-insensitive search

Performing a Fuzzy Search

To perform a fuzzy search, call the search method and pass the search query and an optional maximum Levenshtein distance:

$query = 'aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: ['apple']

The search method returns an array of matching texts, sorted by their Levenshtein distance from the search query.

Getting the Best Match

To get the best match from the search results, call the getBestMatch method:

$bestMatch = $fuzzySearch->getBestMatch($results);
echo $bestMatch; // Output: 'apple'

Levenshtein Distance Calculator

The Levenshtein Distance Calculator is a utility class that calculates the Levenshtein distance between two strings. This class is used internally by the Fuzzy Search package.

Calculating the Levenshtein Distance

To calculate the Levenshtein distance between two strings, call the calculate method:

use Designbycode\FuzzySearch\LevenshteinDistance;

$str1 = 'kitten';
$str2 = 'sitting';
$distance = LevenshteinDistance::calculate($str1, $str2);
echo $distance; // Output: 3

Examples

Example 1: Fuzzy Search with Case-Insensitive Search

$texts = ['Apple', 'Banana', 'Orange', 'Grape'];
$fuzzySearch = new FuzzySearch($texts, true);

$query = 'aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: ['Apple']

Example 2: Fuzzy Search with Case-Sensitive Search

$texts = ['apple', 'banana', 'orange', 'grape'];
$fuzzySearch = new FuzzySearch($texts, false);

$query = 'Aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: []

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-20