Package spell; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. util. arrayList; import java. util. arrays; import java. util. list; import java. util. listIterator; /*** 1. Create a List set to store the dictionary Set * 2. Create a List set to store the set to be checked * 3. Compare the set to be checked with the dictionary set, delete, replace, insert, and other operations to determine whether the word is a dictionary Set * Yes, if the output is XX is correct or not, compare the output, if the dictionary does not exist, the output is null * @ author AbuGe **/public class SpellChecker {public static void main (String [] args) throws IOException {// create a dictionary set and List
DictionaryList = new ArrayList
(); // Create an input dictionary List
InputList = new ArrayList
(); // Set # start and end mark boolean startFlag = false; boolean endFlag = false; // set a count mark int count = 0; // input a row of Data InputStream in = System. in; InputStreamReader isr = new InputStreamReader (in); BufferedReader bufr = new BufferedReader (isr); String line = null; while (! StartFlag |! EndFlag) {line = bufr. readLine (); if (line. equals ("#") {count ++;} if (count = 1 &&! StartFlag) {startFlag = true; continue;} if (count = 2) {endFlag = true; continue;} if (! StartFlag) {dictionaryList. add (line) ;}if (startFlag) {inputList. add (line) ;}// ListIterator of the List set to be checked
InputIterator = inputList. listIterator (); while (inputIterator. hasNext () {String input = inputIterator. next (); StringBuilder sb = new StringBuilder (); if (dictionaryList. contains (input) {System. out. println (input + "is correct");} else {ListIterator
DictionaryIterator = dictionaryList. listIterator (); char [] inputTemp = input. toCharArray (); sb. append (input); sb. append (":"); while (dictionaryIterator. hasNext () {String dictionary = dictionaryIterator. next (); char [] dictionaryTemp = dictionary. toCharArray (); boolean replaceFlag = replace (inputTemp, dictionaryTemp); boolean deleteFlag = delete (inputTemp, dictionaryTemp); boolean addFlag = add (inputTemp, dic TionaryTemp); if (replaceFlag | deleteFlag | addFlag) {sb. append (""); sb. append (dictionary) ;}} String result = sb. toString (); System. out. println (result) ;}}// 1. replace a public static boolean replace (char [] input, char [] dictionary) {// count different letter numbers int dif = 0; if (input. length = dictionary. length) {for (int I = 0, j = 0; I <input. length;) {if (input [I ++]! = Dictionary [j ++]) dif ++;} if (dif> 1) return false; return true ;}else {return false ;}} // 2. delete a public static boolean delete (char [] input, char [] dictionary) {int dif = 0; if (input. length-dictionary. length = 1) {for (int I = 0, j = 0; I <input. length & j <dictionary. length;) {if (input [I]! = Dictionary [j]) {I ++; dif ++; if (dif> 1) return false ;}else {I ++; j ++ ;}} return true;} else {return false;} // 3. add a public static boolean add (char [] input, char [] dictionary) {int dif = 0; if (dictionary. length-input. length = 1) {for (int I = 0, j = 0; j <dictionary. length & I <input. length;) {if (input [I]! = Dictionary [j]) {dif ++; j ++; if (dif> 1) {return false ;}} else {I ++; j ++ ;}} return true;} else {return false ;}}}