项目作者: wyndow

项目描述 :
Fuzzy string matching for PHP
高级语言: PHP
项目地址: git://github.com/wyndow/fuzzywuzzy.git
创建时间: 2015-08-21T22:09:39Z
项目社区:https://github.com/wyndow/fuzzywuzzy

开源协议:

下载


FuzzyWuzzy

Build Status

Fuzzy string matching for PHP, based on the python library of the same name.

Requirements

  • PHP 5.4 or higher

Installation

Using Composer

  1. composer require wyndow/fuzzywuzzy

Usage

  1. use FuzzyWuzzy\Fuzz;
  2. use FuzzyWuzzy\Process;
  3. $fuzz = new Fuzz();
  4. $process = new Process($fuzz); // $fuzz is optional here, and can be omitted.

Simple Ratio

  1. >>> $fuzz->ratio('this is a test', 'this is a test!')
  2. => 96

Partial Ratio

  1. >>> $fuzz->partialRatio('this is a test', 'this is a test!')
  2. => 100

Token Sort Ratio

  1. >>> $fuzz->ratio('fuzzy wuzzy was a bear', 'wuzzy fuzzy was a bear')
  2. => 90
  3. >>> $fuzz->tokenSortRatio('fuzzy wuzzy was a bear', 'wuzzy fuzzy was a bear')
  4. => 100

Token Set Ratio

  1. >>> $fuzz->tokenSortRatio('fuzzy was a bear', 'fuzzy fuzzy was a bear')
  2. => 84
  3. >>> $fuzz->tokenSetRatio('fuzzy was a bear', 'fuzzy fuzzy was a bear')
  4. => 100

Process

  1. >>> $choices = ['Atlanta Falcons', 'New York Jets', 'New York Giants', 'Dallas Cowboys']
  2. >>> $c = $process->extract('new york jets', $choices, null, null, 2)
  3. => FuzzyWuzzy\Collection {#205}
  4. >>> $c->toArray()
  5. => [
  6. [
  7. "New York Jets",
  8. 100,
  9. ],
  10. [
  11. "New York Giants",
  12. 78,
  13. ],
  14. ]
  15. >>> $process->extractOne('cowboys', $choices)
  16. => [
  17. "Dallas Cowboys",
  18. 90,
  19. ]

You can also pass additional parameters to extractOne to make it use a specific scorer.

  1. >>> $process->extractOne('cowbell', $choices, null, [$fuzz, 'ratio'])
  2. => [
  3. "Dallas Cowboys",
  4. 38,
  5. ]
  6. >>> $process->extractOne('cowbell', $choices, null, [$fuzz, 'tokenSetRatio'])
  7. => [
  8. "Dallas Cowboys",
  9. 57,
  10. ]

Caveats

Unicode strings may produce unexpected results. We intend to correct this in future versions.

Further Reading