<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://ubersoft.org/Sputnik/wiki/skins/common/feed.css?301"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
		<id>http://ubersoft.org/Sputnik/wiki/index.php?action=history&amp;feed=atom&amp;title=Core_Function_Levenshtein</id>
		<title>Core Function Levenshtein - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://ubersoft.org/Sputnik/wiki/index.php?action=history&amp;feed=atom&amp;title=Core_Function_Levenshtein"/>
		<link rel="alternate" type="text/html" href="http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;action=history"/>
		<updated>2026-04-12T00:48:11Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.17.0</generator>

	<entry>
		<id>http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=4806&amp;oldid=prev</id>
		<title>UberFoX: 1 revision</title>
		<link rel="alternate" type="text/html" href="http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=4806&amp;oldid=prev"/>
				<updated>2015-06-14T12:37:37Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 12:37, 14 June 2015&lt;/td&gt;
		&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>UberFoX</name></author>	</entry>

	<entry>
		<id>http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=4805&amp;oldid=prev</id>
		<title>UberFoX: 1 revision</title>
		<link rel="alternate" type="text/html" href="http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=4805&amp;oldid=prev"/>
				<updated>2014-08-11T21:41:04Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 21:41, 11 August 2014&lt;/td&gt;
		&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>UberFoX</name></author>	</entry>

	<entry>
		<id>http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=3216&amp;oldid=prev</id>
		<title>UberFoX: Created page with &quot;&lt;pre&gt; Levenshtein( &lt;str1&gt;, &lt;str2&gt;, &lt;cost_ins&gt;, &lt;cost_rep&gt;, &lt;cost_de&gt; ) &lt;/pre&gt;  === Description ===  Calculate Levenshtein distance between two strings.  === Parameters ===  ==== ...&quot;</title>
		<link rel="alternate" type="text/html" href="http://ubersoft.org/Sputnik/wiki/index.php?title=Core_Function_Levenshtein&amp;diff=3216&amp;oldid=prev"/>
				<updated>2013-09-20T09:20:43Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;pre&amp;gt; Levenshtein( &amp;lt;str1&amp;gt;, &amp;lt;str2&amp;gt;, &amp;lt;cost_ins&amp;gt;, &amp;lt;cost_rep&amp;gt;, &amp;lt;cost_de&amp;gt; ) &amp;lt;/pre&amp;gt;  === Description ===  Calculate Levenshtein distance between two strings.  === Parameters ===  ==== ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
Levenshtein( &amp;lt;str1&amp;gt;, &amp;lt;str2&amp;gt;, &amp;lt;cost_ins&amp;gt;, &amp;lt;cost_rep&amp;gt;, &amp;lt;cost_de&amp;gt; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Description ===&lt;br /&gt;
&lt;br /&gt;
Calculate Levenshtein distance between two strings.&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
&lt;br /&gt;
==== string ====&lt;br /&gt;
&lt;br /&gt;
The input string. &lt;br /&gt;
&lt;br /&gt;
=== Return Value ===&lt;br /&gt;
&lt;br /&gt;
This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters. &lt;br /&gt;
&lt;br /&gt;
=== Remarks ===&lt;br /&gt;
&lt;br /&gt;
The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.&lt;br /&gt;
&lt;br /&gt;
The complexity of the algorithm is O(m*n), where n and m are the length of str1 and str2 (rather good when compared to similar_text(), which is O(max(n,m)**3), but still expensive).&lt;br /&gt;
&lt;br /&gt;
In its simplest form the function will take only the two strings as parameter and will calculate just the number of insert, replace and delete operations needed to transform str1 into str2.&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sputnik&amp;quot;&amp;gt;&lt;br /&gt;
printf(&amp;quot;%s\n&amp;quot;, Levenshtein(&amp;quot;Dog&amp;quot;, &amp;quot;Cat&amp;quot;));&lt;br /&gt;
// Prints 3&lt;br /&gt;
echo Levenshtein(&amp;quot;Hello World&amp;quot;,&amp;quot;ello World&amp;quot;,10,20,30);&lt;br /&gt;
// Prints 30&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Practical example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sputnik&amp;quot;&amp;gt;&lt;br /&gt;
// input misspelled word&lt;br /&gt;
my $input = 'carrrot';&lt;br /&gt;
&lt;br /&gt;
// array of words to check against&lt;br /&gt;
my $words  = array('apple','pineapple','banana','orange',&lt;br /&gt;
                'radish','carrot','pea','bean','potato');&lt;br /&gt;
&lt;br /&gt;
// no shortest distance found, yet&lt;br /&gt;
my $shortest = -1;&lt;br /&gt;
&lt;br /&gt;
// no closest found, yet&lt;br /&gt;
my $closest;&lt;br /&gt;
&lt;br /&gt;
// loop through words to find the closest&lt;br /&gt;
foreach ($words as $word) &lt;br /&gt;
{&lt;br /&gt;
    // calculate the distance between the input word,&lt;br /&gt;
    // and the current word&lt;br /&gt;
    my $lev = levenshtein($input, $word);&lt;br /&gt;
&lt;br /&gt;
    // check for an exact match&lt;br /&gt;
    if ($lev == 0) &lt;br /&gt;
    {&lt;br /&gt;
        // closest word is this one (exact match)&lt;br /&gt;
        $closest = $word;&lt;br /&gt;
        $shortest = 0;&lt;br /&gt;
        // break out of the loop; we've found an exact match&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // if this distance is less than the next found shortest&lt;br /&gt;
    // distance, OR if a next shortest word has not yet been found&lt;br /&gt;
    if ($lev &amp;lt;= $shortest || $shortest &amp;lt; 0)&lt;br /&gt;
    {&lt;br /&gt;
        // set the closest match, and shortest distance&lt;br /&gt;
        $closest  = $word;&lt;br /&gt;
        $shortest = $lev;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
echo &amp;quot;Input word: $input\n&amp;quot;;&lt;br /&gt;
if ($shortest == 0)&lt;br /&gt;
    echo &amp;quot;Exact match found: $closest\n&amp;quot;;&lt;br /&gt;
else &lt;br /&gt;
    echo &amp;quot;Did you mean: $closest?\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Core Function]]&lt;/div&gt;</summary>
		<author><name>UberFoX</name></author>	</entry>

	</feed>