Title | Programming Assignment 5 |
---|---|
Author | aisha mohamed |
Course | Programming 2 |
Institution | University of the People |
Pages | 8 |
File Size | 521.2 KB |
File Type | |
Total Downloads | 43 |
Total Views | 153 |
programming assignment...
Programming Assignment 5 CS 1103 Mr. Shadi University of the People
import java.io.File; import java.io.FileNotFoundException; import java.util.HashSet; import java.util.Scanner; import java.util.TreeSet;
import javax.swing.JFileChooser;
//Checking the Spelling with Correction
public class SpellChecker {
(CodeJava, 2021) (GeeksforGeeks, 2021)(StuCode, 2021)(JavaPoint, 2021)
public static void main(String[] args) { Scanner filein; HashSet dict = new HashSet(); Scanner in; try { filein = new Scanner(new File("C:\\\\Users\\\\user\\\\Documents\\\\Code Unit 5-20191217\\\\dictionary"));
while (filein.hasNext()) { String word = filein.next(); dict.add(word.toLowerCase()); Page 2 of 8
}
System.out.println("Size of HashSet : " + dict.size());
in = new Scanner(getInputFileNameFromUser()); // Files Characters
in.useDelimiter("[^a-zA-Z]+");
HashSet badWords = new HashSet();
(CodeJava, 2021) (GeeksforGeeks, 2021)(StuCode, 2021)(JavaPoint, 2021)
while (in.hasNext()) { String userWord = in.next(); userWord = userWord.toLowerCase(); if (!dict.contains(userWord) && !badWords.contains(userWord)) {
badWords.add(userWord); TreeSet goodWords = new TreeSet(); goodWords = corrections(userWord, dict); System.out.print(userWord + ": "); Page 3 of 8
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");
(CodeJava, 2021) (GeeksforGeeks, 2021)(StuCode, 2021)(JavaPoint, 2021)
count++; } } } } }
catch (FileNotFoundException e) { System.exit(0); } Page 4 of 8
}
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();
}
(CodeJava, 2021) (GeeksforGeeks, 2021)(StuCode, 2021)(JavaPoint, 2021)
static TreeSet corrections(String badWord, HashSet dictionary) { TreeSet possibleWords = new TreeSet();
String subStr1, subStr2, possibility;
Page 5 of 8
for (int i = 0; i < badWord.length(); i++) { subStr1 = badWord.substring(0, i); subStr2 = badWord.substring(i + 1); possibility = subStr1 + subStr2; if (dictionary.contains(possibility)) possibleWords.add(possibility); for (char ch = 'a'; ch...