0% found this document useful (0 votes)
199 views5 pages

CS 1103 Assignment Unit 5

This document contains the code for a basic spell-checker program in Java. The program loads words from a dictionary text file, reads words from a user-selected input file, and checks each word against the dictionary to find spelling errors. For each misspelled word, it generates a set of possible corrections by making single-character edits to the word and displays them to the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views5 pages

CS 1103 Assignment Unit 5

This document contains the code for a basic spell-checker program in Java. The program loads words from a dictionary text file, reads words from a user-selected input file, and checks each word against the dictionary to find spelling errors. For each misspelled word, it generates a set of possible corrections by making single-character edits to the word and displays them to the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.io.

*;

import java.util.Scanner;

import java.util.HashSet;

import javax.swing.*;

import java.util.TreeSet;

/*

* This class works as a basic spell-checker. It uses the file words.txt to

* check whether a given word is correctly spelled.

*/

public class SpellChecker {

public static void main(String[] args) {

Scanner words;

HashSet<String> dict = new HashSet<String>();

Scanner userFile;

try {

words = new Scanner(new File("words.txt"));

while (words.hasNext()) {

String word = words.next();

dict.add(word.toLowerCase());

userFile = new Scanner(getInputFileNameFromUser());

userFile.useDelimiter("[^a-zA-Z]+");
HashSet<String> badWords = new HashSet<String>();

while (userFile.hasNext()) {

String userWord = userFile.next();

userWord = userWord.toLowerCase();

if (!dict.contains(userWord) &&

!badWords.contains(userWord)) {

badWords.add(userWord);

TreeSet<String> goodWords = new TreeSet<String>();

goodWords = corrections(userWord, dict);

System.out.print(userWord + ": ");

if (goodWords.isEmpty())

System.out.println("(no suggestions)");

else {

int count = 0;

for (String goodWord: goodWords) {

System.out.print(goodWord);

if (count < goodWords.size() - 1)

System.out.print(", ");

else

System.out.print("\n");

count++;

}
}

catch (FileNotFoundException e) {

System.exit(0);

static File getInputFileNameFromUser() {

JFileChooser fileDialog = new JFileChooser();

fileDialog.setDialogTitle("Select File for Input");

int option = fileDialog.showOpenDialog(null);

if (option != JFileChooser.APPROVE_OPTION)

return null;

else

return fileDialog.getSelectedFile();

static TreeSet<String> corrections(String badWord, HashSet<String> dictionary)


{

TreeSet<String> possibleWords = new TreeSet<String>();

String subStr1, subStr2, possibility;

for (int i = 0; i < badWord.length(); i++) {

// Remove character i from the word.

subStr1 = badWord.substring(0, i);

subStr2 = badWord.substring(i + 1);


// Delete any one of the letters from the misspelled word.

possibility = subStr1 + subStr2;

if (dictionary.contains(possibility))

possibleWords.add(possibility);

for (char ch = 'a'; ch <= 'z'; ch++) {

possibility = subStr1 + ch + subStr2;

if (dictionary.contains(possibility))

possibleWords.add(possibility);

// Divide the word into two substrings.

subStr1 = badWord.substring(0, i);

subStr2 = badWord.substring(i);

// Insert any letter at any point in the misspelled word.

for (char ch = 'a'; ch <= 'z'; ch++) {

possibility = subStr1 + ch + subStr2;

if (dictionary.contains(possibility))

possibleWords.add(possibility);

char ch = ' ';

possibility = subStr1 + ch + subStr2;

if (dictionary.contains(subStr1) && dictionary.contains(subStr2))

possibleWords.add(possibility);

}
for (int i = 1; i < badWord.length(); i++) {

subStr1 = badWord.substring(0, i - 1);

char ch1 = badWord.charAt(i - 1);

char ch2 = badWord.charAt(i);

subStr2 = badWord.substring(i + 1);

possibility = subStr1 + ch2 + ch1 + subStr2;

if (dictionary.contains(possibility))

possibleWords.add(possibility);

return possibleWords;

You might also like