Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default changing telephone numbers into words.

    hey guys, another question i can't solve :(.

    so if i have a number: 2665287, it can be translated into words right?

    for eg, the number can mean coolbus, but it can also have other words behind it.

    I'm having trouble with figuring out how to change the numbers into letters, and then matching it with a pre-configured dictionary file.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    import java.text.DecimalFormat;
    
    
    public class Lab10 {
    	
    	public static void main (String [] args) throws FileNotFoundException{
    		
    		readData();
    	}
    	
    	public static void readData ()throws FileNotFoundException{
    		File myFile = new File("telephone.txt");
    		Scanner input = new Scanner(myFile);
    		while (input.hasNext()){
    			String Message = input.next();
    			findWords(Message);
    		}
    		   input.close();  
    	}
    	
    	
    	public static void findWords(String Message) throws FileNotFoundException{
    		
    		int count=0;
    		int line=5000;
    		double number = Double.parseDouble(Message);
    		DecimalFormat formatter = new DecimalFormat("##########");
    		System.out.println(formatter.format(number));
    		
    		while (count<line){
    			count+= findWords(number,count);
    			count++;
    		}
    		
    	}
    	
    	public static int findWords(double number, int line){
    		
    		String word="";
    		double sixohfour=604E7;
    		number -= sixohfour;
    		String [] numtoletter = {"","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"};
    		File myFile = new File("samplewords.txt");
    		int count=10;
    		int rem;
    		
    		while (count>0){
    			rem= (int) (number%10);
    			number /= 10;
    			switch (rem){
    				case 0:
    					break;
    				case 1:
    					break;
    				case 2:
    					word= 'A'+ word;
    					
    					break;
    				case 3:
    					word= 'D' + word;
    					break;
    				case 4:
    					word='G' + word;
    					break;
    				case 5:
    					word='J' +word;
    					break;
    				case 6:
    					word='M' +word;
    					break;
    				case 7:
    					word='P' + word;
    					break;
    				case 8:
    					word='T' + word;
    					break;
    				case 9:
    					word= 'W' + word;
    					break;
    				default:
    					break;
    				}
    			count--;
    		}
    		
    		System.out.println(word);
    		
    		return 1;
    	}
    			
      }
    The code is nowhere close to finish. But this is how i have tried to approach it so far.

    problem 1: although i have managed to switch the number into alphabets using a switch, how do i keep rolling the alphabets around and get different words out of it?

    problem 2: how do i match the word with my preconfigured dictionary.

    eg named: dictionary.txt.

    edit*

    ignore the decimal format, i was just trying to test that class out.

    also, what i'm thinking is if i can put an if statement into the switch to change it up a bit. But i'm not sure how to approach it.
    Last edited by akeni; 11-18-2011 at 10:10 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Do it the other way around: take each word in your dictionary and convert the words to numbers; build a map from it all: Map<String, List<String> where the keys are the numbers and the values are lists of words from your dictionary.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    Thanks for the tip, sounds way better that way :P. Can you provide a link that provides more insight with the map class? not sure how to really use it.

    also, with the switch command, can it be done like this:

    switch (dictionary.txt) ?

    or do i have to extract the word first and then use cases for it?
    Last edited by akeni; 11-18-2011 at 10:46 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    Thanks for the tip, sounds way better that way :P. Can you provide a link that provides more insight with the map class? not sure how to really use it.

    also, with the switch command, can it be done like this:

    switch (dictionary.txt) ?

    or do i have to extract the word first and then use cases for it?
    First try to map a dictionary word to a sequence of digits (an array of 26 chars can do it); next store the encoded word in a map, together with the non-encoded word (read the API documentation for the Map interface and its implementing classes). You guess about that switch thing doesn't work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    right now i have this:

    Java Code:
    public static int findword(double number, int line) throws FileNotFoundException{
    		
    		String word="";
    		char [] alpha = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    		double epsilon= 0.000000000001;
    		double sixohfour=604E7;
    		number -= sixohfour;
    		File myFile = new File("samplewords.txt");
    		Scanner input = new Scanner(myFile);
    		while (input.hasNext()){
    			String x = input.next();
    			
    			int i =0;
    			
    			while (i<x.length()){
    				
    				switch(alpha[x.indexOf(i)]){
    					case 0:
    					case 1:
    					case 2:
    						word = '2' + word;
    						break;
    					case 3:
    					case 4:
    					case 5:
    						word = '3' + word;
    						break;
    					case 6:
    					case 7:
    					case 8:
    						word = '4' + word;
    						break;
    					case 9:
    					case 10:
    					case 11:
    						word = '5' + word;
    						break;
    					case 12:
    					case 13:
    					case 14:
    						word = '6' + word;
    						break;
    					case 15:
    					case 16:
    					case 17:
    					case 18:
    						word = '7' +word;
    						break;
    					case 19:
    					case 20:
    					case 21:
    						word = '8' +word;
    						break;
    					case 22:
    					case 23:
    					case 24:
    					case 25:
    						word = '9' +word;
    					default:
    						break;
    				}
    				i++;
    			}
    			
    			double tmp = Double.parseDouble(word);
    			
    			if( Math.abs(tmp-number)<epsilon){
    				System.out.println(x);
    			}
    		}
    		return 0;
    problem: switch doesn't work like what we thought.

  6. #6
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    LOL, okay this is really ugly, but i think it works. Probably not efficient, but with my skill level this is what i came up with. have to learn the map class and see how i can improve this.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    import java.text.DecimalFormat;
    import java.lang.Math;
    
    
    public class Lab10 {
    	
    	public static void main (String [] args) throws FileNotFoundException{
    		
    		readData();
    	}
    	
    	public static void readData ()throws FileNotFoundException{
    		File myFile = new File("telephone.txt");
    		Scanner input = new Scanner(myFile);
    		while (input.hasNext()){
    			String Message = input.next();
    			findword(Message);
    		}
    		   input.close();  
    	}
    	
    	
    	public static void findword(String Message) throws FileNotFoundException{
    		
    		double number = Double.parseDouble(Message);
    		
    		word(number);
    
    		
    	}
    	
    	public static int word(double number) throws FileNotFoundException{
    		
    		
    		double epsilon= 0.000000000001;
    		double sixohfour=604E7;
    		number -= sixohfour;
    		File myFile = new File("samplewords.txt");
    		Scanner input = new Scanner(myFile);
    		while (input.hasNext()){
    			String x = input.next();
    			String word="";
    			int i =0;
    			
    			char [] a = x.toCharArray();
    			while (i<a.length){
    				
    				switch(a[i]){
    					case 'A':
    						word = word + 2;
    						break;
    					case 'B':
    						word = word + 2;
    						break;
    					case 'C':
    						word = word + 2;
    						break;
    					case 'D':
    						word = word + 3;
    						break;
    					case 'E':
    						word = word + 3;
    						break;
    					case 'F':
    						word = word + 3;
    						break;
    					case 'G':
    						word = word + 4;
    						break;
    					case 'H':
    						word = word + 4;
    						break;
    					case 'I':
    						word = word + 4;
    						break;
    					case 'J':
    						word = word + 5;
    						break;
    					case 'K':
    						word = word + 5;
    						break;
    					case 'L':
    						word = word + 5;
    						break;
    					case 'M':
    						word = word + 6;
    						break;
    					case 'N':
    						word = word + 6;
    						break;
    					case 'O':
    						word = word + 6;
    						break;
    					case 'P':
    						word = word + 7;
    						break;
    					case 'Q':
    						word = word + 7;
    						break;
    					case 'R':
    						word = word + 7;
    						break;
    					case 'S':
    						word = word + 7;
    						break;
    					case 'T':
    						word = word + 8;
    						break;
    					case 'U':
    						word = word + 8;
    						break;
    					case 'V':
    						word = word + 8;
    						break;
    					case 'W':
    						word = 9 +word;
    						break;
    					case 'X':
    						word = word + 9;
    						break;
    					case 'Y':
    						word = word + 9;
    						break;
    					case 'Z':
    						word = word + 9;
    						break;
    					
    					default:
    						break;
    					
    				}
    				
    				i++;
    			}
    			
    			double tmp = convert(word);
    			if( Math.abs(tmp-number)<epsilon){
    				DecimalFormat formatter = new DecimalFormat("##########");
    				System.out.println(formatter.format(number));
    				System.out.println(x);
    			}
    		}
    		return 0;
    	}
    	
    	public static double convert( String word){
    		char [] x = word.toCharArray();
    		double sum=0;
    		int j=0;
    		
    		for (int i=x.length-1;i>=0;i--){
    			sum += (x[i]-'0') * Math.pow(10,j);
    			j++;
    		}
    		
    		return sum;
    	}
      }

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Oh dear, this is going nowhere; the following is spoonfeeding, so I leave it to you to figure it all out. Read the API documentation for the used interfaces/classes.

    Java Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    
    public class T {
    
    	private static final String chars=   "abcdefghijklmnopqrstuvwxyz";
    	private static final char[] encoded= "22233344455566677778889999".toCharArray();
    	private static final Map<String, List<String>> dict= new HashMap<String, List<String>>();
    	
    	public static String convert(String word) {
    		StringBuilder sb= new StringBuilder();
    		
    		for (char c : word.toLowerCase().toCharArray()) {
    			int i= chars.indexOf(c);
    			if (i >= 0)
    				sb.append(encoded[i]);
    		}
    		
    		return sb.toString();
    	}
    
    	public static void storeWord(String word) {
    		
    		String enc= convert(word);
    		List<String> value= dict.get(enc);
    		
    		if (value == null)
    			dict.put(enc, value= new ArrayList<String>());
    		
    		value.add(word);
    	}
    	
    	public static void main(String[] args) {
    		storeWord("test");
    		storeWord("baba");
    		storeWord("caca");
    		
    		System.out.println(dict);
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    Thanks for showing me how you used those following classes. I will go read the API, and see if i can replicate the way you did it.

  9. #9
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    i'm just going over your code right now.

    Sir that's some beast coding. Thanks again, i'm learning a lot :D.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    Sir that's some beast coding. Thanks again, i'm learning a lot :D.
    I think I consider that a compliment ;-) study it and and feel free to use it if it is any use to you; all you have to do is feed it the words from your dictionary; you can use the map afterwards.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    i figured out how it is stored. What if i want to grab just the string value from dict? where can i grab it and do i just use dict<string>?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    i figured out how it is stored. What if i want to grab just the string value from dict? where can i grab it and do i just use dict<string>?
    Please clarify your question; 'dict' is just a map in my code; given a 'numbered word' as the key, it associates all words that have the same number code. What do you want to 'grab'? You can always create another Map<String, String> that maps a 'raw' word to its 'encoded' version but that's what the convert( ... ) method is for ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    so when you create the map, you have values in the String and list<string>. you can print all the values by using System.out.println(dict);

    but how do i pull out just one of the mapped variables? for ex. i want to see pull out ADDRESS, what do i do?

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    so when you create the map, you have values in the String and list<string>. you can print all the values by using System.out.println(dict);

    but how do i pull out just one of the mapped variables? for ex. i want to see pull out ADDRESS, what do i do?
    First convert it and look up the result in the map:

    Java Code:
    String conv= convert("ADDRESS");
    List<String> results= dict.get(conv);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    i used a while loop to run the whole txt file through the code, so should i extract the map after the whole map is done or at the convert method

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    i used a while loop to run the whole txt file through the code, so should i extract the map after the whole map is done or at the convert method
    I don't understand your question; the dict map contains those numbers that represent a word in your text file, e.g. 1111 -> abba, acca, 1221 -> adda etc. Those numbers are the keys in the map; those lists of Strings are the words. What else do you want?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    Sorry if this is confusing,

    okay so when i run this code:

    Java Code:
    public static void main(String[] args)  throws FileNotFoundException{
    	   File myFile = new File("samplewords.txt");
    	   Scanner input = new Scanner(myFile);
    	   while (input.hasNext()){
    		String Message = input.next();
    		double number= storeWord(Message);
    		
    	   }
             
            System.out.println(dict);
    
        }
    }
    this gives me the output of the picture: changing telephone numbers into words.-example.png

    okay. So now my question is this.

    for example, i have a number 5368463. The dict shows that the word lentine is equal to 5368463. How can i ask the map to pull out the string that is associated with 5368463?

    so lets say i run another text file called telephone.txt.

    inside has numbers

    235235
    2342362362
    234234
    etc..

    and i want to show any words that can be made out of the telephone numbers.

    To do that i would have to use a command that takes the info from the map correct?

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Simply get the number from the Map; if it is in the Map you'll get a list of words matching your number. Read the API documentation for the Map interface and reread my previous replies. It's as simple as:

    Java Code:
    String number= "235235";
    List<String> matches= dict.get(number);
    kind regards,

    Jos

    ps. on second thought, maybe it's better to make it a Map<String, Set<String>> so that you don't get any duplicate words (from your text file).
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: changing telephone numbers into words.

    thanks,

    i tried using the dict.get() before, but instead of having the entry as a string, i stupidly used ints :(. Thanks for the help so far

    and i'll see if Set<string> will help me eradicate any duplicate words (i believe the dictionary txt file does have duplicate words so it should help)

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: changing telephone numbers into words.

    Quote Originally Posted by akeni View Post
    thanks,

    i tried using the dict.get() before, but instead of having the entry as a string, i stupidly used ints :(. Thanks for the help so far

    and i'll see if Set<string> will help me eradicate any duplicate words (i believe the dictionary txt file does have duplicate words so it should help)
    A Set<String> doesn't store duplicate Strings, so it should help. Simply change every List<String> reference to a Set<String> and you're in business. Use a new HashSet<String>() instead of new ArrayList<String>() when you want to instantiate one (see my code in one of my first replies).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Converting numbers to words
    By Zora in forum New To Java
    Replies: 2
    Last Post: 11-14-2011, 10:10 AM
  2. Writing a java applet that converts numbers to words
    By derekmski in forum Java Applets
    Replies: 1
    Last Post: 04-16-2011, 03:33 AM
  3. using a pc like a telephone
    By totitua in forum New To Java
    Replies: 3
    Last Post: 06-03-2010, 12:16 PM
  4. new to java, converting words into numbers
    By echo33 in forum New To Java
    Replies: 4
    Last Post: 04-22-2009, 09:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •