Question

In: Computer Science

3. Write a Java program that discover all anagrams of all wordslisted in the input file,...

3. Write a Java program that discover all anagrams of all wordslisted in the input file, “dict.txt”. An anagram of a work is a rearrangement of its letters into a new legal word. Your program should do the following:

a. Read in the given “dict.txt” file and sort it in each word’s canonical form. The canonical form of a word contains the same letters as the original word, but in sorted order

b. Instead of putting the “dict.txt” in the code, ask the user to enter the file name

c. Retrieve a word’s canonical form and write a Comparator to compare words by using their canonical forms.


The input file does not contain punctuation. and the file has over 300 pages.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//FindAnagrams.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class FindAnagrams {
        // helper method to get a word in sorted order
        static String getCanonicalForm(String word) {
                // converting word to lower case and then to a char array
                char chars[] = word.toLowerCase().toCharArray();
                // sorting chars array
                Arrays.sort(chars);
                // creating and returning a new word contains chars in sorted order
                word = new String(chars);
                return word;
        }

        public static void main(String[] args) throws FileNotFoundException {
                // creating a Comparator to compare two words by their canonical form
                // please note that this comparator is never used anywhere as there is
                // no need for that in our program, because we follow a different,
                // simple approach. I'm writing this only because the question ask us
                // to.
                Comparator<String> comparator = new Comparator<String>() {
                        @Override
                        public int compare(String s1, String s2) {
                                // converting s1 and s2 to cannonical forms
                                s1 = getCanonicalForm(s1);
                                s2 = getCanonicalForm(s2);
                                // comparing s1 and s2, returning comparison result
                                return s1.compareTo(s2);
                        }
                };
                // example usage: comparator.compare("post", "stop") will return 0 since
                // post and stop are anagrams

                // creating a map to store anagrams. here, key is the word in canonical
                // form, values are a set of unique words with that canonical form (when
                // sorted), or in other words, values are anagrams found with same
                // canonical form.
                TreeMap<String, Set<String>> anagrams = new TreeMap<String, Set<String>>();
                // using a scanner, asking and reading a file name
                Scanner sc = new Scanner(System.in);
                System.out.print("Enter the input file name: ");
                String filename = sc.nextLine();

                // reinitializing scanner to read from file. if file is not present,
                // this will throw exception
                sc = new Scanner(new File(filename));
                String word;
                // looping through each word on the file
                while (sc.hasNext()) {
                        word = sc.next();
                        // getting cannonical form of word
                        String sorted = getCanonicalForm(word);
                        // checking if this sorted word is on anagrams map as a key
                        if (!anagrams.containsKey(sorted)) {
                                // nope! we add to anagrams map with key=sorted and value=empty
                                // set. here set is used to preserve only unique values. TreeSet
                                // will keep the words in alphabetical order.
                                anagrams.put(sorted, new TreeSet<String>());
                        }
                        // now adding original word to the set associated with key=sorted on
                        // anagrams map
                        anagrams.get(sorted).add(word);
                }
                // closing file
                sc.close();
                // now looping through the map and displaying each canonical form and
                // the corresponding anagrams found.
                // note: if the input file is huge, then there will be a lot of lines of
                // output, in which case I suggest you to write the results to an output
                // file instead of console.
                System.out.printf("%-20s %s\n", "CANONICAL FORM", "ANAGRAMS");
                for (String str : anagrams.keySet()) {
                        // displaying cannonical form and the set of anagrams associated
                        // with it
                        System.out.printf("%-20s %s\n", str, anagrams.get(str));
                }
        }
}

/*PARTIAL OUTPUT (for some dict.txt)*/

Enter the input file name: dict.txt
CANONICAL FORM       ANAGRAMS
a                    [a]
aa                   [aa]
aaa                  [aaa]
aaaablm              [alabama]
aaaacdgmrs           [madagascar]
aaabbrr              [barbara]
aaabcehillpt         [alphabetical]
aaabdesst            [databases]
aaabdest             [database]
aaabdmorss           [ambassador]
aaabeijnrz           [azerbaijan]
...
abelst               [stable, tables]
abeltt               [battle, tablet]
abest                [beast, beats]
abt                  [bat, tab, tba]
...
acdeilm              [claimed, decimal, medical]
...
aefrs                [fares, fears, safer]
...
deit                 [diet, edit, tide, tied]

Related Solutions

Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT