Question

In: Computer Science

Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt"...

Java Programming: spellcheck

Write code to read in a dictionary from the current directory called "dict.txt" (you can find this in the current directory/data)

Add each word to a hash map after splitting words and throwing away punctuation (see the demo).

open a file called "spell.txt" to check in the current directory.

Print out every word in spell.txt that is NOT in the dictionary.

Example:

if spell.txt contains:

hello, this is a test. 2152189u5
Misspelled! cApitalized

The output should be:
2152189u5
Misspelled
cApitalized

Solutions

Expert Solution

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map.Entry;

public class SpellCheck {

    private static HashMap<String, String> readWords(String fileName) throws IOException {

        var lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
        var itr = lines.iterator();
        var map = new HashMap<String, String>();

        while (itr.hasNext()) {
            var words = itr.next().split("\\W");
            for (String word : words) {
                if (!map.containsKey(word))
                    map.put(word.toLowerCase(), word);
            }
        }
        
        return map;
    }

    public static void main(String[] args) throws IOException {

        var dictWords = readWords("dict.txt");
        var fileWords = readWords("spell.txt");

        for (Entry<String, String> word : fileWords.entrySet()) {
            if(!dictWords.containsKey(word.getKey()))
                System.out.println(word.getValue());
        }
    }
}
Contect of dict.txt
hello, this is a sample test dictionary.
Content of spell.txt
hello, this is a test. 2152189u5
Misspelled! cApitalized
Output
cApitalized
2152189u5
Misspelled

Code with sample input output.


Related Solutions

Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user...
Write a program that creates a file called "data.dat" in the current directory. Prompt the user for five numbers, and write them, one at a time, on both the screen and into the file. Close the file, then open it again for reading only, and display the contents on the screen. Handle error conditions that may occur. Please Need this to be done in PERL. Thanks
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT