Re: cvs: phpdoc /en/reference/strings/functions levenshtein.xml
| From: | Sean Coates | Date: | Thu, 02 Dec 2004 18:21:32 +0000 |
| Subject: | Re: cvs: phpdoc /en/reference/strings/functions levenshtein.xml | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969365336@lists.php.net to get a copy of this message | ||
Sorry about the WS changes.
My editor trimmed the lines.
I diffed, but didn't notice them until now.
S
Sean Coates wrote:
sean Thu Dec 2 12:52:04 2004 EDTModified files: /phpdoc/en/reference/strings/functions levenshtein.xml Log:added example http://cvs.php.net/diff.php/phpdoc/en/reference/strings/functions/levenshtein.xml?r1=1.5&r2=1.6&ty=u Index: phpdoc/en/reference/strings/functions/levenshtein.xml diff -u phpdoc/en/reference/strings/functions/levenshtein.xml:1.5 phpdoc/en/reference/strings/functions/levenshtein.xml:1.6 --- phpdoc/en/reference/strings/functions/levenshtein.xml:1.5 Thu Oct 28 05:44:22 2004 +++ phpdoc/en/reference/strings/functions/levenshtein.xml Thu Dec 2 12:52:04 2004 @@ -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@@ -116,6 +116,68 @@</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 <function>metaphone</function>.