// Simple File Input and Output using "new" I/O // Daniel Shiffman // Programming from A to Z, Spring 2006 // Based off of code from Java Regular Expressions by Mehran Habibi // Input file is the first argument passed from the command line // Output file is the second // This could be improved with some basic error handling (what if an invalid filename is entered, etc.?) import java.io.*; import java.nio.*; import java.nio.channels.*; import java.util.StringTokenizer; public class WordUp { public static void main (String[] args) throws IOException { int totalSentences = 0; String sentenceDelim = ":"; ////////////////////////////////////////////////////////////////// // Create an input stream and file channel // Using first arguemnt as file name to read in int sentencesJay = 0; FileInputStream fisJay = new FileInputStream("JayZ-99Problems.txt"); FileChannel fcJay = fisJay.getChannel(); ByteBuffer bbJay = ByteBuffer.allocate((int)fcJay.size()); fcJay.read(bbJay); fcJay.close(); // Convert ByteBuffer to one long String String contentJay = new String(bbJay.array()); sentencesJay = 0; StringTokenizer sentenceTokenizerJay = new StringTokenizer(contentJay,sentenceDelim); sentencesJay = sentenceTokenizerJay.countTokens(); String[] lyricLinesJay; lyricLinesJay = new String[sentencesJay]; int lyricCountJay = 0; while (sentenceTokenizerJay.hasMoreTokens()) { String lyricJay = sentenceTokenizerJay.nextToken(); lyricLinesJay[lyricCountJay] = "Jay: " + lyricJay; lyricCountJay++; } totalSentences = totalSentences + sentencesJay; ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Create an input stream and file channel // Using first arguemnt as file name to read in int sentencesDela = 0; FileInputStream fisDela = new FileInputStream("DelaSoul-TheGrindDate.txt"); FileChannel fcDela = fisDela.getChannel(); ByteBuffer bbDela = ByteBuffer.allocate((int)fcDela.size()); fcDela.read(bbDela); fcDela.close(); // Convert ByteBuffer to one long String String contentDela = new String(bbDela.array()); sentencesDela = 0; StringTokenizer sentenceTokenizerDela = new StringTokenizer(contentDela,sentenceDelim); sentencesDela = sentenceTokenizerDela.countTokens(); String[] lyricLinesDela; lyricLinesDela = new String[sentencesDela]; int lyricCountDela = 0; while (sentenceTokenizerDela.hasMoreTokens()) { String lyricDela = sentenceTokenizerDela.nextToken(); lyricLinesDela[lyricCountDela] = "DeLa: " + lyricDela; lyricCountDela++; } totalSentences = totalSentences + sentencesDela; ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Create an input stream and file channel // Using first arguemnt as file name to read in int sentencesKanye = 0; FileInputStream fisKanye = new FileInputStream("KanyeWest-HeardEmSay.txt"); FileChannel fcKanye = fisKanye.getChannel(); ByteBuffer bbKanye = ByteBuffer.allocate((int)fcKanye.size()); fcKanye.read(bbKanye); fcKanye.close(); // Convert ByteBuffer to one long String String contentKanye = new String(bbKanye.array()); sentencesKanye = 0; StringTokenizer sentenceTokenizerKanye = new StringTokenizer(contentKanye,sentenceDelim); sentencesKanye = sentenceTokenizerKanye.countTokens(); String[] lyricLinesKanye; lyricLinesKanye = new String[sentencesKanye]; int lyricCountKanye = 0; while (sentenceTokenizerKanye.hasMoreTokens()) { String lyricKanye = sentenceTokenizerKanye.nextToken(); lyricLinesKanye[lyricCountKanye] = "Kanye: " + lyricKanye; lyricCountKanye++; } totalSentences = totalSentences + sentencesKanye; ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Create an input stream and file channel // Using first arguemnt as file name to read in int sentencesFatBoy = 0; FileInputStream fisFatBoy = new FileInputStream("FatBoys-AllYouCanEat.txt"); FileChannel fcFatBoy = fisFatBoy.getChannel(); ByteBuffer bbFatBoy = ByteBuffer.allocate((int)fcFatBoy.size()); fcFatBoy.read(bbFatBoy); fcFatBoy.close(); // Convert ByteBuffer to one long String String contentFatBoy = new String(bbFatBoy.array()); sentencesFatBoy = 0; StringTokenizer sentenceTokenizerFatBoy = new StringTokenizer(contentFatBoy,sentenceDelim); sentencesFatBoy = sentenceTokenizerFatBoy.countTokens(); String[] lyricLinesFatBoy; lyricLinesFatBoy = new String[sentencesFatBoy]; int lyricCountFatBoy = 0; while (sentenceTokenizerFatBoy.hasMoreTokens()) { String lyricFatBoy = sentenceTokenizerFatBoy.nextToken(); lyricLinesFatBoy[lyricCountFatBoy] = "FatBoy: " + lyricFatBoy; lyricCountFatBoy++; } totalSentences = totalSentences + sentencesJay; ////////////////////////////////////////////////////////////////// //Combine Arrays together String[] lyricLinesAll; lyricLinesAll = new String[totalSentences]; int lyricMarker = 0; //Jay Array for(int i = 0; i < lyricLinesJay.length; i++ ) { lyricLinesAll[lyricMarker] = lyricLinesJay[i]; lyricMarker++; } //FatBoy Array for(int i = 0; i < lyricLinesFatBoy.length; i++ ) { lyricLinesAll[lyricMarker] = lyricLinesFatBoy[i]; lyricMarker++; } //Kanye Array for(int i = 0; i < lyricLinesKanye.length; i++ ) { lyricLinesAll[lyricMarker] = lyricLinesKanye[i]; lyricMarker++; } //Dela Array for(int i = 0; i < lyricLinesDela.length; i++ ) { lyricLinesAll[lyricMarker] = lyricLinesDela[i]; lyricMarker++; } System.out.println("WORD UP - A DIALOGUE IN HIP HOP\n\n"); for(int i = 0; i < 20; i++ ) { int randomNum = (int) (Math.random() * totalSentences); System.out.println(lyricLinesAll[randomNum] + "\n"); } String report = ""; report += "Total Sentences: " + totalSentences + "\n"; System.out.println(report); // Conceivably we would now mess with the string here // Doing all sorts of fun stuff // Create an output stream and file channel // Using second argument as file name to write out //FileOutputStream fos = new FileOutputStream("WordUp.txt"); //FileChannel outfc = fos.getChannel(); // Convert content String into ByteBuffer and write out to file //ByteBuffer bb = ByteBuffer.wrap(content.getBytes()); //outfc.write(bb); //outfc.close(); } // A method to count the number of syllables in a word // Pretty basic, just based off of the number of vowels // This could be improved public static int countSyllables(String word) { int syl = 0; boolean vowel = false; int length = word.length(); //check each word for vowels (don't count more than one vowel in a row) for(int i=0; i < length ; i++) { if (isVowel(word.charAt(i)) && (vowel==false)) { vowel = true; syl++; } else if (isVowel(word.charAt(i)) && (vowel==true)) { vowel = true; } else { vowel = false; } } char tempChar = word.charAt(word.length()-1); //check for 'e' at the end, as long as not a word w/ one syllable if (((tempChar == 'e') || (tempChar == 'E')) && (syl != 1)) { syl--; } return syl; } //check if a char is a vowel (count y) public static boolean isVowel(char c) { if ((c == 'a') || (c == 'A')) { return true; } else if ((c == 'e') || (c == 'E')) { return true; } else if ((c == 'i') || (c == 'I')) { return true; } else if ((c == 'o') || (c == 'O')) { return true; } else if ((c == 'u') || (c == 'U')) { return true; } else if ((c == 'y') || (c == 'Y')) { return true; } else { return false; } } }