-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpellingChecker.java
75 lines (67 loc) · 2.66 KB
/
SpellingChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;
/*
author: Ashish Waykar
*/
public class SpellingChecker {
// A dictionary of correctly spelled words
private static Set<String> dictionary = new HashSet<>();
public static void main(String[] args) {
// Load the dictionary with words from a file or database
// For this example, we'll just use a hardcoded set of words
dictionary.add("ashish");
dictionary.add("niraj");
dictionary.add("Duba_i");
dictionary.add("abcd");
dictionary.add("Abhijeet");
// Get user input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word to check: ");
String word = scanner.nextLine().toLowerCase();
// Check if the word is in the dictionary
if (dictionary.contains(word)) {
System.out.println("The word \"" + word + "\" is spelled correctly.");
} else {
// If not, suggest corrections
System.out.println("The word \"" + word + "\" is spelled incorrectly.");
List<String> suggestions = suggestCorrections(word);
if (suggestions.isEmpty()) {
System.out.println("No suggestions found.");
} else {
System.out.println("Suggestions:");
for (String suggestion : suggestions) {
System.out.println(suggestion);
}
}
}
}
// Returns a list of suggested corrections for a misspelled word
private static List<String> suggestCorrections(String word) {
List<String> suggestions = new ArrayList<>();
for (String candidate : dictionary) {
if (distance(word, candidate) <= 2) {
suggestions.add(candidate);
}
}
Collections.sort(suggestions);
return suggestions;
}
// Computes the Levenshtein distance between two strings
private static int distance(String s1, String s2) {
int[] prevRow = new int[s2.length() + 1];
for (int j = 0; j < prevRow.length; j++) {
prevRow[j] = j;
}
for (int i = 1; i <= s1.length(); i++) {
int[] currRow = new int[s2.length() + 1];
currRow[0] = i;
for (int j = 1; j <= s2.length(); j++) {
int insertion = prevRow[j] + 1;
int deletion = currRow[j - 1] + 1;
int substitution = prevRow[j - 1] + (s1.charAt(i - 1) == s2.charAt(j - 1) ? 0 : 1);
currRow[j] = Math.min(Math.min(insertion, deletion), substitution);
}
prevRow = currRow;
}
return prevRow[s2.length()];
}
}