net.sourceforge.wurfl.core.handlers.matchers.strategy
Class LDMatcher

java.lang.Object
  extended by net.sourceforge.wurfl.core.handlers.matchers.strategy.LDMatcher
All Implemented Interfaces:
StringMatcher

public final class LDMatcher
extends Object
implements StringMatcher

Singleton class responsible applying the Levenshtein Distance Algorithm to matching strings. This class use a custom implementation.

Version:
$Id: LDMatcher.java 432 2010-05-06 12:12:53Z filippo.deluca $
Author:
Fantayeneh Asres Gizaw, Filippo De Luca

Field Summary
static LDMatcher INSTANCE
          Singleton instance
 
Method Summary
static int getLevenshteinDistance(String s, String t, int tolerance)
           Find the Levenshtein distance between two Strings.
 String getName()
           
 String match(SortedSet candidates, String needle, int tolerance)
          Searches for the string which has the minor Levenshtein distance from given needle.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INSTANCE

public static final LDMatcher INSTANCE
Singleton instance

Method Detail

getName

public String getName()

match

public String match(SortedSet candidates,
                    String needle,
                    int tolerance)
Searches for the string which has the minor Levenshtein distance from given needle. If there is not candidates within given tolerance, it returns null.

Specified by:
match in interface StringMatcher
Parameters:
candidates - The SortedSet of possible candidates.
needle - The String to match.
tolerance - the tolerance between needle and candidates.
Returns:
Matched candidate String.

getLevenshteinDistance

public static int getLevenshteinDistance(String s,
                                         String t,
                                         int tolerance)

Find the Levenshtein distance between two Strings.

This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

The previous implementation of the Levenshtein distance algorithm was from http://www.merriampark.com /ld.htm

Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError which can occur when my Java implementation is used with very large strings.
This implementation of the Levenshtein distance algorithm is from http://www.merriampark.com/ ldjava.htm

 StringUtils.getLevenshteinDistance(null, *)             = IllegalArgumentException
 StringUtils.getLevenshteinDistance(*, null)             = IllegalArgumentException
 StringUtils.getLevenshteinDistance("","")               = 0
 StringUtils.getLevenshteinDistance("","a")              = 1
 StringUtils.getLevenshteinDistance("aaapppp", "")       = 7
 StringUtils.getLevenshteinDistance("frog", "fog")       = 1
 StringUtils.getLevenshteinDistance("fly", "ant")        = 3
 StringUtils.getLevenshteinDistance("elephant", "hippo") = 7
 StringUtils.getLevenshteinDistance("hippo", "elephant") = 7
 StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8
 StringUtils.getLevenshteinDistance("hello", "hallo")    = 1
 

Parameters:
s - the first String, must not be null
t - the second String, must not be null
tolerance - the minimum distance
Returns:
result distance
Throws:
IllegalArgumentException - if either String input null

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008-2010 WURFL-Pro srl. All Rights Reserved.