Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created February 10, 2010 00:15
Show Gist options
  • Save benfoxall/299852 to your computer and use it in GitHub Desktop.
Save benfoxall/299852 to your computer and use it in GitHub Desktop.
cat all.num | sed 's/\([^ ]*\) \([^ ]*\) .*/\2/' > list
#
# you can find all.num at http://www.kilgarriff.co.uk/bnc-readme.html
#
# was originally using the unsorted set and sorting it myself, which is why
# I decided to use sed to clean it up while I was at it.
# cat all.al | sort -nr | sed 's/\([^ ]*\) \([^ ]*\) .*/\2/' > list
# to reorder
cat list | java Textonyms talent
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Textonyms {
/**
* Outputs any words from the System.in that could have been generated
* using the same numbers on the keypad as the one given as an argument
*/
Textonyms(String word) throws IOException{
//keep track of the found words (because of ignored capitalisations)
HashSet<String> found = new HashSet<String>();
//find the numbers that we are looking for
int[] numbers = new int[word.length()];
for(int i = 0; i < word.length(); i++)
numbers[i] = number(word.charAt(i));
//get words from the System in, and test them
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String line;
char[] test;
while((line = r.readLine()) != null){
test = line.toLowerCase().toCharArray();
if(matches(numbers,test)){
if(!found.contains(line)){
System.out.println(line);
found.add(line);
}
}
}
}
/**
* Check to see if the numbers 'a' could have been presses to result
* in the characters 'b'
*/
private boolean matches(int[] a, char[] b){
if (a.length != b.length) return false;
for(int i = 0; i < a.length; i++)
if(a[i] != number(b[i]))
return false;
return true;
}
/**
* Gives the number pressed to get a particular character
*/
private int number(char c){
switch(c){
case 'a': case'b': case 'c': return 2;
case 'd': case'e': case 'f': return 3;
case 'g': case'h': case 'i': return 4;
case 'j': case'k': case 'l': return 5;
case 'm': case'n': case 'o': return 6;
case 'p': case'q': case 'r': case 's': return 7;
case't': case 'u': case 'v': return 8;
case'w': case 'x': case 'y': case'z': return 9;
}
return 0;
}
public static void main(String[] args){
try {
new Textonyms(args[0].toLowerCase());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment