/** * SpellChecker.java * Jeff Ondich, Carleton College, 2014-01-01 * * A program that tests out the Dictionary interface and one of its * implementations. * * This implementation is part of an introduction to Java interfaces in * Carleton's CS 201 Data Structures course. */ import java.io.File; import java.util.Scanner; import java.util.ArrayList; import java.io.FileNotFoundException; public class SpellChecker { /** * Splits the specified string into words, discarding punctuation and spacing. * @return an ArrayList of the resulting words * @param s the string to be split */ public static ArrayList getWordsFromString(String s) { ArrayList wordList = new ArrayList(); String word = ""; for (int k = 0; k < s.length(); k++) { char ch = s.charAt(k); if (Character.isLetter(ch) || ch == '\'') { word += ch; } else if (word.length() > 0) { wordList.add(word); word = ""; } } // If s ends with a word, then we haven't added it yet. if (word.length() > 0) { wordList.add(word); } return wordList; } /** * A "factory method" that instantiates whichever implementation of * the Dictionary interface is specified in the dictionaryType parameter. * For our purposes, the legal dictionary types are as listed in the * source code below: "linearsearch", "binarysearch", "treemap", and * "hashmap". If dictionaryType is any other value, then this method * returns a LinearSearchDictionary object. */ public static WordGameDictionary createDictionary(String dictionaryType) { if (dictionaryType.equals("linearsearch")) { return new LinearSearchDictionary(); } else if (dictionaryType.equals("binarysearch")) { return new BinarySearchDictionary(); } else if (dictionaryType.equals("treemap")) { return new TreeMapDictionary(); } else if (dictionaryType.equals("hashmap")) { return new HashMapDictionary(); } else { return new LinearSearchDictionary(); } } public static void main(String[] args) { // Check the command-line syntax if (args.length != 3) { System.err.println("Usage: java SpellChecker dictionaryType dictionaryFile fileToCheck"); System.err.println(" legal dictionaryType values: linearsearch, binarysearch, treemap, hashmap"); System.err.println(" dictionaryFile should contain one word per line"); System.exit(1); } File inputFile = null; Scanner scanner = null; String dictionaryType = args[0]; String dictionaryFile = args[1]; String fileToCheck = args[2]; // Create a Dictionary of your choosing. WordGameDictionary dictionary = createDictionary(dictionaryType); // Load data from the dictionary file into your dictionary. inputFile = new File(dictionaryFile); try { scanner = new Scanner(inputFile); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } System.err.println("Loading dictionary..."); int wordCount = 0; while (scanner.hasNextLine()) { String word = scanner.nextLine(); dictionary.addWord(word); //wordCount++; //if (wordCount % 10000 == 0) { // System.err.format("%d words loaded (most recent: %s)\n", wordCount, word); //} } scanner.close(); // Read through the file to be checked, and print out all // words, with line numbers, that are not found in the dictionary. inputFile = new File(fileToCheck); try { scanner = new Scanner(inputFile); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } int lineNumber = 1; System.err.format("Checking spelling for %s.\n\n", fileToCheck); System.err.println("=== Misspelled words ==="); long startTime = System.currentTimeMillis(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); ArrayList wordsInLine = getWordsFromString(line); for (String word : wordsInLine) { if (!dictionary.hasWord(word)) { System.out.format("[%d] %s\n", lineNumber, word); } } lineNumber++; } scanner.close(); System.out.format("Total search time: %d ms\n", System.currentTimeMillis() - startTime); } }