// JcfDemo.java import java.util.*; import java.io.*; @SuppressWarnings("unchecked") public class JcfDemo { public static HashSet loadDictionary(String path) throws Exception { HashSet d = new HashSet(); Scanner scanner = new Scanner(new File(path)); while (scanner.hasNext()) { String s = scanner.next(); d.add(s); } return d; } public static boolean checkSpelling(String s, HashSet d) { return d.contains(s); } public static String getWord(String s) { StringBuffer sb = new StringBuffer(); for (int i=0; i= 'a') && (c <= 'z')) sb.append(c); else if ((c >= 'A') && (c <= 'Z')) sb.append((char)(c - 'A' + 'a')); } return sb.toString(); } private static HashMap alternatesHashMap = null; public static void loadAlternatesHashMap() { HashMap hm = new HashMap(); hm.put("teh", "the"); hm.put("fooo", "dog"); alternatesHashMap = hm; } public static String getSuggestedAlternative(String s) { if (alternatesHashMap == null) loadAlternatesHashMap(); return (String) alternatesHashMap.get(s); } public static void main(String[] args) throws Exception { HashSet d = loadDictionary("sortedDictionary.txt"); System.out.println(d.size()); String text = "This is teh test to seeee if this fooo foooo workz."; Scanner scanner = new Scanner(text); while (scanner.hasNext()) { String s = scanner.next(); s = getWord(s); String alt = getSuggestedAlternative(s); if (alt != null) { System.out.println("Changing " + s + " to " + alt); s = alt; } if (!checkSpelling(s,d)) System.out.println("Misspelled: " + s); } } }