Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class MaxHomophones { private static final String FILE_NAME = cmudict-0.7a.txt; public static void main(String[] args) {

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner;

public class MaxHomophones { private static final String FILE_NAME = "cmudict-0.7a.txt"; public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println(); int n = s.nextInt(); UALDictionary> dict = new UALDictionary<>(); try { Scanner sc = new Scanner(new File(FILE_NAME));

while (n > 0 && sc.hasNextLine()) { String line = sc.nextLine(); if (line.startsWith(";;;")) { continue; } String[] parts = line.split(" "); String word = parts[0].toUpperCase(); String[] phonemes = parts[1].split(" "); String lastPhoneme = phonemes[phonemes.length - 1]; ArrayList homophones = dict.find(lastPhoneme); if (homophones == null) { homophones = new ArrayList(); dict.insert(lastPhoneme, homophones); } homophones.add(word); n--; } sc.close(); } catch (FileNotFoundException e) { System.err.println("File not found: " + FILE_NAME); } int maxCount = 0; ArrayList> maxHomophones = new ArrayList<>(); for (ArrayList homophones : dict.values()) { int count = homophones.size(); if (count > maxCount) { maxCount = count; maxHomophones.clear(); maxHomophones.add(homophones); } else if (count == maxCount) { maxHomophones.add(homophones); } } System.out.println(maxCount); for (ArrayList homophones : maxHomophones) { for (String word : homophones) { System.out.println(word); } System.out.println(); } } }

Need to fix this code, its supposed that the largest set of homophones in the first n lines of the pronunciation dictionary is of size k, then your program should print out k on the first line, followed by k homophones, each on a new line. If there is more than one collection of k homophones, your program should print out each group, separated by a blank line. The output should be all upper case for consistency with the pronunciation dictionary.

For example, if the input is 1000 then the correct output is 5 ABBE ABBEY ABBIE ABBY 1

2 ABIE If the input is 40000 then a correct output is 10 BURY BUERRY BERRY BERRIE BERRI BERREY BARRY(1) BARRIE(1) BARRE BAREY BAYLY BAYLEY BAYLEE BALLY(1) BALEY BAILY BAILLY BAILLIE BAILIE BAILEY It would also be correct if the order of the two groups of homophones was reversed. If the input exceeds the size of the dictionary, the output should be the same as if the input were the size of the dictionary. In this case, the output shown for the case of 1000 is throwing 1000, which is wrong. The current implementation is not producing the correct output and needs to be fixed. Please, help me by fixing the code from above. Rewrite the code if needed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions