Created
December 20, 2023 11:50
-
-
Save whizyrel/1773b2c166ffef5285137b30482415b7 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
| package org.evryword; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.util.HashSet; | |
| import java.util.Scanner; | |
| import java.util.TreeSet; | |
| import javax.swing.*; | |
| public class Main { | |
| public static HashSet<String> setOfTokens = new HashSet<>(); | |
| public static void main(String[] args) { | |
| System.out.println("=== Starting Application ==="); | |
| final String pathToWordsFile = "src/main/resources/words.txt"; | |
| readWordsInFileIntoSet(pathToWordsFile); | |
| final File userFile = getInputFileNameFromUser(); | |
| if (userFile == null) { | |
| System.out.println("No file selected"); | |
| return; | |
| } | |
| readWordsInUserFile(userFile); | |
| System.out.println("=== Terminating Application ==="); | |
| } | |
| public static void readWordsInUserFile(File userFile) { | |
| try (final Scanner scanner = new Scanner(userFile);) { | |
| scanner.useDelimiter("[^a-zA-Z]+"); | |
| while (scanner.hasNext()) { | |
| final String token = scanner.next().toLowerCase(); | |
| final boolean hasWord = setOfTokens.contains(token); | |
| if (!hasWord) { | |
| System.out.println(token + ": is not in file"); | |
| final TreeSet<String> corrections = corrections(token, setOfTokens); | |
| if (corrections.size() == 0) System.out.println("(no suggestions)"); | |
| System.out.println(token + ": " + String.join(", ", corrections)); | |
| } | |
| } | |
| } catch (FileNotFoundException e) { | |
| throw new RuntimeException(e); | |
| } catch (RuntimeException runtimeException) { | |
| System.out.println(runtimeException); | |
| } | |
| } | |
| public static TreeSet<String> corrections(String badWord, HashSet<String> dictionary) { | |
| final TreeSet<String> corrections = new TreeSet<>(); | |
| for (int i = 0; i < badWord.length(); i++) { | |
| for (char ch = 'a'; ch <= 'z'; ch++) { | |
| final String newWord = badWord.substring(0, i) + ch + badWord.substring(i + 1); | |
| if (dictionary.contains(newWord)) corrections.add(newWord); | |
| } | |
| } | |
| return corrections; | |
| } | |
| public static void readWordsInFileIntoSet(String path) { | |
| try (final Scanner scanner = new Scanner(new File(path));) { | |
| while (scanner.hasNext()) { | |
| final String token = scanner.next(); | |
| process(token); // do something with the token | |
| } | |
| System.out.println("Size of set: " + setOfTokens.size()); | |
| } catch (FileNotFoundException e) { | |
| throw new RuntimeException(e); | |
| } catch (RuntimeException runtimeException) { | |
| System.out.println(runtimeException); | |
| } | |
| } | |
| public static File getInputFileNameFromUser() { | |
| final JFileChooser fileDialog = new JFileChooser(); | |
| fileDialog.setDialogTitle("Select File for Input"); | |
| final int option = fileDialog.showOpenDialog(null); | |
| if (option != JFileChooser.APPROVE_OPTION) { | |
| return null; | |
| } else { | |
| return fileDialog.getSelectedFile(); | |
| } | |
| } | |
| public static void process(String token) { | |
| setOfTokens.add(token.toLowerCase()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment