Advertisement
Guest User

RandomWriter

a guest
Jan 14th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class RandomWriter {
  5.  
  6.     public static void main(String[] args) {
  7.         Random r = new Random();
  8.         String sentence = "Mornings before daylight I slipped into cornfields and borrowed a watermelon, or a mushmelon, or a punkin, or some new corn, or things of that kind.  Pap always said it warn't no harm to borrow things if you was meaning to pay them back some time; but the widow said it warn't anything but a soft name for stealing, and no decent body would do it.  Jim said he reckoned the widow was partly right and pap was partly right; so the best way would be for us to pick out two or three things from the list and say we wouldn't borrow them any more—then he reckoned it wouldn't be no harm to borrow the others.  So we talked it over all one night, drifting along down the river, trying to make up our minds whether to drop the watermelons, or the cantelopes, or the mushmelons, or what.  But towards daylight we got it all settled satisfactory, and concluded to drop crabapples and p'simmons.  We warn't feeling just right before that, but it was all comfortable now.  I was glad the way it come out, too, because crabapples ain't ever good, and the p'simmons wouldn't be ripe for two or three months yet. We shot a water-fowl now and then that got up too early in the morning or didn't go to bed early enough in the evening.  Take it all round, we lived pretty high. The fifth night below St. Louis we had a big storm after midnight, with a power of thunder and lightning, and the rain poured down in a solid sheet. We stayed in the wigwam and let the raft take care of itself. When the lightning glared out we could see a big straight river ahead, and high, rocky bluffs on both sides.  By and by says I, \"Hel-lo, Jim, looky yonder!\" It was a steamboat that had killed herself on a rock.  We was drifting straight down for her.  The lightning showed her very distinct.  She was leaning over, with part of her upper deck above water, and you could see every little chimbly-guy clean and clear, and a chair by the big bell, with an old slouch hat hanging on the back of it, when the flashes come. Well, it being away in the night and stormy, and all so mysterious-like, I felt just the way any other boy would a felt when I see that wreck laying there so mournful and lonesome in the middle of the river.  I wanted to get aboard of her and slink around a little, and see what there was there.  So I says:";
  9.         int k = r.nextInt(7) + 1;
  10.         int length = 10;
  11.        
  12.         String seed = getSeed(sentence);
  13.         String word = buildWord(seed, k, sentence);
  14.        
  15.         for (int i = 0; i < length; i++) {
  16.             seed = getSeed(sentence);
  17.             word = buildWord(seed, k, sentence);
  18.             System.out.print(word + " ");
  19.         }
  20.         System.out.println(".");
  21.     }
  22.  
  23.     private static String buildWord(String seed, int k, String sentence) {
  24.         String holder = seed;
  25.         for (int i = 0; i < k; i++) {
  26.             holder += nextBestLetter(holder, sentence);
  27.         }
  28.         return holder;
  29.     }
  30.  
  31.     private static String nextBestLetter(String holder, String sentence) {
  32.         String winner = "";
  33.         Map<String, Integer> mapOfCandidates = new HashMap<String, Integer>();
  34.         for (int i = 0; i < (sentence.length() - holder.length() - 1); i++) {
  35.             if (sentence.substring(i, i + holder.length()).equals(holder) && !mapOfCandidates.containsKey(sentence.substring(i + holder.length() + 1))) {
  36.                 mapOfCandidates.put(sentence.substring(i + holder.length() + 1, i + holder.length() + 2), 1);
  37.             }
  38.             else if (mapOfCandidates.containsKey(sentence.substring(i + holder.length() + 1))) {
  39.                 mapOfCandidates.put(sentence.substring(i + holder.length() + 1, i + holder.length() + 2), mapOfCandidates.get(sentence.substring(i + holder.length() + 1)) + 1);
  40.             }
  41.         }
  42.         for (String s : mapOfCandidates.keySet()) {
  43.             int max = 0;
  44.             if (mapOfCandidates.get(s) > max) {
  45.                 max = mapOfCandidates.get(s);
  46.                 winner = s;
  47.             }
  48.         }
  49.         return winner;
  50.     }
  51.  
  52.     private static String getSeed(String sentence) {
  53.         Random r = new Random();
  54.         int index = r.nextInt(sentence.length());
  55.         String holder = sentence.substring(index, index + 1);
  56.         while (holder.contains(" ") || holder.contains(".") || holder.contains(",") || holder.contains("\"")) {
  57.             index = r.nextInt(sentence.length());
  58.             holder = sentence.substring(index, index + 1);
  59.         }
  60.         return holder;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement