?

Log in

No account? Create an account
 
 
06 November 2006 @ 03:42 pm
NaNoWriMo the Lazy Geek's Way  
50,000 words. That's the goal, ain't it? Well, I'm done, then. And you can be, too! Just find a really big text file, take the code behind the cut, and build/run it.


NOTE: You have to replace the words LESS THAN in the for-loop with the actual less than sign. LJ tries to interpret the less-than sign and everything after it as a tag, hence I had to take it out.

import java.io.*;

public class nanowrimo
{
// ** INSTANCE VARIABLES **
// Source of Nano text and file to write Nano to
private String inSource = "C:\\nano\\fulltext.txt";
private String outSource = "C:\\nano\\nano.txt";

// String that will temporarily hold lines from the source file
private String aLine;

// Array that will hold the broken-down contents of source file
private int arraySize = 6000;
String[] storyLines = new String[arraySize];

// Array that will hold individual words from a given line of text
String[] words;

// Word counter
private int numWords = 50000;


// ** CONSTRUCTOR **
// Where all the magic will happen, because I'm lazy
public nanowrimo()
{
// Step One: Pull out lines of text from the source file
try
{
// Create a reader that will access the source file
BufferedReader reader = new BufferedReader(new FileReader(inSource));

// Read in the contents of the input file until our array is full
int index = 0;
while (((aLine = reader.readLine()) != null) && (index < arraySize))
{
storyLines[index] = aLine;
index++;
}

// Done with the input file, so let's be good programmers and close it up
reader.close();
}
catch (Exception ex)
{
// If something goes wrong, lemme know and show me the error
System.out.println("ERROR READING FILE");
ex.printStackTrace();
}

// Step Two: Compose our Nano!
try
{
// Create a writer to the destination file
BufferedWriter writer = new BufferedWriter(new FileWriter(outSource));

// Now, here's where the magic happens... 50,000 times
int randomNum;
int wordSize;
for (int x=0; x LESS THAN numWords; x++)
{
// Pick a random number between 0 and the size of our array minus one-
// i.e., select a random line of text
randomNum = (int) (Math.random() * (arraySize - 1));

// Split the random line of text on spaces, and dump the resulting words into our word array
words = storyLines[randomNum].split(" ");

// Figure out how many words we have
wordSize = words.length;

// Pick a random number between 0 and the number of words minus one-
// i.e., select a random word
randomNum = (int) (Math.random() * (wordSize - 1));

// Write the random word to our Nano!
writer.write(words[randomNum]);
writer.write(" ");

// Clean everything up for next time
words = null;
randomNum = -1;
}

// Done with the writer, so we should close it!
writer.close();
}
catch (Exception ex)
{
System.out.println("ERROR WRITING FILE");
ex.printStackTrace();
}
}

// ** MAIN PROGRAM **
public static void main(String[] args)
{
// Create a nanowrimo instance and let the constructor do its thing
nanowrimo NNWM = new nanowrimo();
}
}



The output by no means makes sense, but coherency is not a stated goal of NaNoWriMo. Word count is.
 
 
 
a stunning paragon of nomenclaturesemperar on November 6th, 2006 10:08 pm (UTC)
you're a bad person.
How Random Babbling Becomes Corporate Policy: Terroristt3knomanser on November 6th, 2006 11:38 pm (UTC)
Wait'll mine's done- it'll take a bit longer, because I'm writing a program that'll grab a random sentence from Wikipedia (thus ensuring I get grammatically correct (mostly) sentences).
Miusheri: gotevil?miusheri on November 7th, 2006 12:07 am (UTC)
One must do something to preserve sanity in the wake of eighteen daily posts on one's friends page along the lines of, "OMG 500 WURDZ SHORT 2DAY O NOES MY LIEF IZ OVUR"
Chia de Bosk: Ferret Shockchiasmushf on November 7th, 2006 01:51 pm (UTC)
Funniest. Comment. EVAR.

(Funniest I've read in the last 10 minutes, anyway.)
Miusheri: girmiusheri on November 7th, 2006 01:55 pm (UTC)
Thank you! I aim to please! ;)