On Sun, Apr 29, 2001 at 04:12:32PM +0200, [EMAIL PROTECTED] wrote:
> well, i use 12 bit now.  and even before fortunes2 is
> scanned completely, i have 26255 words distributed over
> 3876 hashes (i need 4096 -- 12 bit)

I hacked together this Java program to hash the words in 
/usr/share/dict/words. On my system there are words for all 
12-bit hashes, even after filtering out proper nouns. Most 
hashes seem to offer several choices (906 only offers one).

The program takes one argument, a number between 0 and 4095, 
and prints a list of words that hash to that number. Eg 1023:

binary
conduciveness
descendent
embarked
incongruity
neighborly
potash
sensory
import java.util.Vector;
import java.util.Iterator;
import java.io.InputStream;
import java.io.FileInputStream;

class HashHack
{
	private static int MAX = 4096;

	public static void main (String[] args)
	{
		InputStream in;
		String s;
		byte[] buf, cp;
		Vector[] hash;
		Iterator it;
		int a = 0, b = 0, c = 0, lookup = 0;

		if (args.length != 1) usage();
		try
		{
			lookup = new Integer(args[0]).intValue();
		}
		catch (NumberFormatException grr)
		{
			usage();
		}
		if (lookup < 0 || lookup >= MAX) usage();
		hash = new Vector [MAX];
		for (int i = 0; i < MAX; i++) hash[i] = new Vector();
		try
		{
			in = new FileInputStream ("/usr/share/dict/words");
			buf = new byte [100];
			while (b != -1)
			{
				b = in.read();
				if (b == '\n' || b == -1)
				{
					if (a > 0)
					{
						cp = new byte [a];
						for (int i = 0; i < a; i++)
							cp[i] = buf[i];
						a = 0;
						s = new String (cp, "US-ASCII");
						c = s.hashCode();
						if (c < 0) c = 0 - c;
						hash[c % MAX].add (s);
					}
					else continue;
				}
				else if (b >= 'A' && b <= 'Z')
				{
					while (b != -1)
					{
						b = in.read();
						if (b == '\n' || b == -1)
							break;
					}
					a = 0;
				}
				else buf[a++] = (byte) b;
			}
			in.close();
		}
		catch (Exception x)
		{
			System.out.println (x.toString());
			System.exit (-1);
		}
/*
		for (int i = 0; i < MAX; i++)
			if (hash[i].size() == 0)
				System.out.println ("No words hash to " + i);
*/
		it = hash[lookup].iterator();
		while (it.hasNext())
			System.out.println ((String)it.next());
	}

	private static void usage()
	{
		System.out.println ("Usage: HashHack <n>");
		System.out.println ("Where n is a number between 0 and 4095");
		System.exit (-1);
	}
}

Reply via email to