designbycode/levenshtein-distance
Composer 安装命令:
composer require designbycode/levenshtein-distance
包简介
The LevenshteinDistance class provides a method to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.
README 文档
README
The LevenshteinDistance class provides a method to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.
Installation
You can install the package via composer:
composer require designbycode/levenshtein-distance
Method: calculate
Parameters
- mixed $str1: The first string.
- mixed $str2: The second string.
Return Value
- int: The Levenshtein distance between the two strings.
Description
- Calculates the Levenshtein distance between two strings. The method throws a TypeError if either of the input parameters is not a string.
Use Cases
- Spell Checking: Calculate the Levenshtein distance between a user's input and a list of known words to suggest corrections.
- Text Similarity: Measure the similarity between two pieces of text by calculating the Levenshtein distance.
- Data Validation: Verify the correctness of user input by calculating the Levenshtein distance between the input and a known valid value.
Usage
Example 1: Calculating the Levenshtein distance between two strings
$str1 = 'kitten'; $str2 = 'sitting'; $distance = LevenshteinDistance::calculate($str1, $str2); echo "Levenshtein distance: $distance"; // Output: 3
Example 2: Handling non-string input
$str1 = 'hello'; $nonString = 123; try { LevenshteinDistance::calculate($str1, $nonString); } catch (TypeError $e) { echo "Error: " . $e->getMessage(); // Output: Argument 2 passed to LevenshteinDistance::calculate() must be of the type string }
Example 3: Using Levenshtein distance for spell checking
$userInput = 'teh'; $knownWords = ['the', 'tea', 'ten']; $minDistance = PHP_INT_MAX; $closestWord = ''; foreach ($knownWords as $word) { $distance = LevenshteinDistance::calculate($userInput, $word); if ($distance < $minDistance) { $minDistance = $distance; $closestWord = $word; } } echo "Did you mean: $closestWord"; // Output: Did you mean: the
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.
designbycode/levenshtein-distance 适用场景与选型建议
designbycode/levenshtein-distance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「levenshtein」 「levenshtein-distance」 「designbycode」 「levenshtein distance」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 designbycode/levenshtein-distance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 designbycode/levenshtein-distance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 designbycode/levenshtein-distance 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Calculate the Levenshtein distance and edit progresses between two strings.
Get text similarity level with Damerau-Levenshtein distance
A powerful, zero-config fuzzy search package for Laravel with fluent API
Collection of string similarity and distance algorithms in PHP including Levenshtein, Damerau-Levenshtein, Jaro-Winkler, and more
Fuzzy search using similar_text() and levenshtein() functions. Easy to use and safe for multibyte encodings (UTF-8).
Compare two string and get a similarity percentage
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-20