WordWhacker V0.3

PublishedActual
Birmingham New Street07:1007:11
London Euston08:3008:26

Today is a strange one, there were about 14 inspectors/observers on the train platform watching trains arrive/leave. The funny thing is that today most of the trains have arrived and left late so I’ve been trying to decide if, at some paranormal level, one has influenced the other in some way.

Anyway, as previously covered I have been trying to prove that there are some words and phrases that are equivalent to others in some way.

v0.3

The WWW is a wonderful resource for finding information and resources that can be used freely, after some quick searching I found the FreeBSD wordlist which is a simple list of words.

My idea now was to generate the hash for every word in the word list, I would then compare the hash of the company name against the hashed word list to find matching words.

I transformed the file into a usable state, a quick addition to the Java code and I had a method which read in the text file, generated the numeric value per word and then stored it in a Map ready for use.

  1package words;
  2
  3import java.io.BufferedReader;
  4import java.io.File;
  5import java.io.FileInputStream;
  6import java.io.IOException;
  7import java.io.InputStreamReader;
  8import java.util.HashMap;
  9import java.util.HashSet;
 10import java.util.Map;
 11import java.util.Map.Entry;
 12import java.util.Set;
 13
 14/**
 15 * Class to generate numerical values for words and compare equivalence to other words.
 16 *
 17 * @author a
 18 */
 19public class WordWhackerV03 {
 20
 21    public enum Charset {
 22        ASCII, UNICODE, POSITIONAL
 23    }
 24
 25    public static Map<Character,Integer> letters = new HashMap<Character,Integer>();
 26    public static Map<Character,Integer> asciiletters = new HashMap<Character,Integer>();
 27    static {
 28        asciiletters.put('A', 65);asciiletters.put('B', 66);asciiletters.put('C', 67);
 29        asciiletters.put('D', 68);asciiletters.put('E', 69);asciiletters.put('F', 70);
 30        asciiletters.put('G', 71);asciiletters.put('H', 72);asciiletters.put('I', 73);
 31        asciiletters.put('J', 74);asciiletters.put('K', 75);asciiletters.put('L', 76);
 32        asciiletters.put('M', 77);asciiletters.put('N', 78);asciiletters.put('O', 79);
 33        asciiletters.put('P', 80);asciiletters.put('Q', 81);asciiletters.put('R', 82);
 34        asciiletters.put('S', 83);asciiletters.put('T', 84);asciiletters.put('U', 85);
 35        asciiletters.put('V', 86);asciiletters.put('W', 87);asciiletters.put('X', 88);
 36        asciiletters.put('Y', 89);asciiletters.put('Z', 90);
 37
 38        asciiletters.put('a', 97); asciiletters.put('b', 98); asciiletters.put('c', 99);
 39        asciiletters.put('d', 100);asciiletters.put('e', 101);asciiletters.put('f', 102);
 40        asciiletters.put('g', 103);asciiletters.put('h', 104);asciiletters.put('i', 105);
 41        asciiletters.put('j', 106);asciiletters.put('k', 107);asciiletters.put('l', 108);
 42        asciiletters.put('m', 109);asciiletters.put('n', 110);asciiletters.put('o', 111);
 43        asciiletters.put('p', 112);asciiletters.put('q', 113);asciiletters.put('r', 114);
 44        asciiletters.put('s', 115);asciiletters.put('t', 116);asciiletters.put('u', 117);
 45        asciiletters.put('v', 118);asciiletters.put('w', 119);asciiletters.put('x', 120);
 46        asciiletters.put('y', 121);asciiletters.put('z', 122);asciiletters.put(' ', 20);
 47
 48        letters.put('a', 1); letters.put('b', 2); letters.put('c', 3);
 49        letters.put('d', 4); letters.put('e', 5); letters.put('f', 6);
 50        letters.put('g', 7); letters.put('h', 8); letters.put('i', 9);
 51        letters.put('j', 10);letters.put('k', 11);letters.put('l', 12);
 52        letters.put('m', 13);letters.put('n', 14);letters.put('o', 15);
 53        letters.put('p', 16);letters.put('q', 17);letters.put('r', 18);
 54        letters.put('s', 19);letters.put('t', 20);letters.put('u', 21);
 55        letters.put('v', 22);letters.put('w', 23);letters.put('x', 24);
 56        letters.put('y', 25);letters.put('z', 26);letters.put(' ', 0);
 57    }
 58
 59    public Map<String,Integer> words = new HashMap<String,Integer>();
 60
 61    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
 62    public Charset useCharset = Charset.ASCII;
 63
 64    /**
 65     * @param args a {@link java.lang.String}[] of program arguments
 66     */
 67    public static void main(String[] args) {
 68        WordWhackerV03 whacker = new WordWhackerV03();
 69        whacker.driveApp();
 70    }
 71
 72    /**
 73     * Utility method to control the flow of the application
 74     */
 75    public void driveApp() {
 76        this.createWordlist();
 77        String[] strings = new String[]{"Happiness", "Eternal Happiness",
 78                                        "Perpetual Happiness", "Happy Employees",
 79                                        "Motivational Happiness", "Creative Happiness",
 80                                        "Boundless Creativity"};
 81        for(String val : strings) {
 82            int value = this.getWordValue(val);
 83            Set<String> matchingWords = this.getKeysByValue(words, value);
 84            for(String word : matchingWords) {
 85                System.out.println(val + " = " + word);
 86            }
 87        }
 88    }
 89
 90    /**
 91     * Read in a file and store in a Map
 92     */
 93    private void createWordlist() {
 94        File dictFile = new File(System.getProperty("user.dir")+"\\dict\\dict.txt");
 95        if(dictFile.exists()) {
 96            createWordList(dictFile);
 97        }
 98    }
 99
100    /**
101     * Utility method to get all the matching Keys of a Map by the given value
102     *
103     * @param <K> the key object
104     * @param <V> the value object
105     * @param map a Map to search through
106     * @param value the value to search for
107     *
108     * @return a set of keys which match the given value
109     */
110    private <K, V> Set<K> getKeysByValue(Map<K, V> map, V value) {
111         Set<K> keys = new HashSet<K>();
112         for (Entry<K, V> entry : map.entrySet()) {
113             if (entry.getValue().equals(value)) {
114                 keys.add(entry.getKey());
115             }
116         }
117         return keys;
118    }
119
120    /**
121     * Method to populate a Map of words and values from a given word list file
122     *
123     * @param file pointer to the word list file
124     */
125    private void createWordList(File file) {
126        BufferedReader bufferedStream = null;
127        try {
128            bufferedStream = new BufferedReader(
129                             new InputStreamReader(
130                             new FileInputStream(file)));
131            String line = "";
132            while((line = bufferedStream.readLine()) != null) {
133                words.put(line, getWordValue(line));
134            }
135        } catch (IOException e) {
136            e.printStackTrace();
137        } finally {
138            if(bufferedStream != null) {
139                try {
140                    bufferedStream.close();
141                } catch (IOException e) {
142                    e.printStackTrace();
143                }
144            }
145        }
146    }
147
148    /**
149     * Method to return the numeric value for a given word
150     *
151     * @param word a {@link java.lang.String} containing the word
152     * @return an int representing the words numeric value
153     */
154    private int getWordValue(String word) {
155        int returnable = 0;
156        char[] chars = word.toCharArray();
157        for(char theChar : chars) {
158            Integer charValue = null;
159            switch(useCharset) {
160                case ASCII:
161                    charValue = asciiletters.get(theChar);
162                break;
163                case UNICODE:
164                    charValue = Character.getNumericValue(theChar);
165                break;
166                case POSITIONAL:
167                    charValue = letters.get(Character.toLowerCase(theChar));
168                break;
169                default:
170                break;
171            }
172            if(charValue != null) {
173                returnable = returnable + charValue;
174            }
175        }
176        return returnable;
177    }
178}

I had created a program that would create a list of words that are ‘numerically equivalent’ to a given input, this was good but had a couple of downsides, as the input length grew the number of matches decreased as it was only matching to single words, also the appropriateness of results decreased to a point where the results list is just scientific terms. To demonstrate the downsides here are some of the words that are generated using the code above:

 1Happiness = advisable
 2Happiness = firebreak
 3Happiness = mechanics
 4Happy Employees = encephaloscope
 5Happy Employees = modificability
 6Happy Employees = archaeogeology
 7Eternal Happiness = archecclesiastic
 8Eternal Happiness = hypophosphorous
 9Eternal Happiness = anatomicomedical
10Creative Happiness = hystricomorphous
11Creative Happiness = hysteroproterize
12Perpetual Happiness = micromineralogical
13Boundless Creativity = facioscapulohumeral
14Boundless Creativity = bacteriotherapeutic

Feeling slightly like the idea was good but the results currently weren’t I delivered the word lists to my friend and went on holiday…