Question & Answer: efore "smart phones" mobile phones used the numeric keypad to compose text messages. This was accomplished a couple different w…..

efore “smart phones” mobile phones used the numeric keypad to compose text messages. This was accomplished a couple different ways. ACMIA phones took a numeric code, consulted its dictionary of known words with that code, and offered choices for what that word might be. Consider the following table:

1 2 abc 3 def
4 ghi 5 jkl 6 mno
7 pqrs 8 tuv 9 wxyz
0 (space)

So, 263 might be and, 748 might be sit, or pit, 666 might be mom, or non, etc.

Your task is to write a program that displays all possible matches for given digit sequences, using a given dictionary (list of words).

Input

Your program first will read a file whose name is stored in the environment variable ACMIA_WORDS , compute the codes for each word, and store the (code, word) pair in a dictionary. (Careful! Each code may have several words that match it. Build your dictionary appropriately). The words file has one word per line.

You needn’t worry about words containing spaces, numbers, nor a ‘, as might be found in contractions, nor other punctuation. Capital letters, however, are fair game.

Here is a sample word list, has about 10,000 words in it.

You will then read from stdin a sequence of “phrases”, numberic codes, one phrase per line, and print out the translated phrase, with word choices indicated, as shown below.

Words will be separated by one or more spaces, indicated in the code by a zero (0) . Multiple consecutive zeroes will be treated as a single space. Leading and trailing zeroes will be ignored.

Read every line of input. Do not prompt for responses.

Output

You will decode each line of input, and write the decoded phrase to stdout

For each line read (for each code), for each sequence of non-zero digits, display the matching word from the dictionary. When more than one match is available, display all matches in lexical order between parentheses, separated by bars. If there is no matching word, display a sequence of asterisks of the same length.

So, given a word list:

chocolate
I
hear
loud
love
programming

, and the sequence:

0040568300077647266464077770

, you would output, on a single line:

I (loud|love) programming ****

You will output a single line for every line of input, no blank lines (unless a blank line is input).

Ant

Create an ant build file that has a target called run, which will run your program, making sure that it is first compiled.

Your buildfile will contain, minimally, the following targets:

run – runs your program. No environment variables to worry about

clean – removes all intermediate (*.class) files from the directory

Whatever other targets/dependencies you want to put in there, great.

Expert Answer

 

//main.java

import java.io.*;

import java.util.*;

public class PhoneDict

{

public static void main( String[] args )

{

String filepath = System.getenv( “ACMIA_WORDS” );

HashMap<String, List<String>> hmWords = new HashMap<String,

List<String>>();

try

{

String line;

String code = “”;

File test= new File(filepath);

if (test.exists() )

{

// ========== Read File, Create Map ================== //

new FileReader( filepath );

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

while( (line = br.readLine() ) != null )

{

System.out.println( line );

//reads each char, uses func to convert to code

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

{

code += charToCode( line.toLowerCase().charAt(i) );

}

//stores to hashmap, checks that code is not stored twice

if(!hmWords.containsKey(code))

{

hmWords.put( code, new ArrayList<String>() );

}

hmWords.get(code).add(line);

code = “”;

}

}

else

{

System.out.println( “Error: No file to read…” );

}

// =========== Read from Stdin =========== //

InputStreamReader in = new InputStreamReader( System.in );

BufferedReader input = new BufferedReader( in );

String phrase;

String translation=””;

String[] parts;

while( (phrase = input.readLine()) != null )

{

parts = phrase.split( “(0+)” );

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

{

System.out.println( parts[i] );

if( !hmWords.containsKey( parts[i] ) )

{

System.out.println( “” );

}

else if( hmWords.get(parts[i]).size() > 1)

{

translation += “(“;

for( int j = 0; j < hmWords.get(parts[i]).size(); ++j )

{

if( j == (hmWords.get(parts[i]).size() – 1) )

{

translation+= hmWords.get(parts[i]).get(j);

}

else

{

translation += hmWords.get(parts[i]).get(j) + “|”;

}

}

translation += “)”;

}

else

{

translation += hmWords.get(parts[i]).get(i) + ” “;

}

}

System.out.println( translation );

translation=””;

}

}

catch( Exception except )

{

System.out.println( except.getMessage() );

}

}

// ——————- FUNCTION ——————-

//takes char and returns to respective number on Number Pad as char

public static char charToCode( char word )

{

if( word==’a’ || word==’b’ || word==’c’ )

{

return ‘2’;

}

else if( word==’d’ || word==’e’ || word==’f’ )

{

return ‘3’;

}

else if( word==’g’ || word==’h’ || word==’i’ )

{

return ‘4’;

}

else if( word==’j’ || word==’k’ || word==’l’ )

{

return ‘5’;

}

else if( word==’m’ || word==’n’ || word==’o’ )

{

return ‘6’;

}

else if( word==’p’ || word==’q’ || word== ‘r’ || word==’s’ )

{

return ‘7’;

}

else if( word==’t’ || word==’u’ || word==’v’ )

{

return ‘8’;

}

else if( word==’w’ || word==’x’ || word== ‘y’ || word==’z’ )

{

return ‘9’;

}

else

{

return ‘0’;

}

}

}

Still stressed from student homework?
Get quality assistance from academic writers!