Miusheri (miusheri) wrote,
Miusheri
miusheri

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.
Tags: geek, programming
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 5 comments