<?php
/* Copyright (c) 2009, J.P.Westerhof <jurgen.westerhof@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 */

 
if(isset($_GET['show_source'])) {
    
show_source(__FILE__);
    die();
}

define('DIAGONAL'0);
define('VERTICAL'1);
define('HORIZONTAL'2);

function 
getLevenshteinMatrix($from$to) {
    
//step 1. The first step is to contruct a matrix of n+1 by m+1, where n is the number of words in the first line and m the number of words in the second line.
    
$lm = array();
    
    
//step 2. Secondly, fill the first row and column with (from top left to bottom or right) zero to respectively m and n.
    
for($n 0$n <= count($from); $n++)
        
$lm[0][$n] = $n;
    for(
$m 0$m <= count($to); $m++)
        
$lm[$m][0] = $m;
    
    
//step 3. Now for each n, evaluate each m. If  the evaluated word of n matches m, the cost is 0, otherwise it’s 1.
    
for($n 1$n <= count($from); $n++) {
        for(
$m 1$m <= count($to); $m++) {
            
$cost = ($from[$n-1] == $to[$m-1])? 01;
            
            
//step 4. Fill out cell (n, m) having (n, m) is the minumum of:
            //      * The value of the cell above + 1
            //      * The left neighbour cell value + 1
            //      * The above left cell value + cost
            
$above        $lm[$m 1][$n] + 1;
            
$left        $lm[$m][$n 1] + 1;
            
$topleft    $lm[$m 1][$n 1] + $cost;
            
            
$lm[$m][$n]    = min($abovemin($left$topleft));
        }
    }
    
    return 
$lm;
}

function 
test_levenshtein($from$to) {
    if(!
is_array($from))
        return 
levenshtein(explode(''$from), $to);
    if(!
is_array($to))
        return 
levenshtein($fromexplode(''$to));
    
    
$lm getLevenshteinMatrix($from$to);
    
    return 
$lm[count($lm) - 1][count($lm[count($lm) - 1]) - 1];
}

function 
getTransformations($from$to) {
    
$transformations = array();
    
$directions = array();
    
    
$lm getLevenshteinMatrix($from$to);
    
    
$n count($from);
    
$m count($to);
    while(
$n != && $m != 0) {
        
$direction = -1;
        
//Pick the lowest cost route, but diagonal move is only allowed if the words are the same, else either vertical of horizontal route must be taken
        
        
$diagonal $lm[$m 1][$n 1];
        
$vertical $lm[$m 1][$n];
        
$horizontal $lm[$m][$n 1];

        if(
$from[$n 1] == $to[$m 1])
            
$direction DIAGONAL;
        
        if(
$vertical $diagonal)
            
$direction VERTICAL;        
        
        if(
$horizontal $diagonal)
            
$direction HORIZONTAL;
        
        if(
$direction == -1)
            
$direction = ($horizontal <= $vertical)? HORIZONTALVERTICAL;
        
        if(
$direction == VERTICAL || $direction == DIAGONAL)
            
$m--;
        if(
$direction == HORIZONTAL || $direction == DIAGONAL)
            
$n--;

        
$directions[] = array($direction$from[$n], $to[$m]);
    }
    
    for(
$i count($directions) - 1$i >= 0$i--) {
        if(
$directions[$i][0] == DIAGONAL) {
            
$transformations[] = array('unchanged'$directions[$i][1]);
        } elseif(
$directions[$i][0] == HORIZONTAL) {
            
$transformations[] = array('deleted'$directions[$i][1]);
        } elseif(
$directions[$i][0] == VERTICAL) {
            
$transformations[] = array('added'$directions[$i][2]);
        }
    }
    
    return 
$transformations;
}

function 
mergeTransformations($transformations) {
    
$merged = array();
    
    
$state 'unchanged';
    
    
$unchanged = array();
    
$deleted = array();
    
$added = array();
    
    foreach(
$transformations as $transformation) {
        if(
$transformation[0] == 'unchanged') {
            if(
count($added) > 0)
                
$merged[] = array('added'implode(' '$added));
            if(
count($deleted) > 0)
                
$merged[] = array('deleted'implode(' '$deleted));
            
            
$unchanged[] = $transformation[1];

            
$added = array();
            
$deleted = array();
        } else {
            if(
$state == 'unchanged') {
                
$merged[] = array('unchanged'implode(' '$unchanged));

                
$unchanged = array();
            }
            
            if(
$transformation[0] == 'added')
                
$added[] = $transformation[1];
            else
                
$deleted[] = $transformation[1];
        }
        
        
$state $transformation[0];
    }
    
    if(
count($added) > 0)
        
$merged[] = array('added'implode(' '$added));
    if(
count($deleted) > 0)
        
$merged[] = array('deleted'implode(' '$deleted));
    if(
count($unchanged) > 0)
        
$merged[] = array('unchanged'implode(' '$unchanged));
    
    return 
$merged;
}