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

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 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...
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,...
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT