PHP Classes

File: test/php/test-fuzzy-nfa.php

Recommend this page to a friend!
  Packages of Nikos M.   Matchy   test/php/test-fuzzy-nfa.php   Download  
File: test/php/test-fuzzy-nfa.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Matchy
Perform exact or fuzzy searches in text strings
Author: By
Last change: v.4.0.0, almost complete

* php avoid strict equality potential failures
* update more tests
Date: 5 months ago
Size: 2,728 bytes
 

Contents

Class file image Download
<?php
include(dirname(__FILE__) . '/../../src/php/Matchy.php');

function
NFA($input, $type = 'l')
{
    return new
MatchyNFA($input, $type);
}
function
create_string($alphabet, $n)
{
   
$s = '';
    for (
$i=0; $i<$n; ++$i)
    {
       
$s .= $alphabet[rand(0, count($alphabet)-1)];
    }
    return
$s;
}
function
test_case($nfa, $pattern, $string, $match, $errors)
{
   
$found = $nfa->match($string, 0, false, true);
    echo(
'fuzzynfa("'.$pattern.'", "'.$string.'") = '.$found['match'].", errors ".$found['errors']." (expected ".$match.", errors ".$errors.")"."\n");
}
function
test()
{
   
$test = ['pattern'=>'^(a+)(b+)$', 'nfa'=>NFA(NFA([NFA('', '^'), NFA(NFA('a'), '+'), NFA(NFA('b'), '+'), NFA('', '$')], ','), ['total_errors'=>2,'word_level'=>false])];
   
test_case($test['nfa'], $test['pattern'], "aaabbbbb", 0, 0); // 0 errors
   
test_case($test['nfa'], $test['pattern'], "ababbbbb", 0, 1); // 1 errors
   
test_case($test['nfa'], $test['pattern'], "abababbb", 0, 2); // 2 errors
   
test_case($test['nfa'], $test['pattern'], "abababab", -1, 3); // 3 errors
   
test_case($test['nfa'], $test['pattern'], "aabababbbb", 0, 2); // 2 errors
   
test_case($test['nfa'], $test['pattern'], "baabaaabbb", 0, 2); // 2 errors
   
echo("\n");

   
$test = ['pattern'=>'(aaa)(bbb)', 'nfa'=>NFA(NFA([NFA('aaa'), NFA('bbb')], ','), ['total_errors'=>1,'word_level'=>true])];
   
test_case($test['nfa'], $test['pattern'], "aaabbb", 0, 0); // 0 errors
   
test_case($test['nfa'], $test['pattern'], "bbbaaa", 0, 1); // 1 errors, deletion
   
test_case($test['nfa'], $test['pattern'], "bbb", 0, 1); // 1 errors, deletion
   
test_case($test['nfa'], $test['pattern'], "cbbb", 0, 1); // 1 errors, deletion or substitution
   
test_case($test['nfa'], $test['pattern'], "aacbbb", 0, 1); // 1 errors, deletion or substitution
   
test_case($test['nfa'], $test['pattern'], "aaacbbb", 0, 1); // 1 errors, insertion
   
echo("\n");

   
$test = ['pattern'=>'^(aaa)(bbb)$', 'nfa'=>NFA(NFA([NFA('', '^'), NFA('aaa'), NFA('bbb'), NFA('', '$')], ','), ['total_errors'=>1,'word_level'=>true,'transpositions'=>true])];
   
test_case($test['nfa'], $test['pattern'], "aaabbb", 0, 0); // 0 errors
   
test_case($test['nfa'], $test['pattern'], "bbbaaa", 0, 1); // 1 errors, transposition

   
test_case($test['nfa'], $test['pattern'], "bbb", 0, 1); // 1 errors, deletion
   
test_case($test['nfa'], $test['pattern'], "cbbb", 0, 1); // 1 errors, substitution
   
test_case($test['nfa'], $test['pattern'], "aacbbb", 0, 1); // 1 errors, substitution
   
test_case($test['nfa'], $test['pattern'], "aaacbbb", 0, 1); // 1 errors, insertion
   
test_case($test['nfa'], $test['pattern'], "cccbbb", 0, 1); // 1 errors, substitution
   
echo("\n");
}

test();