PHP Classes

File: libphonetiq.class.php

Recommend this page to a friend!
  Classes of Richard Carson   Ssearch   libphonetiq.class.php   Download  
File: libphonetiq.class.php
Role: Class source
Content type: text/plain
Description: rename
Class: Ssearch
Search and rank text results in an array
Author: By
Last change: Merge branch 'master' of gitlab.richardcarson.ca:rscarson/libphonetiq
Date: 10 years ago
Size: 1,794 bytes
 

Contents

Class file image Download
<?php
 
/*
   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
   
 
class PhonetiQ
 
{
    private
$query;
    private
$query_len;

    public function
__construct()
    {
     
$this->query = null;
     
$this->query_len = 0;
    }

    public function
begin($query)
    {
     
$this->query = $query;
     
$this->query_len = strlen($query);
    }

    public function
next($row)
    {
      if (
$this->query_len == 0)
       return
false;

     
$score = 0;
     
$entries = count($row);
      foreach (
$row as $str)
       
$score += $this->compare($str);

      if (
$score == 0)
       return
false;

      return
$score;
    }

    private function
compare($str)
    {
     
$str = $str;
      return
$this->str_cmp(metaphone($this->query), metaphone($str));
    }

    private function
str_cmp($str1, $str2)
    {
     
$score = 0;
     
$str1 = $this->reg_prep($str1);
     
preg_match_all("/([$str1]){2,}/", $str2, $out);
      for (
$i=0; $i<count($out[0]); $i++)
       
$score += strlen($out[0][$i]);
      return
$score;
    }

    private function
reg_prep($s)
    {
     
$o = '';
      for (
$i=0; $i<strlen($s); $i++)
       
$o .= $s[$i].'?';
      return
$o;
    }
  }
?>