This little program was written for an assignment in a data structures and algorithms class. I'll just note the basic requirements:
Given an inputted string, the program should check to see if it exists in a dictionary of correctly spelled words. If not, it should return a list of words that are obtainable by:
- adding any character to the beginning or end of the inputted string
- removing any single character from the inputted string
- swapping any two adjacent characters in the string
The primary data structure is embodied in the Dictionary
class, which is my implementation of a separately-chaining hash list, and the important algorithms are found in the charAppended()
, charMissing()
and charsSwapped()
methods.
Everything works as expected - I'm just looking for tips about anything that can be done more cleanly, efficiently or better aligned with best practices.
SpellCheck.java
import java.util.ArrayList;
import java.util.Scanner;
public class SpellCheck {
private Dictionary dict;
final static String filePath = "d:/desktop/words.txt";
final static char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
SpellCheck() {
dict = new Dictionary();
dict.build(filePath);
}
void run() {
Scanner scan = new Scanner(System.in);
boolean done = false;
String input;
while (true) {
System.out.print("\n-------Enter a word: ");
input = scan.nextLine();
if (input.equals("")) {
break;
}
if (dict.contains(input)) {
System.out.println("\n" + input + " is spelled correctly");
} else {
System.out.print("is not spelled correctly, ");
System.out.println(printSuggestions(input));
}
}
}
String printSuggestions(String input) {
StringBuilder sb = new StringBuilder();
ArrayList<String> print = makeSuggestions(input);
if (print.size() == 0) {
return "and I have no idea what word you could mean.\n";
}
sb.append("perhaps you meant:\n");
for (String s : print) {
sb.append("\n -" + s);
}
return sb.toString();
}
private ArrayList<String> makeSuggestions(String input) {
ArrayList<String> toReturn = new ArrayList<>();
toReturn.addAll(charAppended(input));
toReturn.addAll(charMissing(input));
toReturn.addAll(charsSwapped(input));
return toReturn;
}
private ArrayList<String> charAppended(String input) {
ArrayList<String> toReturn = new ArrayList<>();
for (char c : alphabet) {
String atFront = c + input;
String atBack = input + c;
if (dict.contains(atFront)) {
toReturn.add(atFront);
}
if (dict.contains(atBack)) {
toReturn.add(atBack);
}
}
return toReturn;
}
private ArrayList<String> charMissing(String input) {
ArrayList<String> toReturn = new ArrayList<>();
int len = input.length() - 1;
//try removing char from the front
if (dict.contains(input.substring(1))) {
toReturn.add(input.substring(1));
}
for (int i = 1; i < len; i++) {
//try removing each char between (not including) the first and last
String working = input.substring(0, i);
working = working.concat(input.substring((i + 1), input.length()));
if (dict.contains(working)) {
toReturn.add(working);
}
}
if (dict.contains(input.substring(0, len))) {
toReturn.add(input.substring(0, len));
}
return toReturn;
}
private ArrayList<String> charsSwapped(String input) {
ArrayList<String> toReturn = new ArrayList<>();
for (int i = 0; i < input.length() - 1; i++) {
String working = input.substring(0, i);// System.out.println(" 0:" + working);
working = working + input.charAt(i + 1); //System.out.println(" 1:" + working);
working = working + input.charAt(i); //System.out.println(" 2:" + working);
working = working.concat(input.substring((i + 2)));//System.out.println(" FIN:" + working);
if (dict.contains(working)) {
toReturn.add(working);
}
}
return toReturn;
}
public static void main(String[] args) {
SpellCheck sc = new SpellCheck();
sc.run();
}
}
Dictionary.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Dictionary {
private int M = 1319; //prime number
final private Bucket[] array;
public Dictionary() {
this.M = M;
array = new Bucket[M];
for (int i = 0; i < M; i++) {
array[i] = new Bucket();
}
}
private int hash(String key) {
return (key.hashCode() & 0x7fffffff) % M;
}
//call hash() to decide which bucket to put it in, do it.
public void add(String key) {
array[hash(key)].put(key);
}
//call hash() to find what bucket it's in, get it from that bucket.
public boolean contains(String input) {
input = input.toLowerCase();
return array[hash(input)].get(input);
}
public void build(String filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//this method is used in my unit tests
public String[] getRandomEntries(int num){
String[] toRet = new String[num];
for (int i = 0; i < num; i++){
//pick a random bucket, go out a random number
Node n = array[(int)Math.random()*M].first;
int rand = (int)Math.random()*(int)Math.sqrt(num);
for(int j = 0; j<rand && n.next!= null; j++) n = n.next;
toRet[i]=n.word;
}
return toRet;
}
class Bucket {
private Node first;
public boolean get(String in) { //return key true if key exists
Node next = first;
while (next != null) {
if (next.word.equals(in)) {
return true;
}
next = next.next;
}
return false;
}
public void put(String key) {
for (Node curr = first; curr != null; curr = curr.next) {
if (key.equals(curr.word)) {
return; //search hit: return
}
}
first = new Node(key, first); //search miss: add new node
}
class Node {
String word;
Node next;
public Node(String key, Node next) {
this.word = key;
this.next = next;
}
}
}
}