PHP Classes

File: tests/FuzzySearch/ConsiderFieldLengthTest.php

Recommend this page to a friend!
  Classes of AccountKiller   Fuse   tests/FuzzySearch/ConsiderFieldLengthTest.php   Download  
File: tests/FuzzySearch/ConsiderFieldLengthTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Fuse
Fuzzy search of arrays using the Bitap algorithm
Author: By
Last change:
Date: 1 month ago
Size: 1,739 bytes
 

 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
Fuse\Test;

use
Fuse\Fuse;

it('shows the entry with the shorter field length first', function () {
   
$list = [
        [
           
'ISBN' => '0312696957',
           
'title' => 'The Lock war Artist nonficon',
           
'author' => 'Steve Hamilton',
           
'tags' => ['fiction war hello no way'],
        ],
        [
           
'ISBN' => '0765348276',
           
'title' => 'Old Man\'s War',
           
'author' => 'John Scalzi',
           
'tags' => ['fiction no'],
        ],
    ];

   
$fuse = new Fuse($list, [
       
'keys' => ['title'],
    ]);

   
$result = $fuse->search('war');

   
expect($result)->toHaveCount(2);
   
expect($result[0]['item']['ISBN'])->toBe('0765348276');
   
expect($result[1]['item']['ISBN'])->toBe('0312696957');
});

it('gives high precedence to weighted entries', function () {
   
$list = [
        [
           
'ISBN' => '0312696957',
           
'title' => 'The Lock war Artist nonficon',
           
'author' => 'Steve Hamilton',
           
'tags' => ['fiction war hello no way'],
        ],
        [
           
'ISBN' => '0765348276',
           
'title' => 'Old Man\'s War',
           
'author' => 'John Scalzi',
           
'tags' => ['fiction no'],
        ],
    ];

   
$fuse = new Fuse($list, [
       
'keys' => [
            [
               
'name' => 'tags',
               
'weight' => 0.8,
            ],
            [
               
'name' => 'title',
               
'weight' => 0.2,
            ],
        ],
    ]);

   
$result = $fuse->search('war');

   
expect($result)->toHaveCount(2);
   
expect($result[0]['item']['ISBN'])->toBe('0312696957');
   
expect($result[1]['item']['ISBN'])->toBe('0765348276');
});