Friday, July 13, 2012

The Unscrambler (Anagrams)

When i was working for a gaming company, there used to be quarterly competitions conducted online. one of them was unscramble. A jumbled letters will be given and you have to find anagrams. I implemented this programatically. My implemention will give results in short time for 7 words, will take a lot of time for words more than that.

I used brute force method. I construct a master word from which small words will be taken and looked up in a dictionary file (dict.txt). There are 3 files. The output for "estma" is
Enter Letters : estma
1 : steam
2 : mates
Finish

The reason why i can't find teams, meats etc.. is, the dict.txt have only singulars for those words.

UnScrambler.java
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class UnScrambler { private static String word; private static int p = 0; static void getWord() throws IOException { BufferedReader d = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Letters : "); word = d.readLine(); } static StringBuffer genMasterWord() { StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = new StringBuffer(); StringBuffer sb3 = new StringBuffer(); char a[] = word.toLowerCase().toCharArray(); for (int i = 0; i < a.length; i++) { for (int j = i; j >= 0; j--) { for (int o = j, l = fact(i); l - 1 >= 0; l--, o = o + i + 1) { sb1 = sb1.insert(o, a[i]); } for (; p == 0; p--) { sb1 = sb1.insert(p, a[i]); } sb2 = sb2.append(sb1); sb1 = new StringBuffer(); sb1 = sb1.append(sb3); } sb3 = sb2; sb2 = new StringBuffer(); sb1 = new StringBuffer(); sb1 = sb1.append(sb3); } return sb3; } public static List<String> formWord() { StringBuffer b = genMasterWord(); List<String> li = new ArrayList<String>(); String a = b.toString(); for (int j = 0; j < a.length(); j = j + word.length()) // System.out.println((i++)+" : "+a.substring(j,j+word.length())); li.add(a.substring(j, j + word.length())); return li; } public static int fact(int i) { int r; if (i == 1) return 1; else if (i == 0) return 0; r = fact(i - 1) * i; return r; } public static List<String> loadDict() throws IOException { String e; int i = 0; List<String> dic = new ArrayList<String>(); FileInputStream fi = new FileInputStream("dict.txt"); BufferedReader d = new BufferedReader(new InputStreamReader(fi)); while ((e = d.readLine()) != null) { i++; dic.add(e.toLowerCase()); } return dic; } }

TestUS.java
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class TestUS { public static void main(String[] args) throws IOException { List<String> word = new ArrayList<String>(); List<String> dic = new ArrayList<String>(); List<String> result = new ArrayList<String>(); UnScrambler.getWord(); word = UnScrambler.formWord(); ListIterator<String> wli = word.listIterator(); dic = UnScrambler.loadDict(); int k = 1; while (wli.hasNext()) { String a = wli.next(); if (dic.contains(a)) { if (!result.contains(a)) { result.add(a); System.out.println((k++) + " : " + a); } } } System.out.println("Finish"); } }

dict.txt
I found the dictionary file in C:\Program Files (x86)\Mozilla Firefox\dictionaries. I removed some of the unwanted chars and words from this file programmatically. download here

No comments:

Post a Comment