All pastes #548575 Raw Edit

Something

public java v1 · immutable
·published 2007-06-07 19:42 UTC
rendered paste body
import java.util.*;import java.io.*;import javax.swing.*;import java.awt.BorderLayout;            public class NumberGame {	//! -> .	// 2 -> abc	// 3 -> def      	// 4 -> ghi	// 5 -> jkl	// 6 -> mno	// 7 -> pqrs	// 8 -> tuv	// 9 -> wxyz		static final char[][] intRelations = new char[][]{		{},		{},		"abc".toCharArray(),		"def".toCharArray(),		"ghi".toCharArray(),		"jkl".toCharArray(),		"mno".toCharArray(),		"pqrs".toCharArray(),		"tuv".toCharArray(),		"wxyz".toCharArray(),	};	static final int[] charRelations = new int[256];			private static Wrapper dictionary = new Wrapper(0);	private static int maxWordLength = 0;		public static void main(String[] args) throws IOException {		JFrame frame = new JFrame();		frame.setSize(800,600);		frame.setLayout(new BorderLayout());		JTextArea textArea = new JTextArea();		frame.add(textArea, BorderLayout.CENTER);				long t1 = System.currentTimeMillis();		System.out.println("Starting... ");		initCharRelations();				readDictionary();		long t2 = System.currentTimeMillis();		System.out.printf("%10dms: done reading the dictionary.\n", t2-t1);		frame.setVisible(true);				String oldResult = "!!XXX!!";		int caretPosition;		while (true) {			t2 = System.currentTimeMillis();						try {				Thread.sleep(50);			} catch (InterruptedException ie) {			}							Scanner scanner = new Scanner(new File("numbers.dat"));			String data = scanner.nextLine();			scanner.close();			scanner = new Scanner(new File("result.txt"));			String result = scanner.nextLine();			scanner.close();						if (oldResult.equals(result)) {				continue;			}			oldResult = result;			caretPosition = textArea.getCaretPosition();			textArea.setText("");						result = result.replaceAll(" ", "");					char[] resultArray = result.toCharArray();			char[][] lines = new char[4][resultArray.length];			int printLines = 0;					for (int i=0;i<lines.length;i++) {				Arrays.fill(lines[i], ' ');			}					for (int i=0;i<resultArray.length;i++) {				char c = resultArray[i];				int n = data.charAt(i) - '0';							if (getInt(c) != n) {					resultArray[i] = '_';					for (int j=0;j<intRelations[n].length;j++) {						lines[j][i] = intRelations[n][j];					}					printLines = Math.max(printLines, intRelations[n].length);				}			}					result = new String(resultArray);			PrintWriter output = new PrintWriter(new File("output.txt"));			output.println(data);			output.println(result);						textArea.append(data+'\n');			textArea.append(result+'\n');			for (int i=0;i<printLines;i++) {				output.println(new String(lines[i]));				textArea.append(new String(lines[i])+'\n');			}					for (int i=result.length();i<data.length();i++) {				String subString = data.substring(i);				final int length = subString.length();							String indention = buildIndention(i);							for (int j=Math.min(maxWordLength, length); j >= 1; j--) {					String subSubString = subString.substring(0, Math.min(length, j));					List<String> matches = dictionary.getMatches(subSubString);					for (String match: matches) {						output.println(indention+match);						textArea.append(indention+match+'\n');								}				}			}					output.close();						textArea.setCaretPosition(caretPosition);					long t3 = System.currentTimeMillis();			System.out.printf("%10dms: done evaluating.\n", t3-t2);			}	}		private static String buildIndention(int length) {		StringBuilder sb = new StringBuilder();		for (int i=0;i<length;i++) {			sb.append(' ');		}		return sb.toString();	}			private static void initCharRelations() {		Arrays.fill(charRelations, 0);				charRelations['a'] = 2;		charRelations['b'] = 2;		charRelations['c'] = 2;		charRelations['d'] = 3;		charRelations['e'] = 3;		charRelations['f'] = 3;		charRelations['g'] = 4;		charRelations['h'] = 4;		charRelations['i'] = 4;		charRelations['j'] = 5;		charRelations['k'] = 5;		charRelations['l'] = 5;		charRelations['m'] = 6;		charRelations['n'] = 6;		charRelations['o'] = 6;		charRelations['p'] = 7;		charRelations['q'] = 7;		charRelations['r'] = 7;		charRelations['s'] = 7;		charRelations['t'] = 8;		charRelations['u'] = 8;		charRelations['v'] = 8;		charRelations['w'] = 9;		charRelations['x'] = 9;		charRelations['y'] = 9;		charRelations['z'] = 9;				for (int i=0;i<=9;i++) {			charRelations[i+'0'] = i;		}	}		private static void readDictionary() throws IOException {		Scanner scanner = new Scanner(new File("dict.dat"));		while (scanner.hasNext()) {			String data = scanner.nextLine().toLowerCase();			dictionary.add(data);			if (data.length() > maxWordLength) {				maxWordLength = data.length();			}		}		scanner.close();			}			static int getInt(char theChar) {		return charRelations[theChar];	}		static class Wrapper {		int length;		Wrapper[] longer = new Wrapper[10];		List<String> matches = new ArrayList<String>();				public Wrapper(int length) {			this.length = length;		}				public void add(String word) {			if (word.length() == length) {				matches.add(word);			} else {				int key = getInt(word.charAt(length));				if (longer[key] == null) {					longer[key] = new Wrapper(length+1);				}				longer[key].add(word);			}		}				public List<String> getMatches(String word) {			/*List<String> match = getMatches(word, 0);						if (match.size() != 0) {				debug = true;				match = getMatches(word, 0);								System.out.println(word);				System.out.println(match);				System.exit(0);			}*/						return getMatches(word, 0);		}						static boolean debug = false;				public List<String> getMatches(String word, int index) {			//if (debug) System.out.println(length+": "+word+" - "+index);			if (index == word.length()) {				//if (debug) System.out.println(length+": found");				return matches;			} else {				int number = word.charAt(index)-'0';				//if (debug) System.out.println(length+": not found - calling "+number);								if (longer[number] == null) {					return Collections.<String>emptyList();				}				return longer[number].getMatches(word, index+1);			}		}	}}