Tuesday, July 6, 2010

Remembering Numbers with Mnemonics

If you are someone who could easily forget numbers then here is some good news for you. You could use the system below to remember numbers. For instance to remember that Columbus discovered america in 1492. You could easily remember that it was discovered in the last millenium and you would aid to remember 492 alone. How easy is it to remember "Columbus FouND america". Piece of cake. huh...? F=4, N=9, D=2. The catch is that you need to remember the table below, but there is aid to remember the table. The table below maps each number to two consonants.

0 Z R ZeRo
1 B C First two consonants
2 D W Duo tWo
3 T J Three, only omitted consonant (other than Y)
4 F Q Four, Quad
5 L V Roman L (50), V(5)
6 S X SiX
7 P M sePteM
8 H K Huiti (HongKong)
9 N G Nine, 9 resembles g

Big Oh: Exponential

1 72
2 864
3 10368
4 124416
5 1492992
6 17915904
7 214990848
8 2579890176
9 30958682112
10 3.71504E+11
11 4.45805E+12

Reference: Boost Your Memory: 52 Brilliant Ideas You Won't Forget by Darren Bridger Infinite Ideas © 2008

Jazzy - The Java Open Source Spell Checker


A java program to achieve this

Writing this program made me think about
a) Cartesian product
b) HashSet, TreeSet
c) Generating cartesian product
d) Research about Java spell checker - Jazzy
e) Create a utility to do spell check (using Jazzy)

Note: Program below needs further tweaking in terms of performance. You are welcome to tweak and post it back to me.

Algorithm:
Step 1: Get input (Say 492)
Step 2: Generate Lists of lists [[A, E, I, O, U, ], [F, Q], [A, E, I, O, U, ], [N, G], [A, E, I, O, U, ], [D, W], [A, E, I, O, U, ]]
Vowels intertwined with number's consonant-mnemonic
Step 3: Generate cartesian product
Step 4: Check in dictionary and output only which is in dictionary
Step 5: FONDA,FUNDI,FINED,FEND,FIND,FOND,FUND


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;

class SuggestionListener implements SpellCheckListener {

private boolean wordFound = true;

public boolean isWordFound() {
return wordFound;
}

public void setWordFound(boolean wordFound) {
this.wordFound = wordFound;
}

public void spellingError(SpellCheckEvent event) {
wordFound = false;
}

}

/**
*
* Not thread save, because of suggestionListener's wordFound instance variable
* usage. Haven't thought about a better way.
*
*/
class SpellCheckUtility {
SpellDictionary dictionary = null;
SpellChecker spellChecker = null;
SuggestionListener suggestionListener = null;

public SpellCheckUtility() {
try {
dictionary = new SpellDictionaryHashMap(new File(
"C:/learn/english_dic/eng_com.dic"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spellChecker = new SpellChecker(dictionary);
suggestionListener = new SuggestionListener();
spellChecker.addSpellCheckListener(suggestionListener);
}

boolean checkValidSpelling(String stringToCheck) {
if (stringToCheck == null) {
return false;
}
StringWordTokenizer stringWordTokenizer = new StringWordTokenizer(
stringToCheck);
spellChecker.checkSpelling(stringWordTokenizer);
boolean result = suggestionListener.isWordFound();
suggestionListener.setWordFound(true);
return result;
}
}

class Word {

public Word() {
}

public Word(String w) {
this(w, false);
}

public Word(String w, boolean inDict) {
setWord(w);
setInDictionary(inDict);
}

public String getWord() {
return word;
}

public void setWord(String word) {
this.word = word;
}

public boolean isInDictionary() {
return inDictionary;
}

public void setInDictionary(boolean inDictionary) {
this.inDictionary = inDictionary;
}

private String word;
private boolean inDictionary;

}

public class MnemonicGenerator {

final static Map<string, list=""><string>> numberToCharsMap = new HashMap<string, list=""><string>>();
final static List<string> vowelList = new ArrayList<string>();
static {
init();
}

static void init() {
List<string> zeroList = new ArrayList<string>();
zeroList.add("Z");
zeroList.add("R");
numberToCharsMap.put("0", zeroList);
List<string> oneList = new ArrayList<string>();
oneList.add("B");
oneList.add("C");
numberToCharsMap.put("1", oneList);
List<string> twoList = new ArrayList<string>();
twoList.add("D");
twoList.add("W");
numberToCharsMap.put("2", twoList);
List<string> threeList = new ArrayList<string>();
threeList.add("T");
threeList.add("J");
numberToCharsMap.put("3", threeList);
List<string> fourList = new ArrayList<string>();
fourList.add("F");
fourList.add("Q");
numberToCharsMap.put("4", fourList);
List<string> fiveList = new ArrayList<string>();
fiveList.add("L");
fiveList.add("V");
numberToCharsMap.put("5", fiveList);
List<string> sixList = new ArrayList<string>();
sixList.add("S");
sixList.add("X");
numberToCharsMap.put("6", sixList);
List<string> sevenList = new ArrayList<string>();
sevenList.add("P");
sevenList.add("M");
numberToCharsMap.put("7", sevenList);
List<string> eightList = new ArrayList<string>();
eightList.add("H");
eightList.add("K");
numberToCharsMap.put("8", eightList);
List<string> nineList = new ArrayList<string>();
nineList.add("N");
nineList.add("G");
numberToCharsMap.put("9", nineList);

vowelList.add("A");
vowelList.add("E");
vowelList.add("I");
vowelList.add("O");
vowelList.add("U");
vowelList.add("");

}

List<word> getWords(int iNumber) {
return getWords(iNumber, 1);
}

List<word> getWords(int iNumber, int noOfVowelsSandwiched) {
SpellCheckUtility checkUtility = new SpellCheckUtility();
List<word> wordList = new ArrayList<word>();
String number = "" + iNumber;

ArrayList<list><string>> listsOfLists = new ArrayList<list><string>>();
for (int i = 0; i < length =" number.length();" i =" 0;" currentnumber = "" j =" 0;" arrlength =" listsOfLists.size();" maxlengtharray =" new" i =" 0;"><string>> iterator = listsOfLists.iterator(); iterator
.hasNext(); i++) {
List<string> list = (List<string>) iterator.next();
maxLengthArray[i] = list.size();
}

int[] counterArray = new int[arrLength];
do {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < currentwordstr =" sb.toString();" currindex =" 0;" currindex ="="" generator =" new"> words = generator.getWords(492, 1);

System.out.println(words.size());
int i = 0;
for (Iterator<word> iterator = words.iterator(); iterator.hasNext();) {
Word word = (Word) iterator.next();
if (word.isInDictionary()) {
i++;
System.out.println(word.getWord());
}

}
System.out.println("total=" + i);

}

public static void main2(String[] args) {
String s1 = "doctor";
String s2 = "decter";
SpellDictionary dictionary = null;
try {
dictionary = new SpellDictionaryHashMap(new File(
"C:/learn/english_dic/eng_com.dic"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SpellChecker spellChecker = new SpellChecker(dictionary);
SuggestionListener suggestionListener = new SuggestionListener();
spellChecker.addSpellCheckListener(suggestionListener);
spellChecker.checkSpelling(new StringWordTokenizer(s1));
if (!suggestionListener.isWordFound()) {
System.out.println("Word not found " + s1);
} else {
System.out.println("Word found " + s1);
}

}

public static void main3(String[] args) {
SpellCheckUtility checkUtility = new SpellCheckUtility();
System.out.println(checkUtility.checkValidSpelling("UWKCC"));
}

}

0 comments:

Post a Comment