chetah
02-24-2009, 02:01 PM
I am trying to solve a predictive text problem, but I am having some difficulty.
----data---
Some sample input:
Pale choose chosen called move
predictive TEXT mobile
Phone Spell Message Note SaKe
$$$$
6683
7253
37549
662453
0
Sample output:
6683
move Note
7253
Pale SaKe
37549
No words found in dictionary
I am to read the above data into an array, after processing, the output should be as indicated above. I have worked on some code, but it is really sloppy. I am at the point were I am reading in the numbers from the data to find the string in the array. The main problem I am having is how do I convert these numbers into words so that I can compare them with the words in the dictinary? Help appreciated. Is there a more efficient way to represent this same code using different structures in java?
import java.io.*;
import java.util.*;
class Test{
static String[]list = new String[100];
public static void main(String[] args)throws IOException{
Scanner in = new Scanner (new FileReader("input.txt"));
PrintWriter out = new PrintWriter (new FileWriter("output.txt"));
String []num = new String[100];
int z = 0;
String str = in.next();
while(!(str.equals("$$$$"))){
list[z] = str;//read data into an array
num[z] = getWord(str);//conver words to numbers
z++;
str = in.next();
}
insertionSort(list,num,0,z);//sort words
String reNum="25533";//Testing to see if string read in is in the coverted list
String ans = compareNumbers(num,reNum,z);
If it is in the list, I want to convert the number into words and send it into the binary search method to find its location in the words list. (Problem, how do I convert the letters into words?
int mo = binarySearch(key, list, 0, z-1);
//System.out.println(mo);
in.close();
out.close();
}//main
public static void insertionSort(String[]list,String[]list2,int lo, int n){
for(int j = lo+1; j < n; j++){
String key = list [j];
int k = j-1;
while(k >= lo && key.compareToIgnoreCase(list[k]) < 0 ){
list [k + 1] = list [k];
--k;
}//end while
list [k+1] = key;
}//end for
}//end insertion sort
public static int binarySearch(String key, String[]b, int lo, int hi){
while(lo <= hi-1){
int mid = (lo + hi)/2;
int cmp = key.compareToIgnoreCase(b[mid]);
if (cmp==0)return mid;
if (cmp < 0)hi = mid -1;
else lo = mid + 1;
}//end while
return lo;
}//end binarysearch
public static String getWord(String word){
int l = word.length();
String str = " ";
for(int i = 0; i < l; i++){
char y = word.charAt(i);
if(y < 100){
str +=2;
}else if(y < 103){
str +=3;
}else if( y < 106){
str +=4;
}else if( y < 109){
str += 5;
}else if(y < 112){
str += 6;
}else if(y < 116){
str += 7;
}else if (y < 119){
str += 8;
}else{
str +=9;
}
}//end for
return str;
}//end getWord();
public static String convertWordsToNumbers(String[]list,String str2)//not working properly{
String hh = " ";
for(int i = 0; i < str2.length(); i++){
String str = list[i];
char x = str.charAt(i);
char y = str2.charAt(i);
if(x < 100 && y==50){
hh +=y;
}else if ( x < 103 && y ==51){
hh +=y;
}else if ( x < 106 && y ==52){
hh +=y;
}else if (x < 109 && y == 53){
hh +=y;
}else if (x < 112 && y == 54){
hh +=y;
}else if (x < 116 && y == 55){
hh +=y;
}else if (x < 119 && y ==56){
hh +=y;
}else{
hh +=y;
}
}//end for
return hh;
}//end method
public static String compareNumbers(String[]numbers, String key, int n){
for(int i = 0; i < n; i++){
int cmp = key.compareToIgnoreCase(numbers[i]);
if(cmp!=0) return key;
else System.out.println("I am not here");
}
return "";
}//end compare
}//class
----data---
Some sample input:
Pale choose chosen called move
predictive TEXT mobile
Phone Spell Message Note SaKe
$$$$
6683
7253
37549
662453
0
Sample output:
6683
move Note
7253
Pale SaKe
37549
No words found in dictionary
I am to read the above data into an array, after processing, the output should be as indicated above. I have worked on some code, but it is really sloppy. I am at the point were I am reading in the numbers from the data to find the string in the array. The main problem I am having is how do I convert these numbers into words so that I can compare them with the words in the dictinary? Help appreciated. Is there a more efficient way to represent this same code using different structures in java?
import java.io.*;
import java.util.*;
class Test{
static String[]list = new String[100];
public static void main(String[] args)throws IOException{
Scanner in = new Scanner (new FileReader("input.txt"));
PrintWriter out = new PrintWriter (new FileWriter("output.txt"));
String []num = new String[100];
int z = 0;
String str = in.next();
while(!(str.equals("$$$$"))){
list[z] = str;//read data into an array
num[z] = getWord(str);//conver words to numbers
z++;
str = in.next();
}
insertionSort(list,num,0,z);//sort words
String reNum="25533";//Testing to see if string read in is in the coverted list
String ans = compareNumbers(num,reNum,z);
If it is in the list, I want to convert the number into words and send it into the binary search method to find its location in the words list. (Problem, how do I convert the letters into words?
int mo = binarySearch(key, list, 0, z-1);
//System.out.println(mo);
in.close();
out.close();
}//main
public static void insertionSort(String[]list,String[]list2,int lo, int n){
for(int j = lo+1; j < n; j++){
String key = list [j];
int k = j-1;
while(k >= lo && key.compareToIgnoreCase(list[k]) < 0 ){
list [k + 1] = list [k];
--k;
}//end while
list [k+1] = key;
}//end for
}//end insertion sort
public static int binarySearch(String key, String[]b, int lo, int hi){
while(lo <= hi-1){
int mid = (lo + hi)/2;
int cmp = key.compareToIgnoreCase(b[mid]);
if (cmp==0)return mid;
if (cmp < 0)hi = mid -1;
else lo = mid + 1;
}//end while
return lo;
}//end binarysearch
public static String getWord(String word){
int l = word.length();
String str = " ";
for(int i = 0; i < l; i++){
char y = word.charAt(i);
if(y < 100){
str +=2;
}else if(y < 103){
str +=3;
}else if( y < 106){
str +=4;
}else if( y < 109){
str += 5;
}else if(y < 112){
str += 6;
}else if(y < 116){
str += 7;
}else if (y < 119){
str += 8;
}else{
str +=9;
}
}//end for
return str;
}//end getWord();
public static String convertWordsToNumbers(String[]list,String str2)//not working properly{
String hh = " ";
for(int i = 0; i < str2.length(); i++){
String str = list[i];
char x = str.charAt(i);
char y = str2.charAt(i);
if(x < 100 && y==50){
hh +=y;
}else if ( x < 103 && y ==51){
hh +=y;
}else if ( x < 106 && y ==52){
hh +=y;
}else if (x < 109 && y == 53){
hh +=y;
}else if (x < 112 && y == 54){
hh +=y;
}else if (x < 116 && y == 55){
hh +=y;
}else if (x < 119 && y ==56){
hh +=y;
}else{
hh +=y;
}
}//end for
return hh;
}//end method
public static String compareNumbers(String[]numbers, String key, int n){
for(int i = 0; i < n; i++){
int cmp = key.compareToIgnoreCase(numbers[i]);
if(cmp!=0) return key;
else System.out.println("I am not here");
}
return "";
}//end compare
}//class