In: Computer Science
In Java
1) A homophone is one of two or more words that are pronounced alike but are different in meaning or spelling; for example, the words “two", “too", and “to". Write a Java program that uses HashMap to find the most words that has the same homophones and return the count of the number of words. 2) Implement insertionSort. 3) Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList or LinkedList must be used in your program.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
public class MostHomophones {
public static void main(String[] args) throws FileNotFoundException
{
MostHomophones ex = new MostHomophones();
}
ArrayList <String> value = new ArrayList<>();
HashMap <String, ArrayList<String>> lhm = new
HashMap();
HashMap <String, ArrayList<String>> Entry = new
HashMap();
public MostHomophones() throws FileNotFoundException {
Scanner input = new Scanner(new
FileReader("cmudict.0.7a.txt"));
while(input.hasNext()) {
String s = input.nextLine();
value.add(s);
String[] line = s.split(" ");
System.out.println(line[0]);
String key = s.substring(line[0].length() + 1 , s.length());
if(lhm.containsKey(key)){
ArrayList<String> value = lhm.get(key);
value.add(line[0]);
lhm.put(key,value);
}
else{
ArrayList<String> value = new
ArrayList<String>(Collections.singleton(line[0]));
lhm.put(key,value);
}
Map.Entry<String, Integer> maxEntry = null;
}
int largest = 0;
ArrayList <String> wordList = new ArrayList<>();
for(Map.Entry<String, ArrayList<String>> ent :
lhm.entrySet()){
largest = (ent.getValue().size() > largest) ?
ent.getValue().size() : largest;
wordList = (ent.getValue().size() > largest) ? ent.getValue() :
wordList;
}
System.out.println("The most frequent is " + largest + "
times.");
System.out.println("The words are " + wordList);
input.close();
}
}