0% found this document useful (0 votes)
31 views

Programming Assignment Unit 5 Java

The document describes a Java code for a spell checker program that reads words from a dictionary file, allows a user to select an input file, checks each word in the input file against the dictionary, and identifies misspelled words along with possible corrections. The main method calls methods to read the dictionary, get the input file, check each word, and return corrections by applying edit operations like deletion, insertion etc.

Uploaded by

Lin Myat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Programming Assignment Unit 5 Java

The document describes a Java code for a spell checker program that reads words from a dictionary file, allows a user to select an input file, checks each word in the input file against the dictionary, and identifies misspelled words along with possible corrections. The main method calls methods to read the dictionary, get the input file, check each word, and return corrections by applying edit operations like deletion, insertion etc.

Uploaded by

Lin Myat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Assignment Unit 5

My code is initiated by calling important classes and packages for file handling, scanning, and
collections. thhe (SpellChecker) class contains the main method where the spell checking
functionality is implemented. The main method first calls the (readDictionary) method, passing the
filename "words.txt", to read the words from the dictionary file and store them in a HashSet called
(dictionary).

It then prompts the user to select an input file using the getInputFileNameFromUser method, which
opens a file selection dialog and returns the selected file.

The selected file is then read using a Scanner, and non-letter characters are skipped by setting the
delimiter with in.useDelimiter("[^a-zA-Z]+");. This ensures that only words consisting of letters are
considered.

Each word from the input file is converted to lowercase and checked against the dictionary HashSet. If
the word is not found in the dictionary, it is added to the misspelledWords set.

My Java Code:
>>>import java.io.File;

>>>import java.io.FileNotFoundException;

>>>import java.util.HashSet;

>>>import java.util.Scanner;

>>>import java.util.Set;

>>>import java.util.TreeSet;

>>>import javax.swing.JFileChooser;

>>>public class SpellChecker {

>>> public static void main(String[] args) {

>>> HashSet<String> dictionary = readDictionary("words.txt");

>>> if (dictionary.isEmpty()) {

>>> System.out.println("Failed to read the dictionary.");

>>> return;

>>> }
>>> File inputFile = getInputFileNameFromUser();

>>> if (inputFile == null) {

>>> System.out.println("No file selected.");

>>> return;

>>> }

>>> Set<String> misspelledWords = new HashSet<>();

>>> try {

>>> Scanner fileScanner = new Scanner(inputFile);

>>> fileScanner.useDelimiter("[^a-zA-Z]+");

>>> while (fileScanner.hasNext()) {

>>> String word = fileScanner.next().toLowerCase();

>>> if (!dictionary.contains(word)) {

>>> misspelledWords.add(word);

>>> }

>>> }

>>> fileScanner.close();

>>> } catch (FileNotFoundException e) {

>>> System.out.println("File not found: " + e.getMessage());

>>> return;

>>> }

>>> for (String word : misspelledWords) {

>>> TreeSet<String> corrections = getCorrections(word, dictionary);

>>> if (corrections.isEmpty()) {

>>> System.out.println(word + ": (no suggestions)");

>>> } else {

>>> System.out.print(word + ": ");

>>> for (String correction : corrections) {

>>> System.out.print(correction + ", ");

>>> }
>>> System.out.println();

>>> }

>>> }

>>> }

>>> public static HashSet<String> readDictionary(String fileName) {

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

>>> try {

>>> File file = new File(fileName);

>>> Scanner fileScanner = new Scanner(file);

>>> while (fileScanner.hasNext()) {

>>> String word = fileScanner.next().toLowerCase();

>>> dictionary.add(word);

>>> }

>>> fileScanner.close();

>>> } catch (FileNotFoundException e) {

>>> System.out.println("Dictionary file not found: " + e.getMessage());

>>> }

>>> return dictionary;

>>> }

>>> public static TreeSet<String> getCorrections(String badWord, HashSet<String> dictionary) {

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

>>> // Delete, Change, Insert, Swap, Space Insertion operations on badWord

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

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

>>> String deleted = badWord.substring(0, i) + badWord.substring(i + 1);

>>> String changed = badWord.substring(0, i) + ch + badWord.substring(i + 1);

>>> String inserted = badWord.substring(0, i) + ch + badWord.substring(i);

>>> String swapped = swapCharacters(badWord, i);

>>> String withSpace = badWord.substring(0, i) + " " + badWord.substring(i);


>>> if (dictionary.contains(deleted)) {

>>> corrections.add(deleted);

>>> }

>>> if (dictionary.contains(changed)) {

>>> corrections.add(changed);

>>> }

>>> if (dictionary.contains(inserted)) {

>>> corrections.add(inserted);

>>> }

>>> if (dictionary.contains(swapped)) {

>>> corrections.add(swapped);

>>> }

>>> if (dictionary.contains(withSpace)) {

>>> corrections.add(withSpace);

>>> }

>>> }

>>> }

>>> return corrections;

>>> }

>>> public static String swapCharacters(String word, int index) {

>>> if (index >= word.length() - 1) {

>>> return word;

>>> }

>>> char[] characters = word.toCharArray();

>>> char temp = characters[index];

>>> characters[index] = characters[index + 1];

>>> characters[index + 1] = temp;

>>> return new String(characters);

>>> }
>>> public 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();

>>> }

>>> }

>>>}

You might also like