Avatar

Random sentence generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl

use LWP::Simple;
$url = "http://suggestqueries.google.com/complete/search?output=firefox&qu=";
$word = $ARGV[0];
for (1..25) {
    $content = get($url . $word);
    @matches = $content =~ /\"([^\"]+\\s)(\\w+)\"/g;
    last if not @matches;
    $x = int(rand(@matches / 2));
    $line .= $matches[2 * $x];
    $word = $matches[2 * $x + 1];
}
print $line . "\n";

Refactorings

No refactoring yet !

C9bdcef9013e8caf8bea1ed177522568

James Smith

September 28, 2007, September 28, 2007 00:56, permalink

No rating. Login to rate!

This is a good example of using eval to evaluate the response from Google as in this
case JSON and perl arrays are represented in the same way! Also uses perl grep to
pick out the matches... I know I really like Perl regexps - but they are just not the
way to go to parse structured strings..!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl

use strict;
use LWP::Simple;
my $URL = 'http://suggestqueries.google.com/complete/search?output=firefox&qu=';
     # Returns a JSON array of arrays, just the same as a perl array of arrays!
my $word = $ARGV[0] || die "usage ./script.pl WORD\n";

for( 1..25 ) {
  my $results = eval get "$url$word";         # returns an arrayref - matches in [1]
  my @matches = grep { /\s/ } @{$r->[1]};     # grab only multi-word responses
  last unless @matches;                       # end if no multi-word answers
  $matches[@matches*rand] =~ /^(.*\s)(\w+)$/; # split off last word
  print $1;                                   # print out the first half of the match
  $word = $2;                                 # replace word with second half!
}
print "\n";
008b3d87ecb72e599c3559f7442b4faf

anus

September 28, 2007, September 28, 2007 02:21, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
use LWP::Simple;
$word = $ARGV[0];
for (1..25) {
    $content = get("http://suggestqueries.google.com/complete/search?output=firefox&qu=" . $word);
    @matches = $content =~ /\"([^\"]+\\s)(\\w+)\"/g;
    last if not @matches;
    $x = int(rand(@matches / 2));
    print $matches[2 * $x];
    $word = $matches[2 * $x + 1];
}
Fd1e01e82ec664971379d333a6e1088e

Relipuj

September 28, 2007, September 28, 2007 03:46, permalink

No rating. Login to rate!

Your code doesnt work, so i'm not sure what you're trying to do...
If you give me some explanations, i could perhaps help more...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/perl

use LWP::UserAgent;
my $browser = LWP::UserAgent->new( agent => 'Mozilla' );        # Google needs a UserAgent header != LWP

my $url = 'http://suggestqueries.google.com/complete/search?output=firefox&qu=';

while (<DATA>) {
	chomp;
	my @matches;
	my $retries = 3;

	while ($retries) {
		my $response = $browser->get("$url$_");
		die $response->status_line unless $response->is_success;        # Error checking...

		my $content = $response->content;
		push @matches, $content =~ /"([^\"]+)"/g;        # Capture the strings between quotes (doesnt respect the google answer)
		shift @matches;
		last unless @matches;

		print "\n\n>>>>>> $url$_", "\n<<<<$content", "\n::", join ( " -- ", @matches );

		$_ = $matches[ int rand $#matches ];
		$retries--;
	} ## end while ($retries)
} ## end while (<DATA>)

__DATA__
angelina jolie
car
expert
flowers
perl
programmer
B8d457d2c39911ea4c74ba7d66b9c3f7

Marco Valtas

September 30, 2007, September 30, 2007 12:52, permalink

No rating. Login to rate!

As Relipuj I didn't know what is the specs you want. I've just made your code work cause your regular expression had a little problem.

My fix just fill up the "@matches" array with the words returned by google. First I remove "[","]" and ", then I split the line by ",". This a simple fix and is not a refactor, since refactor implies a working code to begin with.

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
use LWP::Simple;
$url = "http://suggestqueries.google.com/complete/search?output=firefox&qu=";
$word = $ARGV[0];
for (1..25) {
    $content = get($url . $word);
    $content =~ s/[\[|\]|"]//g;
    @matches = split(/,/,$content); 
    last if not @matches;
    $x = int(rand(@matches / 2));
    $line .= $matches[2 * $x];
    $word = $matches[2 * $x + 1];
}
print $line . "\n";
Avatar

Andrew Moore

September 30, 2007, September 30, 2007 15:49, permalink

No rating. Login to rate!

strict, warnings, working regex, no eval, better variable names.
This could probably stand to have some documentation and some usage information to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl

use strict;
use warnings;

use LWP::Simple;

my $url = "http://suggestqueries.google.com/complete/search?output=firefox&qu=";
my $word = $ARGV[0];

my $wordcount = 25;
my @foundwords;

for ( 1..$wordcount ) {
    my $suggestedwords = get($url . $word);
    my @matches = $suggestedwords =~ /"([\w\s]+)"/xmg;
    shift @matches;  # the first match is the word that we passed in. shift it off.
    last if not @matches;
    my $randomindex = int( rand( scalar @matches ) );
    push @foundwords, $matches[ $randomindex ];
    $word = $matches[ ( $randomindex + 1 ) % ( scalar @matches )]; # the next one, wrapped.
}

print join( ' ', @foundwords ) . "\n";
Avatar

TrendyWhistle

February 12, 2008, February 12, 2008 14:07, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<td>
  




 <area shape="rect" coords="29,24,112,43" alt="Information about me and my services" href="/Clients/"/>
 <area shape="rect" coords="29,54,112,74" alt="Download free programs" href="/Downloads/"/>
 <area shape="rect" coords="164,24,343,93" alt="Goto homepage of Jeroen Kessels, internet engineer" href="/"/>

 <area shape="rect" coords="396,24,481,43" alt="Hobby area" href="/Hobby/"/>
 <area shape="rect" coords="396,54,481,73" alt="Email, address, and phone information" href="/Address/"/>
 
<map name="map2">
 <area shape="rect" coords="114,34,242,54" alt="Copyright 2005 J.C. Kessels" href="/"/>
 </map>
<map name="map3">
 <area shape="rect" coords="53,34,107,54" alt="Goto homepage of Jeroen Kessels, internet engineer" href="/"/>
 <area shape="rect" coords="123,24,163,64" alt="Email, address, and phone information" href="/Address/"/>
 </map>

<center>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
  <td><img border="0" width="76" src="/img/Top1.gif" height="107"/></td>
  <td width="100%" background="/img/Top2.gif" align="center"><img border="0" src="/img/Top3.gif" height="107"/></td>
  <td><img border="0" width="77" src="/img/Top4.gif" height="107"/></td>
  </tr>
</table>
</center>


<table border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>

  
  <td width="100%">
    <br/>
    <center>
    
    <font face="Verdana" color="#000000" size="+2"><b></b></font><br/>
    <img border="0" width="462" src="/img/Bar02.gif" height="20"/><p>
    </p></center>

    <font face="Verdana">
<script language="JavaScript">
<!--
// The functions 'qrand' and 'qinit' below are my random number generator.
// You must call qinit() once at the beginning of your script's execution.
// Then call qrand(n) to generate a random number from 0 to n-1.

function qrand(n) {
  RandSeed = (RandMultiplier * RandSeed + RandIncrement) % 0x7fffffff
  return (RandSeed >> 16) % n
}


function qinit() {
  RandMultiplier = 0x015a4e35
  RandIncrement = 1


  // Initialize using the computer's date and time...
  var now = new Date()
  RandSeed = now.getTime() % 0xffffffff
  FirstSentence = 1
  FirstAmerica = 1
}


function GenRandomSentenceTemplate() {
  // code key:
  //    0 = lone noun
  //    1 = noun phrase
  //    2 = transitive verb phrase (present tense, singular, third person)
  //    3 = conjunction
  //    4 = intransitive verb phrase
  //    5 = transitive verb phrase (infinitive, singular)
  //    6 = adjective
  //    7 = adverb
  var w = ""
  var n = 17
  var r = qrand(n+5)
  if ( r > n )    w = "1 2 1."
  else if ( r == 1 )  w = "1 2 1, 3 1 2 1."
  else if ( r == 2 )  w = "When 1 4, 1 4."
  else if ( r == 3 )  w = "If 1 2 1, then 1 4."
  else if ( r == 4 )  w = "Sometimes 1 4, but 1 always 2 1!"
  else if ( r == 5 )  w = "Most people believe that 1 2 1, but they need to remember how 7 1 4."
  else if ( r == 6 ) {
    if ( FirstAmerica ) {
      FirstAmerica = 0
      w = "1, 1, and 1 are what made America great!"
    } else {
      w = "1 2 1."
    }
  }
  else if ( r == 7 )  w = "1 4, 3 1 2 1."
  else if ( r == 8 )  w = "Now and then, 1 2 1."
  else if ( r == 9 )  w = "1 4, and 1 4; however, 1 2 1."
  else if ( r == 10 ) {
    if ( FirstSentence ) {
      w = "1 2 1."
    } else {
      w = "Indeed, 1 2 1."
    }
  }
  else if ( r == 11 ) {
    if ( FirstSentence ) {
      w = "1 2 1."
    } else {
      w = "Furthermore, 1 4, and 1 2 1."
    }
  }
  else if ( r == 12 ) {
    if ( FirstSentence ) {
      w = "1 2 1."
    } else {
      w = "For example, 1 indicates that 1 2 1."
    }
  }
  else if ( r == 13 ) w = "When you see 1, it means that 1 4."
  else if ( r == 14 ) w = "Any 0 can 5 1, but it takes a real 0 to 5 1."
  else if ( r == 15 ) w = "1 is 6."
  else if ( r == 16 ) w = "When 1 is 6, 1 2 1."
  FirstSentence = 0
  return w
}



function GenNoun() {
  var n = 130
  var r = qrand(n)
  if ( r == PrevNoun ) {
    r = qrand(n)
  }
  PrevNoun = r
  var w = ""
  if ( r == 0 )   w = "cocker spaniel"
  else if ( r == 1 )  w = "roller coaster"
  else if ( r == 2 )  w = "abstraction"
  else if ( r == 3 )  w = "pine cone"
  else if ( r == 4 )  w = "microscope"
  else if ( r == 5 )  w = "bottle of beer"
  else if ( r == 6 )  w = "bowling ball"
  else if ( r == 7 )  w = "grain of sand"
  else if ( r == 8 )  w = "wheelbarrow"
  else if ( r == 9 )  w = "pork chop"
  else if ( r == 10 ) w = "bullfrog"
  else if ( r == 11 ) w = "squid"
  else if ( r == 12 ) w = "tripod"
  else if ( r == 13 ) w = "girl scout"
  else if ( r == 14 ) w = "light bulb"
  else if ( r == 15 ) w = "hole puncher"
  else if ( r == 16 ) w = "carpet tack"
  else if ( r == 17 ) w = "submarine"
  else if ( r == 18 ) w = "diskette"
  else if ( r == 19 ) w = "tape recorder"
  else if ( r == 20 ) w = "anomaly"
  else if ( r == 21 ) w = "insurance agent"
  else if ( r == 22 ) w = "mortician"
  else if ( r == 23 ) w = "fire hydrant"
  else if ( r == 24 ) w = "photon"
  else if ( r == 25 ) w = "line dancer"
  else if ( r == 26 ) w = "paper napkin"
  else if ( r == 27 ) w = "stovepipe"
  else if ( r == 28 ) w = "graduated cylinder"
  else if ( r == 29 ) w = "hydrogen atom"
  else if ( r == 30 ) w = "garbage can"
  else if ( r == 31 ) w = "reactor"
  else if ( r == 32 ) w = "power drill"
  else if ( r == 33 ) w = "scooby snack"
  else if ( r == 34 ) w = "freight train"
  else if ( r == 35 ) w = "ocean"
  else if ( r == 36 ) w = "bartender"
  else if ( r == 37 ) w = "senator"
  else if ( r == 38 ) w = "mating ritual"
  else if ( r == 39 ) w = "briar patch"
  else if ( r == 40 ) w = "jersey cow"
  else if ( r == 41 ) w = "chain saw"
  else if ( r == 42 ) w = "prime minister"
  else if ( r == 43 ) w = "cargo bay"
  else if ( r == 44 ) w = "buzzard"
  else if ( r == 45 ) w = "polar bear"
  else if ( r == 46 ) w = "tomato"
  else if ( r == 47 ) w = "razor blade"
  else if ( r == 48 ) w = "ball bearing"
  else if ( r == 49 ) w = "fighter pilot"
  else if ( r == 50 ) w = "support group"
  else if ( r == 51 ) w = "fundraiser"
  else if ( r == 52 ) w = "cowboy"
  else if ( r == 53 ) w = "football team"
  else if ( r == 54 ) w = "cab driver"
  else if ( r == 55 ) w = "nation"
  else if ( r == 56 ) w = "ski lodge"
  else if ( r == 57 ) w = "mastadon"
  else if ( r == 58 ) w = "recliner"
  else if ( r == 59 ) w = "minivan"
  else if ( r == 60 ) w = "deficit"
  else if ( r == 61 ) w = "food stamp"
  else if ( r == 62 ) w = "wedding dress"
  else if ( r == 63 ) w = "fairy"
  else if ( r == 64 ) w = "globule"
  else if ( r == 65 ) w = "movie theater"
  else if ( r == 66 ) w = "tornado"
  else if ( r == 67 ) w = "rattlesnake"
  else if ( r == 68 ) w = "CEO"
  else if ( r == 69 ) w = "corporation"
  else if ( r == 70 ) w = "plaintiff"
  else if ( r == 71 ) w = "class action suit"
  else if ( r == 72 ) w = "judge"
  else if ( r == 73 ) w = "defendant"
  else if ( r == 74 ) w = "dust bunny"
  else if ( r == 75 ) w = "vacuum cleaner"
  else if ( r == 76 ) w = "lover"
  else if ( r == 77 ) w = "sandwich"
  else if ( r == 78 ) w = "hockey player"
  else if ( r == 79 ) w = "avocado pit"
  else if ( r == 80 ) w = "fruit cake"
  else if ( r == 81 ) w = "turkey"
  else if ( r == 82 ) w = "sheriff"
  else if ( r == 83 ) w = "apartment building"
  else if ( r == 84 ) w = "industrial complex"
  else if ( r == 85 ) w = "inferiority complex"
  else if ( r == 86 ) w = "salad dressing"
  else if ( r == 87 ) w = "short order cook"
  else if ( r == 88 ) w = "pig pen"
  else if ( r == 89 ) w = "grand piano"
  else if ( r == 90 ) w = "tuba player"
  else if ( r == 91 ) w = "traffic light"
  else if ( r == 92 ) w = "turn signal"
  else if ( r == 93 ) w = "paycheck"
  else if ( r == 94 ) w = "blood clot"
  else if ( r == 95 ) w = "earring"
  else if ( r == 96 ) w = "blithe spirit"
  else if ( r == 97 ) w = "customer"
  else if ( r == 98 ) w = "warranty"
  else if ( r == 99 ) w = "grizzly bear"
  else if ( r == 100 )  w = "cyprus mulch"
  else if ( r == 101 )  w = "pit viper"
  else if ( r == 102 )  w = "crank case"
  else if ( r == 103 )  w = "oil filter"
  else if ( r == 104 )  w = "steam engine"
  else if ( r == 105 )  w = "chestnut"
  else if ( r == 106 )  w = "chess board"
  else if ( r == 107 )  w = "pickup truck"
  else if ( r == 108 )  w = "cheese wheel"
  else if ( r == 109 )  w = "eggplant"
  else if ( r == 110 )  w = "umbrella"
  else if ( r == 111 )  w = "skyscraper"
  else if ( r == 112 )  w = "dolphin"
  else if ( r == 113 )  w = "asteroid"
  else if ( r == 114 )  w = "parking lot"
  else if ( r == 115 )  w = "demon"
  else if ( r == 116 )  w = "tabloid"
  else if ( r == 117 )  w = "particle accelerator"
  else if ( r == 118 )  w = "cloud formation"
  else if ( r == 119 )  w = "cashier"
  else if ( r == 120 )  w = "burglar"
  else if ( r == 121 )  w = "spider"
  else if ( r == 122 )  w = "cough syrup"
  else if ( r == 123 )  w = "satellite"
  else if ( r == 124 )  w = "scythe"
  else if ( r == 125 )  w = "canyon"
  else if ( r == 126 )  w = "polygon"
  else if ( r == 127 )  w = "crane"
  else if ( r == 128 )  w = "wedge"
  else if ( r == 129 )  w = "fraction"
  else if ( r == 130 )  w = "BrutalCarpet"
  else if ( r == 131 )  w = "TrendyWhistle"
  else if ( r == 132 )  w = "Bezner"
  else if ( r == 133 )  w = "Jared"
  else if ( r == 134 )  w = "Wesley"
  else if ( r == 135 )  w = "Your Mom"
  else if ( r == 136 )  w = "Motiki"
  return w
}



function GenPreposition() {
  var n = 14
  var r = qrand(n)
  var w = ""
  if ( r == 0 )   w = "of"
  else if ( r == 1 )  w = "from"
  else if ( r == 2 )  w = "near"
  else if ( r == 3 )  w = "about"
  else if ( r == 4 )  w = "around"
  else if ( r == 5 )  w = "for"
  else if ( r == 6 )  w = "toward"
  else if ( r == 7 )  w = "over"
  else if ( r == 8 )  w = "behind"
  else if ( r == 9 )  w = "beyond"
  else if ( r == 10 ) w = "related to"
  else if ( r == 11 ) w = "defined by"
  else if ( r == 12 ) w = "inside"
  else if ( r == 13 ) w = "living with"
  return w
}



function GenNounPhrase(depth) {
  var phraseKind = qrand(3)
  var s = ""
  if ( phraseKind == 0 || depth>0 ) {
    s = GenNoun()
  } else if ( phraseKind == 1 ) {
    s = GenAdjective() + " " + GenNoun()
  } else if ( phraseKind == 2 ) {
    s = GenNoun() + " " + GenPreposition() + " " + GenNounPhrase(depth+1)
  }
  var r = qrand(100)
  if ( r < 30 ) {
    s = "the " + s
  } else if ( r < 35 ) {
    s = "another " + s
  } else if ( r < 40 ) {
    s = "some " + s
  } else {
    var c = s.substring(0,1).toLowerCase()
    if ( (s.substring(0,8) != "Eurasian") &&
                 (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') ) {
      s = "an " + s
    } else {
      s = "a " + s
    }
  }
  return s
}



function GenAdverb() {
  var n = 28
  var r = qrand(n)
  if ( r == PrevAdverb ) {
    r = qrand(n)
  }
  PrevAdverb = r
  var s = ""
  if ( r == 0 )   s = "knowingly"
  else if ( r == 1 )  s = "slyly"
  else if ( r == 2 )  s = "greedily"
  else if ( r == 3 )  s = "hesitantly"
  else if ( r == 4 )  s = "secretly"
  else if ( r == 5 )  s = "carelessly"
  else if ( r == 6 )  s = "thoroughly"
  else if ( r == 7 )  s = "barely"
  else if ( r == 8 )  s = "ridiculously"
  else if ( r == 9 )  s = "non-chalantly"
  else if ( r == 10 ) s = "hardly"
  else if ( r == 11 ) s = "eagerly"
  else if ( r == 12 ) s = "feverishly"
  else if ( r == 13 ) s = "lazily"
  else if ( r == 14 ) s = "inexorably"
  else if ( r == 15 ) s = "accurately"
  else if ( r == 16 ) s = "accidentally"
  else if ( r == 17 ) s = "completely"
  else if ( r == 18 ) s = "usually"
  else if ( r == 19 ) s = "single-handledly"
  else if ( r == 20 ) s = "underhandedly"
  else if ( r == 21 ) s = "almost"
  else if ( r == 22 ) s = "wisely"
  else if ( r == 23 ) s = "ostensibly"
  else if ( r == 24 ) s = "somewhat"
  else if ( r == 25 ) s = "overwhelmingly"
  else if ( r == 26 ) s = "seldom"
  else if ( r == 27 ) s = "often"
  return s
}



function GenAdjective() {
  var n = 105
  var r = qrand(n)
  if ( r == PrevAdjective ) {
    r = qrand(n)
  }
  PrevAdjective = r
  var w = ""
  if ( r == 0 )   w = "slow"
  else if ( r == 1 )  w = "surly"
  else if ( r == 2 )  w = "gentle"
  else if ( r == 3 )  w = "optimal"
  else if ( r == 4 )  w = "treacherous"
  else if ( r == 5 )  w = "loyal"
  else if ( r == 6 )  w = "smelly"
  else if ( r == 7 )  w = "ravishing"
  else if ( r == 8 )  w = "annoying"
  else if ( r == 9 )  w = "burly"
  else if ( r == 10 ) w = "raspy"
  else if ( r == 11 ) w = "moldy"
  else if ( r == 12 ) w = "blotched"
  else if ( r == 13 ) w = "federal"
  else if ( r == 14 ) w = "phony"
  else if ( r == 15 ) w = "magnificent"
  else if ( r == 16 ) w = "alleged"
  else if ( r == 17 ) w = "crispy"
  else if ( r == 18 ) w = "gratifying"
  else if ( r == 19 ) w = "elusive"
  else if ( r == 20 ) w = "revered"
  else if ( r == 21 ) w = "spartan"
  else if ( r == 22 ) w = "righteous"
  else if ( r == 23 ) w = "mysterious"
  else if ( r == 24 ) w = "worldly"
  else if ( r == 25 ) w = "cosmopolitan"
  else if ( r == 26 ) w = "college-educated"
  else if ( r == 27 ) w = "bohemian"
  else if ( r == 28 ) w = "statesmanlike"
  else if ( r == 29 ) w = "stoic"
  else if ( r == 30 ) w = "hypnotic"
  else if ( r == 31 ) w = "dirt-encrusted"
  else if ( r == 32 ) w = "purple"
  else if ( r == 33 ) w = "infected"
  else if ( r == 34 ) w = "shabby"
  else if ( r == 35 ) w = "tattered"
  else if ( r == 36 ) w = "South American"
  else if ( r == 37 ) w = "Alaskan"
  else if ( r == 38 ) w = "overripe"
  else if ( r == 39 ) w = "self-loathing"
  else if ( r == 40 ) w = "frustrating"
  else if ( r == 41 ) w = "rude"
  else if ( r == 42 ) w = "pompous"
  else if ( r == 43 ) w = "impromptu"
  else if ( r == 44 ) w = "makeshift"
  else if ( r == 45 ) w = "so-called"
  else if ( r == 46 ) w = "proverbial"
  else if ( r == 47 ) w = "molten"
  else if ( r == 48 ) w = "wrinkled"
  else if ( r == 49 ) w = "psychotic"
  else if ( r == 50 ) w = "foreign"
  else if ( r == 51 ) w = "familiar"
  else if ( r == 52 ) w = "pathetic"
  else if ( r == 53 ) w = "precise"
  else if ( r == 54 ) w = "moronic"
  else if ( r == 55 ) w = "polka-dotted"
  else if ( r == 56 ) w = "varigated"
  else if ( r == 57 ) w = "mean-spirited"
  else if ( r == 58 ) w = "false"
  else if ( r == 59 ) w = "linguistic"
  else if ( r == 60 ) w = "temporal"
  else if ( r == 61 ) w = "fractured"
  else if ( r == 62 ) w = "dreamlike"
  else if ( r == 63 ) w = "imaginative"
  else if ( r == 64 ) w = "cantankerous"
  else if ( r == 65 ) w = "obsequious"
  else if ( r == 66 ) w = "twisted"
  else if ( r == 67 ) w = "load bearing"
  else if ( r == 68 ) w = "orbiting"
  else if ( r == 69 ) w = "radioactive"
  else if ( r == 70 ) w = "unstable"
  else if ( r == 71 ) w = "outer"
  else if ( r == 72 ) w = "nearest"
  else if ( r == 73 ) w = "most difficult"
  else if ( r == 74 ) w = "Eurasian"
  else if ( r == 75 ) w = "hairy"
  else if ( r == 76 ) w = "flabby"
  else if ( r == 77 ) w = "soggy"
  else if ( r == 78 ) w = "muddy"
  else if ( r == 79 ) w = "salty"
  else if ( r == 80 ) w = "highly paid"
  else if ( r == 81 ) w = "greasy"
  else if ( r == 82 ) w = "fried"
  else if ( r == 83 ) w = "frozen"
  else if ( r == 84 ) w = "boiled"
  else if ( r == 85 ) w = "incinerated"
  else if ( r == 86 ) w = "vaporized"
  else if ( r == 87 ) w = "nuclear"
  else if ( r == 88 ) w = "paternal"
  else if ( r == 89 ) w = "childlike"
  else if ( r == 90 ) w = "feline"
  else if ( r == 91 ) w = "fat"
  else if ( r == 92 ) w = "skinny"
  else if ( r == 93 ) w = "green"
  else if ( r == 94 ) w = "financial"
  else if ( r == 95 ) w = "frightened"
  else if ( r == 96 ) w = "fashionable"
  else if ( r == 97 ) w = "resplendent"
  else if ( r == 98 ) w = "flatulent"
  else if ( r == 99 ) w = "mitochondrial"
  else if ( r == 100 )  w = "overpriced"
  else if ( r == 101 )  w = "snooty"
  else if ( r == 102 )  w = "self-actualized"
  else if ( r == 103 )  w = "miserly"
  else if ( r == 104 )  w = "geosynchronous"


  if ( qrand(10) > 7 ) {
    w = GenAdverb() + " " + w
  }


  return w
}


// 'tense' is one of the following:
//  0 = infinitive
//  1 = present tense, third person singular
function GenTransitiveVerbPhrase(tense) {
  var n = 56
  var r = qrand(n)
  if ( r == PrevTransitiveVerb ) {
    r = qrand(n)
  }
  PrevTransitiveVerb = r
  var s = ""
  if ( r == 0 )   s = "eat$"
  else if ( r == 1 )  s = "conquer$"
  else if ( r == 2 )  s = "figure$ out"
  else if ( r == 3 )  s = "know$"
  else if ( r == 4 )  s = "teach*"
  else if ( r == 5 )  s = "require$ assistance from"
  else if ( r == 6 )  s = "pour$ freezing cold water on"
  else if ( r == 7 )  s = "find$ lice on"
  else if ( r == 8 )  s = "seek$"
  else if ( r == 9 )  s = "ignore$"
  else if ( r == 10 ) s = "dance$ with"
  else if ( r == 11 ) s = "recognize$"
  else if ( r == 12 ) s = "compete$ with"
  else if ( r == 13 ) s = "reach* an understanding with"
  else if ( r == 14 ) s = "negotiate$ a prenuptial agreement with"
  else if ( r == 15 ) s = "assimilate$"
  else if ( r == 16 ) s = "bestow$ great honor upon"
  else if ( r == 17 ) s = "derive$ perverse satisfaction from"
  else if ( r == 18 ) s = "steal$ pencils from"
  else if ( r == 19 ) s = "tr& to seduce"
  else if ( r == 20 ) s = "go* deep sea fishing with"
  else if ( r == 21 ) s = "find$ subtle faults with"
  else if ( r == 22 ) s = "laugh$ and drink$ all night with"
  else if ( r == 23 ) s = "befriend$"
  else if ( r == 24 ) s = "make$ a truce with"
  else if ( r == 25 ) s = "give$ secret financial aid to"
  else if ( r == 26 ) s = "brainwash*"
  else if ( r == 27 ) s = "trade$ baseball cards with"
  else if ( r == 28 ) s = "sell$ " + GenNounPhrase(0) + " to"
  else if ( r == 29 ) s = "caricature$"
  else if ( r == 30 ) s = "sanitize$"
  else if ( r == 31 ) s = "satiate$"
  else if ( r == 32 ) s = "organize$"
  else if ( r == 33 ) s = "graduate$ from"
  else if ( r == 34 ) s = "give$ lectures on morality to"
  else if ( r == 35 ) s = "^ a change of heart about"
  else if ( r == 36 ) s = "play$ pinochle with"
  else if ( r == 37 ) s = "give$ a pink slip to"
  else if ( r == 38 ) s = "share$ a shower with"
  else if ( r == 39 ) s = "buy$ an expensive gift for"
  else if ( r == 40 ) s = "cook$ cheese grits for"
  else if ( r == 41 ) s = "take$ a peek at"
  else if ( r == 42 ) s = "pee$ on"
  else if ( r == 43 ) s = "write$ a love letter to"
  else if ( r == 44 ) s = "fall$ in love with"
  else if ( r == 45 ) s = "avoid$ contact with"
  else if ( r == 46 ) s = ") a big fan of"
  else if ( r == 47 ) s = "secretly admire$"
  else if ( r == 48 ) s = "borrow$ money from"
  else if ( r == 49 ) s = "operate$ a small fruit stand with"
  else if ( r == 50 ) s = "throw$ " + GenNounPhrase(0) + " at"
  else if ( r == 51 ) s = "bur&"
  else if ( r == 52 ) s = "can be kind to"
  else if ( r == 53 ) s = "learn$ a hard lesson from"
  else if ( r == 54 ) s = "plan$ an escape from " + GenNounPhrase(0)
  else if ( r == 55 ) s = "make$ love to"
  else if ( r == 56 ) s = "sodomised"


  vt = ""
  var i
  for (i=0; i<s.length; i++ ) {
    var c = s.substring(i,i+1)
    var w = c
    if ( c == '$' ) {
      if ( tense == 0 )   w = ""
      else if ( tense == 1 )  w = "s"
    }
    else if ( c == '*' ) {
      if ( tense == 0 )   w = ""
      else if ( tense == 1 )  w = "es"
    }
    else if ( c == ')' ) {
      if ( tense == 0 )   w = "be"
      else if ( tense == 1 )  w = "is"
    }
    else if ( c == '^' ) {
      if ( tense == 0 )   w = "have"
      else if ( tense == 1 )  w = "has"
    }
    else if ( c == '&' ) {
      if ( tense == 0 )   w = "y"
      else if ( tense == 1 )  w = "ies"
    }
    vt += w
  }


  if ( qrand(10) < 3 ) {
    vt = GenAdverb() + " " + vt
  }


  return vt
}



function GenIntransitiveVerbPhrase() {
  var n = 28
  var r = qrand(n)
  if ( r == PrevIntransitiveVerb ) {
    r = qrand(n)
  }
  PrevIntransitiveVerb = r
  var s = ""
  if ( r == 0 )   s = "leaves"
  else if ( r == 1 )  s = "goes to sleep"
  else if ( r == 2 )  s = "takes a coffee break"
  else if ( r == 3 )  s = "hibernates"
  else if ( r == 4 )  s = "reads a magazine"
  else if ( r == 5 )  s = "self-flagellates"
  else if ( r == 6 )  s = "meditates"
  else if ( r == 7 )  s = "starts reminiscing about lost glory"
  else if ( r == 8 )  s = "flies into a rage"
  else if ( r == 9 )  s = "earns frequent flier miles"
  else if ( r == 10 ) s = "sweeps the floor"
  else if ( r == 11 ) s = "feels nagging remorse"
  else if ( r == 12 ) s = "returns home"
  else if ( r == 13 ) s = "rejoices"
  else if ( r == 14 ) s = "prays"
  else if ( r == 15 ) s = "procrastinates"
  else if ( r == 16 ) s = "daydreams"
  else if ( r == 17 ) s = "ceases to exist"
  else if ( r == 18 ) s = "hides"
  else if ( r == 19 ) s = "panics"
  else if ( r == 20 ) s = "beams with joy"
  else if ( r == 21 ) s = "laughs out loud"
  else if ( r == 22 ) s = "gets stinking drunk"
  else if ( r == 23 ) s = "wakes up"
  else if ( r == 24 ) s = "hesitates"
  else if ( r == 25 ) s = "trembles"
  else if ( r == 26 ) s = "ruminates"
  else if ( r == 27 ) s = "dies"
  return s
}



function GenConjunction() {
  var n = 4
  var r = qrand(n)
  var s = ""
  if ( r == 0 )   s = "and"
  else if ( r == 1 )  s = "or"
  else if ( r == 2 )  s = "but"
  else if ( r == 3 )  s = "because"
  return s
}



function CapFirst(s) {
  return s.substring(0,1).toUpperCase() + s.substring(1,s.length)
}



function GenRandomSentence() {
  var stemp = GenRandomSentenceTemplate()
  var i
  var s = ""
  for ( i=0; i<stemp.length; i++ ) {
    var c = stemp.substring(i,i+1)
    var w = ""
    if      ( c == '0' )  w = GenNoun()
    else if ( c == '1' )  w = GenNounPhrase(0)
    else if ( c == '2' )  w = GenTransitiveVerbPhrase(1)
    else if ( c == '3' )  w = GenConjunction()
    else if ( c == '4' )  w = GenIntransitiveVerbPhrase()
    else if ( c == '5' )  w = GenTransitiveVerbPhrase(0)
    else if ( c == '6' )  w = GenAdjective()
    else if ( c == '7' )  w = GenAdverb()
    else        w = c
    s += w
  }
  return CapFirst(s)
}


// -->
</script>


<center>
<h2></h2>

<table border="3" cellspacing="0" cellpadding="10" width="100%" background="/img/Paper.jpg"><tr><td><br/><font face="Verdana">

<script language="JavaScript"> <!--
qinit() // This initializes the random number generator

// The following are used to greatly reduce the incidence of
// repeated words in a sentence...
PrevNoun = -1
PrevTransitiveVerb = -1
PrevIntransitiveVerb = -1
PrevAdjective = -1
PrevAdverb = -1

document.write ( "<h2>", CapFirst(GenNounPhrase(0)), "</h2>" )
for ( i=0; i<5; i++ ) {
  document.write ( GenRandomSentence(), " " )
  }

for ( j=0; j<3; j++ ) {
  document.write ( "<h2><br></h2>" )
  for ( i=0; i<5; i++ ) {
    document.write ( GenRandomSentence(), " " )
    }
  }

document.write ( "<h2><br></h2>" )
for ( i=0; i<5; i++ ) {
  document.write ( GenRandomSentence(), " " )
  }
// -->
</script>
<br/><br/>

</font></td></tr></table><p></p></center></font></td></tr></table></td>

Your refactoring





Format Copy from initial code

or Cancel