pt.tumba.spell
Class KeyboardProximity

java.lang.Object
  extended by pt.tumba.spell.KeyboardProximity

public class KeyboardProximity
extends java.lang.Object

Implementation of String similarity measures with basis on Keyboard proximity.

The keyboard is assumed to have a QWERTY layout, and we also make a distinction between keys on the same row of the keyboard (which therefore should be "more similar") and adjacent keys.

A large percentage of spelling mistakes are typographical errors, mainly due to keyboard typing slips (e.g. Jack becomes Hack since h and j are next to each other on the keyboard). Handling these errors is therefore of special importance.

Author:
Bruno Martins

Field Summary
private static java.util.Map firstChars
          Associates keys with a Map of adjacent keys in the same row of the keyboard.
private static java.util.Map secondChars
          Associates keys with a Map of adjacent keys but at diferent rows of the keyboard.
private static KeyboardProximity theInstance
          The single instance of this class.
 
Constructor Summary
private KeyboardProximity()
          Sole constructor, private because this is a Singleton class.
 
Method Summary
static KeyboardProximity getInstance()
          Return the single instance of this class.
static boolean isNear(char ch1, char ch2)
          Check if two characters are adjacent in the keyboard.
static boolean isSameRow(char ch1, char ch2)
          Check if two characters are adjacent in the same row of the keyboard.
static int similarityStringsNear(java.lang.String s1, java.lang.String s2)
          Returns the similarity between two String objects in terms of the number of different characters that are adjacent in the keyboard.
static int similarityStringsSameRow(java.lang.String s1, java.lang.String s2)
          Returns the similarity between two String objects in terms of the number of different characters that are adjacent in the same row of the keyboard.
static boolean stringsNear(java.lang.String s1, java.lang.String s2)
          Check if two String objects differ in only one character adjacent in the keyboard.
static boolean stringsNear(java.lang.String s1, java.lang.String s2, int n)
          Check if two String objects differ in only a given number of characters adjacent in the keyboard.
static boolean stringsSameRow(java.lang.String s1, java.lang.String s2)
          Check if two String objects differ in only one character adjacent in the same row of the keyboard.
static boolean stringsSameRow(java.lang.String s1, java.lang.String s2, int n)
          Check if two String objects differ in only a given number of characters adjacent in the same row of the keyboard.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

firstChars

private static java.util.Map firstChars
Associates keys with a Map of adjacent keys in the same row of the keyboard.


secondChars

private static java.util.Map secondChars
Associates keys with a Map of adjacent keys but at diferent rows of the keyboard.


theInstance

private static final KeyboardProximity theInstance
The single instance of this class.

Constructor Detail

KeyboardProximity

private KeyboardProximity()
Sole constructor, private because this is a Singleton class.

Method Detail

getInstance

public static KeyboardProximity getInstance()
Return the single instance of this class.

Returns:
An instance of KeyboardProximity.

isSameRow

public static boolean isSameRow(char ch1,
                                char ch2)
Check if two characters are adjacent in the same row of the keyboard.

Parameters:
ch1 - A character.
ch2 - Another character.
Returns:
true if the characters are adjacent in the same row of the keyboard and false otherwise.

isNear

public static boolean isNear(char ch1,
                             char ch2)
Check if two characters are adjacent in the keyboard.

Parameters:
ch1 - A character.
ch2 - Another character.
Returns:
true if the characters are adjacent in the keyboard and false otherwise.

stringsSameRow

public static boolean stringsSameRow(java.lang.String s1,
                                     java.lang.String s2,
                                     int n)
Check if two String objects differ in only a given number of characters adjacent in the same row of the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
n - the number of diferent characters.
Returns:
true if the strings differ in only a given number of characters adjacent in the same row of the keyboard and false otherwise.

stringsSameRow

public static boolean stringsSameRow(java.lang.String s1,
                                     java.lang.String s2)
Check if two String objects differ in only one character adjacent in the same row of the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
Returns:
true if the strings differ in only one character adjacent in the same row of the keyboard and false otherwise.

stringsNear

public static boolean stringsNear(java.lang.String s1,
                                  java.lang.String s2,
                                  int n)
Check if two String objects differ in only a given number of characters adjacent in the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
n - the maximum number of diferent characters.
Returns:
true if the strings differ in only a given number of characters adjacent in the keyboard and false otherwise.

stringsNear

public static boolean stringsNear(java.lang.String s1,
                                  java.lang.String s2)
Check if two String objects differ in only one character adjacent in the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
Returns:
true if the strings differ in only one character adjacent in the keyboard and false otherwise.

similarityStringsNear

public static int similarityStringsNear(java.lang.String s1,
                                        java.lang.String s2)
Returns the similarity between two String objects in terms of the number of different characters that are adjacent in the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
Returns:
-1 if the strings differ in more than adjacent characters, 0 if the strings are equal and the number of different characters otherwise.

similarityStringsSameRow

public static int similarityStringsSameRow(java.lang.String s1,
                                           java.lang.String s2)
Returns the similarity between two String objects in terms of the number of different characters that are adjacent in the same row of the keyboard.

Parameters:
s1 - A String.
s2 - Another String.
Returns:
-1 if the strings differ in more than adjacent characters, 0 if the strings are equal and the number of different characters otherwise.