Question

In: Computer Science

The purpose of this problem is to gain familiarity with the dictionary ADT. A homophone is...

The purpose of this problem is to gain familiarity with the dictionary ADT. 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 one of the implementations of the dictionary interface (UALDictionary or OALDictionary on Moodle) to find the word that has the most homophones.

Do not use data structures that we have not covered so far in the course. The file “cmudict.0.7a.txt” in the Dictionaries folder on Moodle contains a pronunciation dictionary downloaded from

http://www.speech.cs.cmu.edu/cgi-bin/cmudict

The page also contains a detailed description of the pronunciation dictionary. The file consists of lines of the form

ABUNDANT AH0 B AH1 N D AH0 N T

The first string is the word, which is followed by one or more phonemes (or phones) that describe the pronunciation of the word. There are 39 phonemes occurring in North American English that are used in the dictionary. The collection of 39 symbols is known as the Arpabet, for the Advanced Research Projects Agency (ARPA), which developed it in the 1970’s in connection with research on speech understanding.

The Dictionaries directory on Moodle contains several files you can use. UALDictionary.java is a class that implements the dictionary interface using an unordered array list. The array list stores (key, value) pairs as described in the class KVpair.java. Pronunciation.java is a class that stores and manages access to a (word, phonemes) pair.

There is also a program Homophones.java that shows how you can read in the cmu dictionary (skipping comments and so on). You can modify that program so that when you read in a pronunciation entry you store it in an appropriate dictionary. Call your program MostHomophones.

The output is a first line containing a single integer n, which is the largest number of homophones. The n homophones follow on the next n lines, one word per line.

UAL Dictionary

import java.util.ArrayList;

import java.util.Collections;

class UALDictionary<Key extends Comparable, E> implements Dictionary<Key, E> {

private static final int defaultSize = 10;

private ArrayList<KVpair<Key, E>> list;

// Constructors

UALDictionary() {

this(defaultSize);

}

UALDictionary(int sz) {

list = new ArrayList<KVpair<Key, E>>(sz);

}

public void clear() {

list.clear();

}

/** Insert an element: append to list */

public void insert(Key k, E e) {

KVpair<Key, E> temp = new KVpair<Key, E>(k, e);

list.add(temp);

}

/** Remove element with key k, return element */

public E remove(Key k) {

E temp = find(k);

if (temp != null)

list.remove(new KVpair<Key, E>(k, temp));

return temp;

}

/** Remove any element */

public E removeAny() {

return list.remove(list.size()-1).value();

}

/**

* Find k using sequential search

*

* @return Record with key value k

*/

public E find(Key k) {

for (KVpair<Key, E> t : list)

if (k.compareTo(t.key())==0)

return t.value();

return null; // "k" does not appear in dictionary

}

public Iterable<E> findAll(Key k) {

ArrayList<E> al = new ArrayList<E>();

for (KVpair<Key, E> t : list)

if (k.compareTo(t.key()) == 0)

al.add(t.value());

return al;

}

public int size(){

return list.size();

}

/** Returns an iterable collection of the dictionary. */

public Iterable<E> values() {

ArrayList<E> elements = new ArrayList<E>(list.size());

for(KVpair<Key, E> t : list)

elements.add(t.value());

return elements;

}

}

Homophones.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.util.ArrayList;

public class Homophone {

public static void main(String[] args) {

UALDictionary<String, Pronunciation> PDict = new UALDictionary<String, Pronunciation>();

File file = new File("cmudict.0.7a.txt");

  

final int len = 5; // we start with words of length 5 characters

long start = System.currentTimeMillis();

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

if (line.substring(0, 3).equals(";;;"))

continue; // skip comment lines

Pronunciation p = new Pronunciation(line);

if ((p.getWord().length() < len - 1)

|| (p.getWord().length() > len))

continue;

if ((p.getWord().length() == len - 1)

|| (p.getWord().length() == len))

PDict.insert(p.getWord(), p);

}

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

long middle = System.currentTimeMillis();

System.out.println("Loaded dictionary.");

for (Pronunciation p : PDict.values()) {

String w = p.getWord();

if (w.length() == len) {

/* Fill in code to determine if this word

is pronounced the same if we remove the

first letter or if we remove the second.

*/

}

}

long end = System.currentTimeMillis();

System.out.println("Run times: load dictionary= " + (middle - start)

+ " process= " + (end - middle) + " total= " + (end - start));

}

}

Please provide comments if possible, I will Thumbs Up for the help!

Solutions

Expert Solution

:: Solution ::

class Soundex{

    private static int getConsonantCode( char ch ){

        String codeList[] = { "BFPV", "CGJKQSXZ","DT","L","MN","R" };

        int code = 0;

        for( int i = 0 ; i < codeList.length ; i++ ){

             if( codeList[i].indexOf(ch) >= 0 ) {

                code = i+1;

            }

        }

        return code;

    }

    private static boolean isVowel( char ch ){

        return (new String("AEIOUaeiou")).indexOf(ch) >= 0 ;

    }

    public static String getSoundexCode( String str ){

        str=str.toUpperCase();

        String soundexCode = "" + str.charAt(0), temp="";

        int length = str.length();

        char curr, prev, next;{ }

        String dropList = "AEIOUYHW";

        for( int i=1 ; i< length ; i++ ){

            curr = str.charAt(i);

            prev = str.charAt( i-1 );

            if( ( curr=='H' || curr == 'W') && i != length-1 ){

                if( temp.length() >= 2) temp=temp.substring(1);

                next=str.charAt( i+1 );

                if( isVowel(curr) && getConsonantCode( prev ) == getConsonantCode( next ) ){

                    temp += prev+prev;

                    i=i+1;

                }else if( getConsonantCode( prev ) == getConsonantCode( next ) ){

                    temp += prev;

                    i=i+1;

                }

            }else if( getConsonantCode( curr ) != getConsonantCode( prev ) ){

                if( dropList.indexOf( curr ) == -1 ){

                    temp += curr;

                }

            }

        }

        temp = ( temp + "0000" ).substring( 0, 3 );

        for( int i = 0; i<=2 ; i++ ){

            soundexCode += getConsonantCode( temp.charAt( i ) );

        }

        return soundexCode;

    }

    public static void main( String []args ){

        System.out.println( getSoundexCode( "Ashcraft" )+" "+"A261" );

        System.out.println( getSoundexCode( "Ashcroft" )+" "+"A261" );

        System.out.println( getSoundexCode( "Gauss" )+" "+"G200" );

        System.out.println( getSoundexCode( "Ghosh" )+" "+"G200" );

        System.out.println( getSoundexCode( "Hilbert" )+" "+"H416" );

        System.out.println( getSoundexCode( "Heilbronn" )+" "+"H416" );

        System.out.println( getSoundexCode( "Lee" )+" "+"L000" );

        System.out.println( getSoundexCode( "Lloyd" )+" "+"L300" );

        System.out.println( getSoundexCode( "Moses" )+" "+"M220" );

        System.out.println( getSoundexCode( "Pfister" )+" "+"P236" );

        System.out.println( getSoundexCode( "Robert" )+" "+"R163" );

        System.out.println( getSoundexCode( "Rupert" )+" "+"R163" );

        System.out.println( getSoundexCode( "Rubin" )+" "+"R150" );

        System.out.println( getSoundexCode( "Tymczak" )+" "+"T522" );

        System.out.println( getSoundexCode( "Soundex" )+" "+"S532" );

        System.out.println( getSoundexCode( "Example" )+" "+"E251" );

    }

}

Please rate my answer. Thank you...


Related Solutions

Interpret managed care contract agreements and gain familiarity with language and strategies used by providers and...
Interpret managed care contract agreements and gain familiarity with language and strategies used by providers and managed care organizations. Comparative analysis of managed care contracts
The purpose of this assignment is to test your familiarity with Java I/O statements and if-else statements.
ObjectiveThe purpose of this assignment is to test your familiarity with Java I/O statements andif-else statements. This assignment also tests your understanding of the basics of Javaprogramming and execution, like top-down control flows, translating the logical solutionto a problem into code, and integrating the new concepts you just learned with theolder concepts, including data types and variables, declaration, initialization andassignments, arithmetic operators, etc.Please submit your source code (i.e. your java file) on Canvas under HW1.ProblemIt’s the year 2030 and H&R...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
The purpose of this assignment is to gain a better understanding of the code sets used...
The purpose of this assignment is to gain a better understanding of the code sets used for medical billing. These sets can be complicated, but you will learn in the EHR#8 assignment this week, that the EHR practice management functions for billing can use the clinical information to help you with code selection. Coding Classification Sets for Medical Coding and Billing Code Set When is this Code Set used? Format Example Source CPT Category I CPT Category I codes are...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
Purpose: This lab will give you experience modifying an existing ADT. Lab Main Task 1: Modify...
Purpose: This lab will give you experience modifying an existing ADT. Lab Main Task 1: Modify the ListInterface Java interface source code given below. Change the name of ListInterface to ComparableListInterface, and have ComparableListInterface inherit from the built-in Java interface Comparable. Note that Comparable has a method compareTo(). compareTo() must be used in programming logic you will write in a method called isInAscendingOrder() (more about isInAscendingOrder() is mentioned further down in the lab description). You can find a brief summary...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
Computer Science Design and implement an ADT that represents a triangle. The data for the ADT...
Computer Science Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle's three angles. The data should be in the private section of the class that implements the ADT. Include at least two initialization operations; one that provides default values for the ADT's data, and another that sets this data to client-supplied values. These operations are the class's constructors. The ADT also...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
Problem 1. The purpose of this problem is to practice the use of logic operations and...
Problem 1. The purpose of this problem is to practice the use of logic operations and quantifiers. For each Statement X below determine if each of the three statementsX1, X2, X3 that follow it satisfy the following: a) Xi implies X; b) X implies Xi; c) if Xi is true then X must be false; d) if X is true then Xi must be false. Statement A. In every house there is a mouse. A1. There is no house without...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT