Created
July 5, 2014 21:55
-
-
Save anonymous/e348f22cf213736623a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.io.File; | |
import java.util.ArrayList; | |
public class SpellCheck | |
{ | |
private ArrayList<String> dict = new ArrayList<>(); | |
private ArrayList<String> sentence = new ArrayList<>(); | |
private String input; | |
private char[] A = new char[] {'k','l','s','d'}; | |
private char[] B = new char[] {'c','v','n','m'}; | |
private char[] C = new char[] {'z','x','v','b'}; | |
private char[] D = new char[] {'a','s','f','g'}; | |
private char[] E = new char[] {'q','w','r','t'}; | |
private char[] F = new char[] {'s','d','g','h'}; | |
private char[] G = new char[] {'d','f','h','j'}; | |
private char[] H = new char[] {'f','g','j','k'}; | |
private char[] I = new char[] {'y','u','o','p'}; | |
private char[] J = new char[] {'g','h','k','l'}; | |
private char[] K = new char[] {'h','j','l','a'}; | |
private char[] L = new char[] {'j','k','a','s'}; | |
private char[] M = new char[] {'b','n','z','x'}; | |
private char[] N = new char[] {'v','b','m','z'}; | |
private char[] O = new char[] {'u','i','p','q'}; | |
private char[] P = new char[] {'i','o','q','w'}; | |
private char[] Q = new char[] {'o','p','w','e'}; | |
private char[] R = new char[] {'w','e','t','y'}; | |
private char[] S = new char[] {'l','a','d','f'}; | |
private char[] T = new char[] {'e','r','y','u'}; | |
private char[] U = new char[] {'t','y','i','o'}; | |
private char[] V = new char[] {'x','c','b','n'}; | |
private char[] W = new char[] {'p','q','e','r'}; | |
private char[] X = new char[] {'m','z','c','v'}; | |
private char[] Y = new char[] {'r','t','u','i'}; | |
private char[] Z = new char[] {'n','m','x','c'}; | |
public SpellCheck() | |
{ | |
readDictionary(); | |
askInput(); | |
checkWords(); | |
printSentence(); | |
} | |
private void printSentence() | |
{ | |
for (String word : sentence) | |
System.out.print(word + " "); | |
System.out.println(); | |
} | |
private void askInput() | |
{ | |
Scanner in = new Scanner(System.in); | |
input = in.nextLine(); | |
input = input.toLowerCase(); | |
for (String word : input.split(" ")) | |
{ | |
sentence.add(word); | |
} | |
} | |
private void checkWords() | |
{ | |
for (String word : sentence) | |
{ | |
if (!dict.contains(word)) | |
{ | |
fixWord(word); | |
} | |
} | |
} | |
private void fixWord(String word) | |
{ | |
for (int sp = 0; sp < 4; ++sp) | |
{ | |
String newWord = new String(); | |
for (int spot = 0; spot < word.length(); ++spot) | |
{ | |
if (word.charAt(spot) == 'a') | |
{ | |
newWord += A[sp]; | |
} | |
else if (word.charAt(spot) == 'b') | |
{ | |
newWord += B[sp]; | |
} | |
else if (word.charAt(spot) == 'c') | |
{ | |
newWord += C[sp]; | |
} | |
else if (word.charAt(spot) == 'd') | |
{ | |
newWord += D[sp]; | |
} | |
else if (word.charAt(spot) == 'e') | |
{ | |
newWord += E[sp]; | |
} | |
else if (word.charAt(spot) == 'f') | |
{ | |
newWord += F[sp]; | |
} | |
else if (word.charAt(spot) == 'g') | |
{ | |
newWord += G[sp]; | |
} | |
else if (word.charAt(spot) == 'h') | |
{ | |
newWord += H[sp]; | |
} | |
else if (word.charAt(spot) == 'i') | |
{ | |
newWord += I[sp]; | |
} | |
else if (word.charAt(spot) == 'j') | |
{ | |
newWord += J[sp]; | |
} | |
else if (word.charAt(spot) == 'k') | |
{ | |
newWord += K[sp]; | |
} | |
else if (word.charAt(spot) == 'l') | |
{ | |
newWord += L[sp]; | |
} | |
else if (word.charAt(spot) == 'm') | |
{ | |
newWord += M[sp]; | |
} | |
else if (word.charAt(spot) == 'n') | |
{ | |
newWord += N[sp]; | |
} | |
else if (word.charAt(spot) == 'o') | |
{ | |
newWord += O[sp]; | |
} | |
else if (word.charAt(spot) == 'p') | |
{ | |
newWord += P[sp]; | |
} | |
else if (word.charAt(spot) == 'q') | |
{ | |
newWord += Q[sp]; | |
} | |
else if (word.charAt(spot) == 'r') | |
{ | |
newWord += R[sp]; | |
} | |
else if (word.charAt(spot) == 's') | |
{ | |
newWord += S[sp]; | |
} | |
else if (word.charAt(spot) == 't') | |
{ | |
newWord += T[sp]; | |
} | |
else if (word.charAt(spot) == 'u') | |
{ | |
newWord += U[sp]; | |
} | |
else if (word.charAt(spot) == 'v') | |
{ | |
newWord += V[sp]; | |
} | |
else if (word.charAt(spot) == 'w') | |
{ | |
newWord += W[sp]; | |
} | |
else if (word.charAt(spot) == 'x') | |
{ | |
newWord += X[sp]; | |
} | |
else if (word.charAt(spot) == 'y') | |
{ | |
newWord += Y[sp]; | |
} | |
else if (word.charAt(spot) == 'z') | |
{ | |
newWord += Z[sp]; | |
} | |
} | |
if (dict.contains(newWord)) | |
{ | |
for (int spot = 0; spot < sentence.size(); ++spot) | |
{ | |
if (sentence.get(spot).equals(word)) | |
{ | |
sentence.set(spot, newWord); | |
} | |
} | |
break; | |
} | |
} | |
} | |
private void readDictionary() | |
{ | |
Scanner reader; | |
try | |
{ | |
reader = new Scanner(new File("enable1.txt")); | |
while (reader.hasNext()) | |
{ | |
dict.add(reader.next()); | |
} | |
} | |
catch (Exception e){}; | |
} | |
public static void main(String[] args) | |
{ | |
new SpellCheck(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment