承接 designbycode/levenshtein-distance 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

designbycode/levenshtein-distance

最新稳定版本:v1.0.1

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

Latest Version on Packagist Tests Total Downloads

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

  1. Spell Checking: Calculate the Levenshtein distance between a user's input and a list of known words to suggest corrections.
  2. Text Similarity: Measure the similarity between two pieces of text by calculating the Levenshtein distance.
  3. 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.

统计信息

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

GitHub 信息

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

其他信息

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