de.folt.fuzzy
Class FuzzyNodeKey

java.lang.Object
  extended by java.util.Observable
      extended by de.folt.fuzzy.FuzzyNodeKey
All Implemented Interfaces:
java.io.Serializable

public class FuzzyNodeKey
extends java.util.Observable
implements java.io.Serializable

Basically a FuzzyNodeKey is an array of shorts of length maxFuzzyKeyLength. This array represents a point in an n-dimensional space (dimension = maxFuzzyKeyLength de.folt.fuzzy.FuzzyNodeKey#setMaxFuzzyKeyLength(int)).
The key is used by the FuzzyNode search algorithm search (and addFuzzyNode) to search FuzzyNode.search(FuzzyNode, int) resp. insert FuzzyNode FuzzyNode.insertFuzzyNode(FuzzyNode) (for more details: FuzzyNode).
A specific method is available for generating a key from a String FuzzyNodeKey(String) . FuzzyNodeKey(String string) and its versions FuzzyNodeKey(String, int) and FuzzyNodeKey(String, int, int) generate a FuzzyNodeKey based on n grams.

Author:
klemens
See Also:
Serialized Form

Constructor Summary
FuzzyNodeKey(short[] key)
          Generate a FuzzyNodeKey based on a short array.
FuzzyNodeKey(java.lang.String string)
          FuzzyNodeKey generates a FuzzyNodeKey for a string.
FuzzyNodeKey(java.lang.String string, int nGram)
          FuzzyNodeKey generates a FuzzyNodeKey for a string.
FuzzyNodeKey(java.lang.String string, int nGrams, int maxfuzzy)
          FuzzyNodeKey generates a FuzzyNodeKey for a string.
 
Method Summary
 int computeKeyDistance(FuzzyNodeKey fuzzyNodeKey)
          computeKeyDistance computes the distance between the two fuzzy node keys.
 int computeKeySum()
          computeKeySum computes the sum of the key elements (counters for a specific ngram) for (int i = 0; i < this.key.length; i++) { isum += (int) key[i]; }
 int computeKeySumTillLevel(int level)
          computeKeySumTillLevel computes the keysum till the level of the key node
 java.lang.String format()
          format produces a string which represents the key elements in a string separated by ","
Example: 4, [3,0,1,5]
static long getDefaultFuzzyBaseCharNumber()
           
static int getDefaultFuzzyKeyLength()
           
 long getFuzzyBaseCharNumber()
           
 int getFuzzyKeyLength()
           
 short[] getKey()
           
 int getKeysum()
          return the sum of the keys.
 int getNGrams()
           
static void setDefaultFuzzyBaseCharNumber(long defaultFuzzyBaseCharNumber)
           
static void setDefaultFuzzyKeyLength(int defaultFuzzyKeyLength)
           
 void setFuzzyBaseCharNumber(long fuzzybase_charnum)
           
 void setFuzzyKeyLength(int maxFuzzyKeyLength)
          This sets the length of the FuzzyKey.
 void setKey(short[] key)
           
 void setKeysum(int keysum)
           
 void setNGrams(int grams)
           
 
Methods inherited from class java.util.Observable
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FuzzyNodeKey

public FuzzyNodeKey(short[] key)
Generate a FuzzyNodeKey based on a short array. The array size must be equal to FuzzyKeyLength! The method computes the key sum using @see computeKeySum()

Parameters:
key - the key for the fuzzy node key to generate the array representing a key (n-dimensional space)

FuzzyNodeKey

public FuzzyNodeKey(java.lang.String string)
FuzzyNodeKey generates a FuzzyNodeKey for a string. It uses Trigrams (nGram = 3) and the default key length (maxFuzzyKeyLength) for key generation.

Parameters:
key - the string from which the key should be generated (using 3 for nGram and maxFuzzyKeyLength) this is a short array

FuzzyNodeKey

public FuzzyNodeKey(java.lang.String string,
                    int nGram)
FuzzyNodeKey generates a FuzzyNodeKey for a string. It uses the default key length (maxFuzzyKeyLength) for key generation.

Parameters:
string - the string from which the key should be generated - based on nGrams
nGram - nGrams the length of an nGram

FuzzyNodeKey

public FuzzyNodeKey(java.lang.String string,
                    int nGrams,
                    int maxfuzzy)
FuzzyNodeKey generates a FuzzyNodeKey for a string. An ngram of a string looks like that (shown for 3 = TriGram)
Hallo = [Hal, all, llo] The position of the ngram in the key is computed like that:
 for (i = 0; i < k - nGrams + 1; i++)
 { // loop over all chars until the last n
     lNum = 0;
     for (j = 0; j < nGrams; j++)
     {
         // sum up the n gram values
         // if we have n chars to sum up
         // Example: FUZZYBASE_CHARNUM = 10, 3Grams
         // Value of the trigram "abc" = a*10*10 + b*10 + c = 61*100 + 62*10 + 63 = 6783
         lNum = lNum * (long) FUZZYBASE_CHARNUM + (long) string.charAt(i + j);
     }
     // Position for Example: maxfuzzy = 48: 6783%48 = 15
     lNum = lNum % ((long) (maxfuzzy - 1)); // position of the nGram in the key - increment +1
     // Example: we increment now element 15 of key +1
     key[(int) lNum]++;
 }
 
The method computes the key sum using @see computeKeySum()

Parameters:
string - the string from which the key should be generated - based on nGrams
nGrams - the length of an nGram
maxfuzzy - the length of the fuzzy key to generate
Method Detail

getDefaultFuzzyBaseCharNumber

public static long getDefaultFuzzyBaseCharNumber()
Returns:
the defaultFuzzyBaseCharNumber

getDefaultFuzzyKeyLength

public static int getDefaultFuzzyKeyLength()
Returns:
the defaultFuzzyKeyLength

setDefaultFuzzyBaseCharNumber

public static void setDefaultFuzzyBaseCharNumber(long defaultFuzzyBaseCharNumber)
Parameters:
defaultFuzzyBaseCharNumber - the defaultFuzzyBaseCharNumber to set

setDefaultFuzzyKeyLength

public static void setDefaultFuzzyKeyLength(int defaultFuzzyKeyLength)
Parameters:
defaultFuzzyKeyLength - the defaultFuzzyKeyLength to set

computeKeyDistance

public int computeKeyDistance(FuzzyNodeKey fuzzyNodeKey)
computeKeyDistance computes the distance between the two fuzzy node keys.
The difference between the two keys is computed and the final sum divided by two.
Algorithm:
 distance = distance + Math.abs((int)fuzzyNodeKey.key[i] - (int)this.key[i]);<br>
 distance = distance / 2;
 

Parameters:
fuzzyNodeKey -
Returns:
the distance between the two keys; a positive number

computeKeySum

public int computeKeySum()
computeKeySum computes the sum of the key elements (counters for a specific ngram)
 for (int i = 0; i < this.key.length; i++)
 {
     isum += (int) key[i];
 }
 

Returns:
the key sum of the fuzzy key

computeKeySumTillLevel

public int computeKeySumTillLevel(int level)
computeKeySumTillLevel computes the keysum till the level of the key node

Parameters:
level - the level for the node
Returns:
the summed up key value till level

format

public java.lang.String format()
format produces a string which represents the key elements in a string separated by ","
Example:
 4, [3,0,1,5]
 

Returns:
a formated version of the key; key elements are separated by ","

getFuzzyBaseCharNumber

public long getFuzzyBaseCharNumber()
Returns:
the fUZZYBASE_CHARNUM

getFuzzyKeyLength

public int getFuzzyKeyLength()
Returns:
the maxFuzzyKeyLength

getKey

public short[] getKey()
Returns:
the key

getKeysum

public int getKeysum()
return the sum of the keys. Normally this is computed using the function @see de.folt.fuzzy.FuzzyNodeKey#computeKeySum()

Returns:
the keysum

getNGrams

public int getNGrams()
Returns:
the nGrams

setFuzzyBaseCharNumber

public void setFuzzyBaseCharNumber(long fuzzybase_charnum)
Parameters:
fuzzybase_charnum - the fUZZYBASE_CHARNUM to set

setFuzzyKeyLength

public void setFuzzyKeyLength(int maxFuzzyKeyLength)
This sets the length of the FuzzyKey. The Fuzzy key "key" is an array of shorts.

Parameters:
maxFuzzyKeyLength - the maxFuzzyKeyLength to set

setKey

public void setKey(short[] key)
Parameters:
key - the key to set

setKeysum

public void setKeysum(int keysum)
Parameters:
keysum - the kEYSUM to set

setNGrams

public void setNGrams(int grams)
Parameters:
grams - the nGrams to set