Thanks! Here's the Java version of the poem generator (same dictionaries
as before).

Cheers,
Michael

Volodya wrote:
> Michael Rogers wrote:
>> Matthew Toseland wrote:
>>> Base32 this is 35 chars; the base32 may be easier, and is certainly 
>>> shorter, 
>>> but this would be a nice option if you port the code to java. :)
>> Yeah, base32 would definitely be more practical but I just love the idea
>> of using random poetry to set up a darknet. :-) I'll have a crack at
>> porting it to Java when I get a minute.
>>
>> Cheers,
>> Michael
> 
> This is something to start with. It assumes to find the words in the 
> "words-#.dic" files where # represents the
> order of the words... so its probably not as clean as you'd want it to be.
> 
> Also i think the problem would be with loading the file into the memory, when 
> it will get large enough to be
> representing the true key i think you'll be having some serious out of memory 
> issues.
> 
>                           - Volodya
> 
> 
> 
> ------------------------------------------------------------------------
> 
> import java.io.BufferedReader;
> import java.io.FileReader;
> 
> import java.io.FileNotFoundException;
> import java.io.IOException;
> 
> public class PoemKey {
>       public static int WORDS_PER_KEY = 4;
>       public static int BYTES_PER_WORD = 2;
>       
>       private static String[][] words;
>       
>       static
>       {
>               words = new String[WORDS_PER_KEY][];
>               final int numOfWords = (int)Math.pow(2, BYTES_PER_WORD * 8);
>               
>               for(int i=0; i<WORDS_PER_KEY; i++)
>               {
>                       words[i] = new String[numOfWords];
>                       try
>                       {
>                               BufferedReader fin = new BufferedReader(new 
> FileReader("words-"+i+".dic"));
>                               for(int j=0; j < numOfWords; j++)
>                               {
>                                       words[i][j] = fin.readLine();
>                               }
>                               fin.close();
>                       }
>                       catch(FileNotFoundException e)
>                       {
>                               //Well i don't really know what you'd want to 
> do then.
>                       }
>                       catch(IOException e)
>                       {
>                               //Well i don't really know what you'd want to 
> do then.
>                       }
>               }
>               
>       }
>       
>       public static int[] poemToKey(String poem[])
>       {
>               int result[] = new int[WORDS_PER_KEY];
>               
>               return result;
>       }
>       
>       public static String[] keyToPoem(int[] key)
>       {
>               String result[] = new String[WORDS_PER_KEY];
>               
>               for(int i=0; i<WORDS_PER_KEY; i++)
>               {
>                       result[i] = words[i][key[i]];
>               }
>               
>               return result;
>       }
>       
>       public static void main(String args[])
>       {
>               int key[] = new int[WORDS_PER_KEY];
>               
>               System.out.print("The random key with "+WORDS_PER_KEY+" blocks 
> "+BYTES_PER_WORD+" bytes each is: ");
>               
>               for(int i=0; i<WORDS_PER_KEY; i++)
>               {
>                       key[i]=(int)(Math.random()*8*BYTES_PER_WORD);
>                       System.out.print(Integer.toHexString(key[i])+" ");
>               }
>               
>               System.out.println("");
>               
>               System.out.print("The 'Poem' key is: ");
>               String[] poem = keyToPoem(key);
>               for(int i=0; i<WORDS_PER_KEY; i++)
>               {
>                       System.out.print(poem[i]+" ");
>               }
>               System.out.println("");
>       }
> }
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Devl mailing list
> [email protected]
> http://emu.freenetproject.org/cgi-bin/mailman/listinfo/devl

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

class BasicEnglish
{
	int bits;
	ArrayList<String> n, vi, vt, adj, adv, p, art;
	
	public BasicEnglish (int b)
	{
		bits = b;
		try {
			n = readFile ("basic-nouns");
			vi = readFile ("basic-verbs-i");
			vt = readFile ("basic-verbs-t");
			adj = readFile ("basic-adjectives");
			adv = readFile ("basic-adverbs");
			p = readFile ("basic-prepositions");
			art = readFile ("basic-articles");
		}
		catch (Exception e) {
			System.err.println (e);
			System.exit (1);
		}
	}
	
	ArrayList<String> readFile (String name) throws Exception
	{
		BufferedReader in = new BufferedReader (new FileReader (name));
		ArrayList<String> words = new ArrayList<String>();
		String word = in.readLine();
		while (word != null) {
			words.add (word);
			word = in.readLine();
		}
		in.close();
		return words;
	}
	
	int rand (int range)
	{
		bits -= (int) (Math.log (range) / Math.log (2));
		return (int) (Math.random() * (range));
	}
	
	void printWord (ArrayList<String> array)
	{
		System.out.print (array.get (rand (array.size())) + " ");
	}
	
	void printNounPhrase()
	{
		printWord (art);
		if (rand (1) == 0) printWord (adj);
		printWord (n);
	}
	
	void printSentence()
	{
		printNounPhrase(); // Subject
		if (rand (1) == 0) printWord (vi); // Intransitive verb
		else {
			printWord (vt); // Transitive verb
			printNounPhrase(); // Object of transitive verb
		}
		if (rand (1) == 0) printWord (adv); // Adverb
		if (rand (1) == 0) {
			printWord (p); // Preposition
			printNounPhrase(); // Object of preposition
		}
		System.out.println();
	}
	
	void printPoem()
	{
		while (bits > 0.0) printSentence();
	}
	
	public static void main (String[] args)
	{
		if (args.length != 1) return;
		int b = Integer.parseInt (args[0]);
		new BasicEnglish(b).printPoem();
	}
}
_______________________________________________
Devl mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/devl

Reply via email to