metaphone 


string apg

CALCULATE the metaphone key of a STRING.





This function is similar to soundex and creates the same key for similar sounding words, but more accurate than this because it is based on the basic rules of English pronunciation.

Metaphone was developed by Lawrence Philips.

It is described in ["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995].



<?php

str metaphone 
str $str )


where,

$str The STRING to calcule the metaphone

?>
 

$string


The STRING to get the metaphone.



  1 EXERCISE   

<?php

$arrstr01 
= [ 'widow''window'];

foreach(
$arrstr01 as $str01 => $s01)
{
    echo 
'[ ' $str01 ' ]&nbsp;' 
                   
$s01 ' => ' metaphone($s01) . '<br>';
}

?> 

 RESULT   

[ 0 ] widow => WT
[ 1 ] window => WNT


  2 EXERCISE   

<?php

$arrstr02 
= [ 'Doc',  'Grumpy',  'Happy',  
                                
'Sleepy',  'Bashful',  'Sneezy',  'Dopey' ];

foreach(
$arrstr02 as $str02 => $s02)
{
    echo 
'[ ' $str02 ' ]&nbsp;' 
                    
$s02 ' => ' metaphone($s02) . '<br>';
}

?>

 RESULT   

[ 0 ] Doc => TK
[ 1 ] Grumpy => KRMP
[ 2 ] Happy => HP
[ 3 ] Sleepy => SLP
[ 4 ] Bashful => BXFL
[ 5 ] Sneezy => SNS
[ 6 ] Dopey => TP


  3 EXERCISE   

<?php

$arrstr03 
= [ 'Soneca''Dengoso''Feliz'
                                      
'Atchim''Mestre''Zangado''Dunga' ];

foreach(
$arrstr03 as $str03 => $s03)
{
    echo 
'[ ' $str03 ' ]&nbsp;' 
                   
$s03 ' => ' metaphone($s03) . '<br>';
}

?> 

 RESULT   

[ 0 ] Soneca => SNK
[ 1 ] Dengoso => TNKS
[ 2 ] Feliz => FLS
[ 3 ] Atchim => AXM
[ 4 ] Mestre => MSTR
[ 5 ] Zangado => SNKT
[ 6 ] Dunga => TNK


  4 EXERCISE   

<?php

var_dump
(metaphone(""));
echo 
'<br><br>';
var_dump(metaphone(-1));
echo 
'<br><br>';

try {
    
var_dump(metaphone("valid phrase", -1));
    echo 
'<br><br>';
} catch (
ValueError $e) {
    echo 
$e->getMessage(), "<br>";
}
var_dump(metaphone("valid phrase"0));
echo 
'<br><br>';
var_dump(metaphone("valid phrase"10000));

$array = array(
"They fell forward, grovelling heedlessly 
on the cold earth."
,
"But the shadow of horror wheeled and returned, 
passing lower now, right above them, sweeping 
the fen-reek with its ghastly wings."
,
"And then it was gone, flying back to Mordor 
with the speed of the wrath of Sauron; and behind 
it the wind roared away, leaving the Dead Marshes 
bare and bleak."
,
"The naked waste, as far as the eye could pierce, 
even to the distant menace of the mountains, 
was dappled with the fitful moonlight."
);

foreach(
$array as $k => $v) {
    echo 
"[ $k ] $v => " metaphone($v) . "<br><br>";
}

?>