Answered You can buy a ready-made answer or pick a professional tutor to order an original one.

QUESTION

For this project, you are going to write a program to find the anagrams in a given dictionary for a given word. If two words have the same letters but in different order, they are called anagrams. For

For this project, you are going to write a program to find the anagrams in a given dictionary for a given word. If two words have the same letters but in different order, they are called anagrams. For example, “listen” and “silent” are a pair of anagrams.

**JAVA**

First let’s focus on “LetterInventory.java”.

Its purpose is to compute the canonical “sorted letter form” for a given word.

1. Implement the constructor, which takes the String input word. You can assume that the input word contains only valid letters (e.g. no punctuation or blanks), but it may contain letters in different cases.

2. Implement the method: public String getCanonical() It should return the canonical “sorted letter form” for the input word (given in the constructor). It should use only lower cases. For example, if the given word is “AliBaba”, then this method should return “aaabbil”.

****I have already completed all of the above tasks I am struggling with the next part****

**below is the code I have for LetterInventory.java**

import java.util.*;

public class LetterInventory {

   private String input; 

    //stores the original word. 

    public LetterInventory(String input) {        this.input = input;    }    //uses an array of chars to hold word and sorts it into alphabetical order.    //displays it in lower case.    public String getCanonical() {        input = input.toLowerCase(); // converting input to lower case        char [] word = input.toCharArray();        Arrays.sort(word);        String sorted = new String (word);        return sorted; 

    }}

*** this is where i am stuck ***

Once “LetterInventory.java” is working as expected, we can now focus on “AnagramFinder.java”. It will read an input “dictionary” file, which contains quite many words, to build a map. The “key” for the map is a word’s canonical form, the “value” for the map will be all the words in the given dictionary that share the same canonical form. For example, if both “listen” and “silent” are in the dictionary, one entry in the map would have key as “eilnst” and value as an ArrayList of words “listen” and “silent”.

Implement the constructor, which takes in the input dictionary file and build the map. You can assume the words in the dictionary contain only valid letters. You should take advantage of “LetterInventory”.

3. Now implement the method: public void printAnagrams(String word) It should try to search the anagrams for the given word in the map. Again, you can assume the input word contains only valid letters, and you should take advantage of “LetterInventory”.

Print out any anagrams found. For now, don’t worry about the case where the input word is the same as the word inside the dictionary, just print them out.

Here is some sample print out:

Your input is: silent. Found anagrams: listen, silent, tinsel.

Your input is: AliBaba. Sorry, didn’t find any anagrams.

A sample main method is already written for you to test various functionalities with some sample input words.

*** Below is the shell for the main method ***

import java.io.*;import java.util.*;

public class AnagramFinder {    public static void main(String[] args) throws FileNotFoundException {        String[] originals = new String[] {            "realfun",            "mias",            "EVIL",            "unable",            "Silent",            "AliBaba"        };

       for(String original: originals) {            LetterInventory inv = new LetterInventory(original);            System.out.println("Original: " + original + ", Canonical: " + inv.getCanonical());        }

       System.out.println("\n");

       File f = new File("HW_sample_dict.txt"); 

        AnagramFinder finder = new AnagramFinder(f);

       for(String original: originals) {            finder.printAnagrams(original);        }    }

   public AnagramFinder(File f) throws FileNotFoundException {       //TODO implement      }

   public void printAnagrams(String word) {        //TODO implement    }

   public void printAnagrams2(String word) {        // Extra Credit: optional to implement    }} 

** can leave out extra credit part i just need help with the first 2 methods in the main file **

** the dict.txt is 400 pages long so I did not include it (the file just contains thousands of words) **

Show more
ANSWER

import java.io.*;import java.util.*;

public class AnagramFinder {

  Map<String, String> dictionary = new   HashMap<String, String>();

   public static void main(String[] args) throws FileNotFoundException {       String[] originals = new String[] {           "realfun",           "mias",           "EVIL",           "unable",           "Silent",           "AliBaba"       };

       for(String original: originals) {           LetterInventory inv = new LetterInventory(original);           System.out.println("Original: " + original + ", Canonical: " + inv.getCanonical());       }

       System.out.println("\n");

       // TODO: change path to match where your input file is located      File f = new File("sample_dict.txt");

       AnagramFinder finder = new AnagramFinder(f);

       for(String original: originals) {           finder.printAnagrams(original);       }

   }

   public AnagramFinder(File f)throws FileNotFoundException      {       // TODO: implement try{

BufferedReader br = new BufferedReader(new FileReader(f));

String input = "";

LetterInventory sort = null ;

// add dictionay file to mapwhile ((input = br.readLine()) != null) {

//sort the string by getCanonicalsort  = new LetterInventory(input);String sortedInput = sort.getCanonical();

if (dictionary.get(sortedInput) == null) {dictionary.put(sortedInput, input);}

else {String previousValue = dictionary.get(sortedInput);dictionary.put(sortedInput, previousValue + "; " + input);}}}catch  (IOException e){System.out.println(e);}

   }

public void printAnagrams(String word) {       // TODO: implement

String output = "";int length = word.length();

LetterInventory sort  =null;

int lengthloop = (int) (Math.pow(2, length)-1);

  Map<String, String> othermap = new  HashMap<String, String>();

for (int j=0; j<lengthloop; j++) {

String convertbin = Integer.toBinaryString(j+1);String tempstring = "";

int convertbinLength = convertbin.length();

for (int k=convertbinLength-1; k>=0; k--) {

if (convertbin.charAt(k)=='1') {int getChar = length - (convertbinLength - k);tempstring = word.charAt(getChar) + tempstring;}}

sort  = new LetterInventory(tempstring);String sortedBuffer = sort.getCanonical();

if (othermap.get(sortedBuffer) != null) { }

else {

othermap.put(sortedBuffer, "");String dictVal = "";

if ((dictVal = dictionary.get(sortedBuffer)) != null) {

String[] dictValSplit = dictVal.split("; ");

for (int m=0; m<dictValSplit.length; m++) {

if(dictValSplit[m].length()   == length) {

 output += dictValSplit[m] ;  if( m != dictValSplit.length - 1)           output += ", ";

      }

}

}}

}

if(!output.equals(""))System.out.println ("Your input is: "+word+". Found anagrams:  "+output+".");elseSystem.out.println ("Your input is: "+word+". Sorry, didn't find any angrams.");

    }

public void printAnagrams2(String word) {

// Extra Credit: optional to implement

String output = "";int length = word.length();

int lengthloop = (int) (Math.pow(2, length)-1);

 Map<String, String> othermap = new HashMap<String, String>();

for (int j=0; j<lengthloop; j++) {

String convertbin = Integer.toBinaryString(j+1);String tempstring = "";

int convertbinLength = convertbin.length();

for (int k=convertbinLength-1; k>=0; k--) {

if (convertbin.charAt(k)=='1') {    int getChar = length - (convertbinLength - k);    tempstring = word.charAt(getChar) + tempstring;}}

char[] letters = tempstring.toCharArray();Arrays.sort(letters);String sortedBuffer = String.valueOf(letters);

if (othermap.get(sortedBuffer) != null) { }

else {

othermap.put(sortedBuffer, "");String dictVal = "";

if ((dictVal = dictionary.get(sortedBuffer)) != null) {

    String[] dictValSplit = dictVal.split("; ");

    for (int m=0; m<dictValSplit.length; m++) {

        if(dictValSplit[m].length()   == length && !dictValSplit[m].equals(word) ) {

         output += dictValSplit[m] ;          if( m != dictValSplit.length - 1)                      output += ", ";

              }

    }

}}

   }

if(!output.equals(""))System.out.println ("Your input is: "+word+". Found anagrams:  "+output+".");elseSystem.out.println ("Your input is: "+word+". Sorry, didn't find any angrams.");

   }}

import java.util.Arrays;

public class LetterInventory {

    String words;

    public LetterInventory(String input) {        // TODO: implement

        words = input;    }

    public String getCanonical() {        // TODO: implement

        words = words.toLowerCase();        char [] word = words.toCharArray();        Arrays.sort(word);        String result  = new String (word);

       return result;    }}

LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question