added example

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@173968 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Sean Coates 2004-12-02 17:52:04 +00:00
parent 5c9dccea61
commit a1366d5d9c

View file

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- $Revision: 1.6 $ -->
<!-- splitted from ./en/functions/strings.xml, last change in rev 1.12 -->
<refentry id="function.levenshtein">
<refnamediv>
<refname>levenshtein</refname>
<refname>levenshtein</refname>
<refpurpose>
Calculate Levenshtein distance between two strings
</refpurpose>
@ -48,14 +48,14 @@
insert, replace and delete operations needed to transform
<parameter>str1</parameter> into <parameter>str2</parameter>.
</para>
<para>
<para>
A second variant will take three additional parameters that
define the cost of insert, replace and delete operations. This
is more general and adaptive than variant one, but not as
efficient.
</para>
<!-- Callback function not yet implemented, see bug #29552
<para>
<para>
The third variant (which is not implemented yet) will be the most
general and adaptive, but also the slowest alternative. It will
call a user-supplied function that will determine the cost for
@ -105,7 +105,7 @@
describing the cost for this particular operation, but it may
decide to use only some of the supplied arguments.
</para>
<para>
<para>
The user-supplied function approach offers the possibility to
take into account the relevance of and/or difference between
certain symbols (characters) or even the context those symbols
@ -115,6 +115,68 @@
been worked into the other two variants.
</para>
-->
<para>
<example>
<title><function>levenshtein</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
// input misspelled word
$myWord = 'carrrot';
// array of words to check against
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($words AS $word) {
// calculate the distance between the input word,
// and the current word
$thisLev = levenshtein($myWord, $word);
// check for an exact match
if ($thisLev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($thisLev <= $shortest || $shortest < 0) {
// set the closest matchm, and shortest distance
$closest = $word;
$shortest = $thisLev;
}
}
echo "Input word: $myWord\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
?>
]]>
</programlisting>
</example>
&example.outputs;
<screen>
<![CDATA[
Input word: carrrot
Did you mean: carrot?
]]>
</screen>
</para>
<para>
See also <function>soundex</function>,
<function>similar_text</function>, and