metaphone
=======================
The metaphone() function can be used for spelling applications.This function returns the metaphone key of the string on success, or FALSE on failure.Its main use is when you are searching a genealogy database. check to see if a metaphone search is offered. It is also useful in making/searching family tree.
Given below is a simple code that calculates and compares two strings to find whether its metaphone codes are equivalent.
html code
==========
<html>
<body>
<form action="test.php" name="test" method="get">
Name1:<input type="text" name="name1" /><br />
Name2:<input type="text" name="name2" /><br />
<input type="submit" name="submit" value="compare" />
</form>
<!--php code begins here -->
<?php
if(isset($_GET['submit']))
{
$str1 = $_GET['name1'];
$str2 = $_GET['name2'];
$meta_one=metaphone($str1);
$meta_two=metaphone($str2);
echo "metaphone code for ".$str1." is ". $meta_one;
echo "<br />";
echo "metaphone code for ".$str2." is ". $meta_two."<br>";
if($meta_one==$meta_two)
{
echo "metaphone codes are matching";
}
else
{
echo "metaphone codes are not matching";
}
}
?>
</body>
</html>
Metaphone algorithm was developed by Lawrence Philips.
Lawrence Philips' RULES follow:
The 16 consonant sounds:
|--- ZERO represents "th"
|
B X S K J T F H L M N P R 0 W Y
Exceptions:
Beginning of word: "ae-", "gn", "kn-", "pn-", "wr-" ----> drop first letter
"Aebersold", "Gnagy", "Knuth", "Pniewski", "Wright"
Beginning of word: "x" ----> change to "s"
as in "Deng Xiaopeng"
Beginning of word: "wh-" ----> change to "w"
as in "Whalen"
Transformations:
B ----> B unless at the end of word after "m", as in "dumb", "McComb"
C ----> X (sh) if "-cia-" or "-ch-"
S if "-ci-", "-ce-", or "-cy-"
SILENT if "-sci-", "-sce-", or "-scy-"
K otherwise, including in "-sch-"
D ----> J if in "-dge-", "-dgy-", or "-dgi-"
T otherwise
F ----> F
G ----> SILENT if in "-gh-" and not at end or before a vowel
in "-gn" or "-gned"
in "-dge-" etc., as in above rule
J if before "i", or "e", or "y" if not double "gg"
K otherwise
H ----> SILENT if after vowel and no vowel follows
or after "-ch-", "-sh-", "-ph-", "-th-", "-gh-"
H otherwise
J ----> J
K ----> SILENT if after "c"
K otherwise
L ----> L
M ----> M
N ----> N
P ----> F if before "h"
P otherwise
Q ----> K
R ----> R
S ----> X (sh) if before "h" or in "-sio-" or "-sia-"
S otherwise
T ----> X (sh) if "-tia-" or "-tio-"
0 (th) if before "h"
silent if in "-tch-"
T otherwise
V ----> F
W ----> SILENT if not followed by a vowel
W if followed by a vowel
X ----> KS
Y ----> SILENT if not followed by a vowel
Y if followed by a vowel
Z ----> S
metaphone
(PHP 4, PHP 5)
metaphone — 文字列の metaphone キーを計算する
説明
string metaphone
( string
$str
[, int $phonemes = 0
] )
str の metaphone キーを計算します。
soundex() と同様に metaphone は、 発音が似た単語について同じキーを作成します。metaphone は、 英語の発音の基本的ルールを知っているので、 soundex() よりも正確です。 metaphone が生成するキーは可変長です。
metaphone は、Lawrence Philips <lphilips at verity dot com> により 開発されました。["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995] で解説されています。
パラメータ
-
str -
入力文字列。
-
phonemes -
このパラメータは、返される metaphone キーの長さを最大
phonemes文字までに制限します。 デフォルト値の 0 は、無制限であることを意味します。
返り値
metaphone キーを文字列で返します。失敗した場合に FALSE を返します。
例
例1 metaphone() の基本的な例
<?php
var_dump(metaphone('programming'));
var_dump(metaphone('programmer'));
?>
上の例の出力は、 たとえば以下のようになります。
string(7) "PRKRMNK" string(6) "PRKRMR"
例2 phonemes パラメータの使用例
<?php
var_dump(metaphone('programming', 5));
var_dump(metaphone('programmer', 5));
?>
上の例の出力は、 たとえば以下のようになります。
string(5) "PRKRM" string(5) "PRKRM"
Vipindas K.S
09-May-2008 09:49
php at casadebender dot com
27-Mar-2008 10:24
A double metaphone pecl module is available: http://pecl.php.net/package/doublemetaphone
mail at spam-off dot iaindooley dot com
16-Jan-2004 12:37
you can use the metaphone function quite effectively with phrases by taking the levenshtein distances between two metaphone codes, and then taking this as a percentage of the length of the original metaphone code. thus you can define a percentage error, (say 20%) and accept only matches that are closer than that. i've found this works quite effectively in a function i am using on my website where an album name that the user entered is verified against existing album names that may be similar. this is also an excellent way of people being able to vaguely remember a phrase and get several suggestions out of the database. so you could type "i stiched nine times" with an error percentage of, say, 50 and still get 'a stitch in time saves nine' back as a match.
server: zinkconsulting.com user: galen@
26-Nov-2003 02:18
A small warning about the double metaphone function/class implemented in php: it's slow. Very slow.
On my Mac OS X box it takes roughly 4 seconds to calculate the double_metaphone of a given string 1,000 times and set a variable to that value. Given the exact same code, the built in metaphone function takes a mere 0.03 seconds. That's a huge difference. For reference, soundex takes about 0.015 seconds.
I had dreams of ranking my person database search results with double_metaphone, but with searches yielding a few thousand results sometimes, this isn't terribly reasonable. I'm back to basic metaphone functions.
I'd suggest anyone considering the use of the double_metaphone functionality weigh the slight improvement in results with the fact it's 100 times slower than the php function metaphone. I believe this difference is probably due mostly to implementation and optimization (php code vs. optimized internal php function).
Anybody up for writing a double_metaphone function for php? Maybe php 5?
