Source

misc-games / conx.el

  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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
;;; conx.el --- Yet another dissociater

;; Copyright status unknown

;; Author: Jamie Zawinski <jwz@netscape.com>
;; Keywords: games

;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not in FSF

;;; Commentary:

;;; conx.el: Yet Another Dissociator.
;;; Original design by Skef Wholey <skef@cs.cmu.edu>;
;;; ported to Emacs-Lisp by Jamie Zawinski <jwz@netscape.com>, 5-mar-91.
;;; Run this compiled.  It will be an order of magnitude faster.
;;;
;;; Select a buffer with a lot of text in it.  Say M-x conx-buffer
;;; or M-x conx-region.  Repeat on as many other bodies of text as
;;; you like.
;;;
;;; M-x conx will use the word-frequency tree the above generated
;;; to produce random sentences in a popped-up buffer.  It will pause
;;; at the end of each paragraph for two seconds; type ^G to stop it.
;;;
;;; M-x conx-init will clear the data structures so you can start
;;; over.  Note that if you run it twice consecutively on the same
;;; body of text, word sequences in that buffer will be twice as
;;; likely to be generated.
;;;
;;; Once you have sucked in a lot of text and like the kinds of
;;; sentences conx is giving you, you can save the internal data
;;; structures to a file with the M-x conx-save command.  Loading
;;; this file with M-x conx-load will be a lot faster and easier
;;; than re-absorbing all of the text files.  Beware that loading a
;;; saved conx-file clears the conx database in memory.
;;;
;;; M-x conx-emit-c will write out C source code which, when compiled,
;;; will produce a standalone program which generates sentences from
;;; a copy of the database currently loaded.
;;;
;;; Ideas for future improvement:
;;;
;;;  o  It would be nice if we could load in more than one saved
;;;     file at a time.
;;;
;;;  o  use it to collect statistics on newsgroup conversations by
;;;     examining the tree for the most common words and phrases
;;;
;;;  o  when replying to mail, insert an X-CONX: header field which
;;;     contains a sentence randomly generated from the body of the
;;;     message being replied to.
;;;
;;;  o  It could stand to be faster...

;;; Code:
(defconst conx-version "1.6,  6-may-94.")

(defvar conx-bounce 10) ; 1/x
(defvar conx-hashtable-size 9923)  ; 9923 is prime
(defconst conx-words-hashtable nil)
(defconst conx-words-vector nil)
(defconst conx-words-vector-fp 0)

(defconst conx-last-word nil)
p
(defvar conx-files nil "FYI")

(defun conx-init ()
  "Forget the current word-frequency tree."
  (interactive)
  (if (and conx-words-hashtable
	   (>= (length conx-words-hashtable) conx-hashtable-size))
      (fillarray conx-words-hashtable 0)
      (setq conx-words-hashtable (make-vector conx-hashtable-size 0)))
  (if conx-words-vector
      (fillarray conx-words-vector nil)
      (setq conx-words-vector (make-vector 1000 nil))) ; this grows
  (setq conx-words-vector-fp 0)
  (setq conx-last-word nil
	conx-files nil))

(defun conx-rehash ()
  ;; misnomer; this just grows the linear vector, growing the hash table
  ;; is too hard.
  (message "Rehashing...")
  (let* ((L (length conx-words-vector))
	 (v2 (make-vector (+ L L) nil)))
    (while (< 0 L)
      (aset v2 (1- L) (aref conx-words-vector (setq L (1- L)))))
    (setq conx-words-vector v2)
    )
  (message "Rehashing...done"))

(defmacro conx-count  (word) (list 'aref word 0))
(defmacro conx-cap    (word) (list 'aref word 1))
(defmacro conx-comma  (word) (list 'aref word 2))
(defmacro conx-period (word) (list 'aref word 3))
(defmacro conx-quem   (word) (list 'aref word 4))
(defmacro conx-bang   (word) (list 'aref word 5))
(defmacro conx-succ   (word) (list 'aref word 6))
(defmacro conx-pred   (word) (list 'aref word 7))
(defmacro conx-succ-c (word) (list 'aref word 8))
(defmacro conx-pred-c (word) (list 'aref word 9))
(defconst conx-length 10)

(defmacro conx-make-word ()
  '(copy-sequence '[1 0 0 0 0 0 nil nil 0 0]))

(defmacro conx-setf (form val)  ; mind-numbingly simple
  (setq form (macroexpand form (and (boundp 'byte-compile-macro-environment)
				    byte-compile-macro-environment)))
  (cond ((symbolp form) (list 'setq form val))
	((eq (car form) 'aref) (cons 'aset (append (cdr form) (list val))))
	((eq (car form) 'cdr) (list 'setcdr (nth 1 form) val))
	((eq (car form) 'car) (list 'setcar (nth 1 form) val))
	(t (error "can't setf %s" form))))

(defmacro conx-push (thing list)
  (list 'conx-setf list (list 'cons thing list)))

(defconst conx-most-positive-fixnum (lsh -1 -1)
  "The largest positive integer that can be represented in this emacs.")

(defmacro conx-rand (n)
  (list '% (list 'logand 'conx-most-positive-fixnum '(random)) n))

(defmacro conx-relate-succ (word related)
  (` (let ((vec (symbol-value (, word))))
       (conx-setf (conx-succ-c vec) (1+ (conx-succ-c vec)))
       (let ((rel (assq (, related) (conx-succ vec))))
	 (if rel
	     (setcdr rel (1+ (cdr rel)))
	     (conx-push (cons (, related) 1) (conx-succ vec)))))))

(defmacro conx-relate-pred (word related)
  (` (let ((vec (symbol-value (, word))))
       (conx-setf (conx-pred-c vec) (1+ (conx-pred-c vec)))
       (let ((rel (assq (, related) (conx-pred vec))))
	 (if rel
	     (setcdr rel (1+ (cdr rel)))
	     (conx-push (cons (, related) 1) (conx-pred vec)))))))

(defmacro conx-add-word (word)
  (` (let* ((word (, word))
	    (fc (aref word 0)))
       (setq word (intern (downcase word) conx-words-hashtable))
       (let ((vec (and (boundp word) (symbol-value word))))
	 (if vec
	     (conx-setf (conx-count vec) (1+ (conx-count vec)))
	   (if (= conx-words-vector-fp (length conx-words-vector))
	       (conx-rehash))
	   (set word (setq vec (conx-make-word)))
	   (aset conx-words-vector conx-words-vector-fp word)
	   (setq conx-words-vector-fp (1+ conx-words-vector-fp)))
	 (or (< fc ?A) (> fc ?Z)
	     (conx-setf (conx-cap vec) (1+ (conx-cap vec)))))
       (if conx-last-word
	   (progn
	     (conx-relate-succ conx-last-word word)
	     (conx-relate-pred word conx-last-word)))
       (setq conx-last-word word))))

(defmacro conx-punx (char)
  (` (if conx-last-word
	 (let ((char (, char))
	       (vec (symbol-value conx-last-word)))
	   (cond ((eq char ?\,)
		  (conx-setf (conx-comma vec) (1+ (conx-comma vec))))
		 ((or (eq char ?\.)
		      (eq char ?\;))
		  (conx-setf (conx-period vec) (1+ (conx-period vec)))
		  (setq conx-last-word nil))
		 ((eq char ?\?)
		  (conx-setf (conx-quem vec) (1+ (conx-quem vec)))
		  (setq conx-last-word nil))
		 ((eq char ?\!)
		  (conx-setf (conx-bang vec) (1+ (conx-bang vec)))
		  (setq conx-last-word nil)))))))

(defun conxify-internal ()
  (let (p w)
    (while (not (eobp))
      (skip-chars-forward "^A-Za-z0-9'")
      (while (memq (following-char) '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?\'))
	;; ignore words beginning with digits
	(skip-chars-forward "A-Za-z0-9'")
	(skip-chars-forward "^A-Za-z0-9'"))
      (setq p (point))
      (skip-chars-forward "A-Za-z0-9'")
      (if (= ?\' (preceding-char)) (forward-char -1))
      (if (eq p (point))
	  nil
	(setq w (buffer-substring p (point)))
	(if (equal "nil" w)  ; hey, nil is totally magic, this doesn't work!
	    nil
	  (conx-add-word w)
	  (setq n (1+ n))
	  (skip-chars-forward " \t\n\r")
	  (if (memq (setq p (following-char)) '(?\, ?\. ?\! ?\? ?\;))
	      (conx-punx p)))))))

;;;###autoload
(defun conx-buffer ()
  "Absorb the text in the current buffer into the tree."
  (interactive)
  (or conx-words-vector (conx-init))
  (let ((i conx-words-vector-fp)
	(n 0)
	(pm (point-max)))
    (save-excursion
      (goto-char (point-min))
      (save-restriction
	(widen)
	(while (< (setq p (point)) pm)
	  (search-forward "\n\n" pm 0)
	  (narrow-to-region p (point))
	  (goto-char (prog1 p (setq p (point))))
	  (conxify-internal)
	  (widen)
	  (message "%d%%..." (/ (* p 100) (point-max))))))
    (if buffer-file-name
	(setq conx-files (nconc conx-files (list buffer-file-name))))
    (message "%s words, %d unique" n (- conx-words-vector-fp i))))

;;;###autoload
(defun conx-region (p m)
  "Absorb the text in the current region into the tree."
  (interactive "r")
  (save-restriction
    (widen)
    (narrow-to-region p m)
    (conx-buffer)))

(defun conx-mail-buffer ()
  "Conxify a buffer in /bin/mail format."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (skip-chars-forward "\n \t")
    (let ((case-fold-search nil)
	  (buffer-file-name nil)
	  p p2 p3)
      (or (looking-at "^From ") (error "not in /bin/mail format"))
      (while (not (eobp))
	(search-forward "\n\n" nil 0)
	(setq p (point))
	(search-forward "\nFrom " nil 0)
	(setq p3 (setq p2 (point)))
	;; don't count ".signature" sections.
	(and (re-search-backward "\n--+\n" nil t)
	     (< (count-lines (point) p2) 9)
	     (setq p2 (point)))
	(conx-region p (point))
	(goto-char p3)))
    (if buffer-file-name
	(setq conx-files (nconc conx-files (list buffer-file-name))))
    ))

;;; output

(defun conx-random-related (count list)
  (let ((foll (if (= 0 count) 0 (conx-rand count)))
	ans)
    (while list
      (if (<= foll (cdr (car list)))
	  (setq ans (car (car list))
		list nil)
	  (setq foll (- foll (cdr (car list)))
		list (cdr list))))
    ans))

(defun conx-random-succ (word)
  (if (= 0 (conx-succ-c (symbol-value word)))
      word
      (let ((next (conx-random-related
		    (conx-succ-c (symbol-value word))
		    (conx-succ (symbol-value word)))))
	(if (= 0 (conx-rand conx-bounce))
	    (conx-random-succ
	      (conx-random-related
		(conx-pred-c (symbol-value next))
		(conx-pred (symbol-value next))))
	    next))))


(defun conx-sentence ()
  (or (> conx-words-vector-fp 0)
      (error "no conx data is loaded; see `conx-buffer'."))
  (let* ((word (aref conx-words-vector (conx-rand conx-words-vector-fp)))
	 (first-p t)
	 (p (point))
	 vec punc str)
    (while word
      (setq punc (conx-rand (conx-count (setq vec (symbol-value word)))))
      (if (or first-p
	      ;; (< (conx-rand (conx-count vec)) (conx-cap vec))
	      (= (conx-count vec) (conx-cap vec))
	      )
	  (progn
	    (setq first-p nil)
	    (setq str (symbol-name word))
	    (insert (+ (- ?A ?a) (aref str 0)))
	    (insert (substring str 1)))
	  (insert (symbol-name word)))
      (cond ((< punc (conx-comma vec))
	     (insert ", "))
	    ((< (setq punc (- punc (conx-comma vec))) (conx-period vec))
	     (setq word nil)
	     (if (= 0 (conx-rand 5))
		 (if (= 0 (conx-rand 4))
		     (insert ": ")
		     (insert "; "))
		 (insert ".  ")))
	    ((< (setq punc (- punc (conx-period vec))) (conx-quem vec))
	     (setq word nil)
	     (insert "?  "))
	    ((< (setq punc (- punc (conx-quem vec))) (conx-bang vec))
	     (setq word nil)
	     (insert "!  "))
	    (t
	     (insert " ")
	     (if (= 0 (conx-succ-c vec)) (setq word nil))))
      (if word
	  (setq word (conx-random-succ word))))
    (fill-region-as-paragraph (save-excursion
				(goto-char p)
				(beginning-of-line)
				(point))
			      (point))
    (if (= (preceding-char) ?\n)
	(if (= 0 (conx-rand 4))
	    (insert "\n")
	  (delete-char -1)
	  (insert "  "))))
  nil)

;;;###autoload
(defun conx ()
  "Generate some random sentences in the *conx* buffer."
  (interactive)
  (display-buffer (set-buffer (get-buffer-create "*conx*")))
  (select-window (get-buffer-window "*conx*"))
  (message "type ^G to stop.")
  (while t
    (goto-char (point-max))
    (sit-for (if (= (preceding-char) ?\n) 2 0))
    (conx-sentence)))


;;; GNUS interface; grab words from the current message.

(defun conx-gnus-snarf ()
  "For use as a gnus-select-article-hook."
  (set-buffer gnus-article-buffer)
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (search-forward "\n\n" nil t)
      (conx-region (point) (point-max)))))

;;(add-hook 'gnus-select-article-hook 'conx-gnus-snarf)

(defun psychoanalyze-conx ()
  "Mr. Random goes to the analyst."
  (interactive)
  (doctor)				; start the psychotherapy
  (message "")
  (switch-to-buffer "*doctor*")
  (sit-for 0)
  (while (not (input-pending-p))
    (conx-sentence)
    (if (= (random 2) 0)
	(conx-sentence))
    (sit-for 0)
    (doctor-ret-or-read 1)))


;;; Saving the database

(defun conx-save (file)
  "Save the current CONX database to a file for future retrieval.
You can re-load this database with the \\[conx-load] command."
  (interactive "FSave CONX corpus to file: ")
  (save-excursion
   (let (b)
    (unwind-protect
      (progn
	(set-buffer (setq b (get-buffer-create "*conx-save-tmp*")))
	(delete-region (point-min) (point-max))
	(insert ";;; -*- Mode:Emacs-Lisp -*-\n")
	(insert ";;; This is a CONX database file.  Load it with `conx-load'.\n")
	(if conx-files
	    (insert ";;; Corpus: " (mapconcat 'identity conx-files ", ") "\n"))
	(insert ";;; Date: " (current-time-string) "\n\n")
	;; The file format used here is such a cute hack that I'm going to
	;; leave it as an excercise to the reader to figure it out.
	(let ((p (point))
	      (fill-column 78)
	      (fill-prefix "\t")
	      (i 0))
	  (insert "(!! [\t")
	  (while (< i conx-words-vector-fp)
	    (prin1 (aref conx-words-vector i) (current-buffer))
	    (insert " ")
	    (setq i (1+ i)))
	  (insert "])\n")
	  (fill-region-as-paragraph p (point))
	  (insert "\n"))
	(mapatoms (function (lambda (sym)
		    (if (not (boundp sym))
			nil
		      (insert "\(! ")
		      (prin1 sym (current-buffer))
		      (insert " ")
		      (prin1 (symbol-value sym) (current-buffer))
		      (insert "\)\n"))))
		  conx-words-hashtable)
	(goto-char (point-min))
	(while (re-search-forward "\\bnil\\b" nil t)
	  (replace-match "()"))
	(set-visited-file-name file)
	(save-buffer)))
    (and b (kill-buffer b)))))

;;;###autoload
(defun conx-load (file)
  "Load in a CONX database written by the \\[conx-save] command.
This clears the database currently in memory."
  (interactive "fLoad CONX corpus from file: ")
  (conx-init)
  (fset (intern "!!" conx-words-hashtable)
	(function (lambda (vec)
	  (setq conx-words-vector vec
		conx-words-vector-fp (length vec)))))
  (fset (intern "!" conx-words-hashtable)
	(symbol-function 'setq))
  (let ((obarray conx-words-hashtable))
    (load file)))


;;; Emitting C code

(defun conx-emit-c-data (&optional ansi-p)
  (let ((all '())
	(standard-output (current-buffer))
	(after-change-functions nil) ; turning off font-lock speeds it up x2
	(before-change-functions nil)
	(after-change-function nil)
	(before-change-function nil)
	(float-output-format "%.2f")
	count total total100)
    (or conx-words-hashtable (error "no words"))
    (let ((i 0))
      (mapatoms (function (lambda (x)
			    (if (boundp x)
				(setq all (cons (cons i x) all)
				      i (1+ i)))))
		conx-words-hashtable))
    (setq all (nreverse all))
    (setq total (* 4 (length all))
	  total100 (max 1 (if (featurep 'lisp-float-type)
			      (/ (float total) 100)
			    (/ total 100)))
	  count 0)
    (let ((rest all)
	  (i 5)
	  rest2
	  word)
      (insert "static unsigned short D[] = {")
      (while rest
	(setq word (symbol-value (cdr (car rest))))
	(setq rest2 (conx-pred word))
	(setq count (1+ count))
	(while rest2
	  (princ (cdr (car rest2))) (insert ",")
	  (princ (car (rassq (car (car rest2)) all)))
	  (insert ",")
	  (setq i (1+ i))
	  (cond ((> i 10)
		 (insert "\n")
		 (setq i 0)))
	  (setq rest2 (cdr rest2)))
	(message "Writing C code... %s%%" (/ count total100))
	(setq count (1+ count))
	(setq rest2 (conx-succ word))
	(while rest2
	  (princ (cdr (car rest2)))
	  (insert ",")
	  (princ (car (rassq (car (car rest2)) all)))
	  (insert ",")
	  (setq i (1+ i))
	  (cond ((> i 10)
		 (insert "\n")
		 (setq i 0)))
	  (setq rest2 (cdr rest2)))
	(message "Writing C code... %s%%" (/ count total100))
	(setq count (1+ count))
	(setq rest (cdr rest))))
    (insert "0};\nstatic char T[] = \"")
    (let ((rest all)
	  (i 0) (j 20)
	  k word)
      (while rest
	(setq word (symbol-name (cdr (car rest))))
	(setq k (1+ (length word))
	      i (+ i k)
	      j (+ j k 3))
	(cond ((> j 77)
	       (insert (if ansi-p "\"\n\"" "\\\n"))
	       (setq j (+ k 3))))
	(insert word)		; assumes word has no chars needing backslashes
	(insert "\\000")
	(message "Writing C code... %s%%" (/ count total100))
	(setq count (1+ count))
	(setq rest (cdr rest))))
    (insert "\";\nstatic struct conx_word words [] = {")
    (let ((rest all)
	  (i 0) (j 0)
	  cons name word)
      (while rest
	(setq cons (car rest)
	      name (symbol-name (cdr cons))
	      word (symbol-value (cdr cons)))
	(insert "{") (princ (conx-count word))
	(insert ",") (princ (conx-cap word))
	(insert ",") (princ (conx-comma word))
	(insert ",") (princ (conx-period word))
	(insert ",") (princ (conx-quem word))
	(insert ",") (princ (conx-bang word))
	(if (null (conx-pred word))
	    (insert ",0")
	  (insert ",")
	  (princ i)
	  (setq i (+ i (* 2 (length (conx-pred word))))))
	(if (null (conx-succ word))
	    (insert ",0,")
	  (insert ",")
	  (princ i)
	  (insert ",")
	  (setq i (+ i (* 2 (length (conx-succ word))))))
	(princ (conx-pred-c word)) (insert ",")
	(princ (conx-succ-c word)) (insert ",")
	(princ j)
	(setq j (+ j (length name) 1))
	(insert (if (cdr rest) (if (= 0 (% (car cons) 2)) "},\n" "},") "}"))
	(message "Writing C code... %s%%" (/ count total100))
	(setq count (1+ count))
	(setq rest (cdr rest))
	))
    (insert "};\n#define conx_bounce ")
    (princ conx-bounce)
    (insert "\n")
    (message "Writing C code... done.")
    ))

(defvar conx-c-prolog "\
#if __STDC__
#include <stddef.h>
#include <unistd.h>
extern long random (void);
extern void srandom (int);
extern void abort (void);
#endif
#include <stdio.h>
#include <time.h>

struct conx_word {
  unsigned short count;
  unsigned short cap;
  unsigned short comma;
  unsigned short period;
  unsigned short quem;
  unsigned short bang;
  unsigned short pred;
  unsigned short succ;
  unsigned short npred;
  unsigned short nsucc;
  unsigned short text;
};
")

(defvar conx-c-code "\
#define countof(x) (sizeof((x)) / sizeof(*(x)))
#define conx_rand(n) (random()%(n))

static struct conx_word *
conx_random_related (count, which_list)
     unsigned short count, which_list;
{
  unsigned short *list = D + which_list;
  int i = 0;
  unsigned short foll = (count == 0 ? 0 : conx_rand (count));
  while (1)
    {
      if (foll <= list [i * 2])
	{
	  if ((list [i * 2 + 1]) > countof (words))
	    abort ();
	  return &words [list [i * 2 + 1]];
	}
      foll -= list [i * 2];
      i++;
    }
}

static struct conx_word *
conx_random_succ (word)
     struct conx_word *word;
{
  if (word->nsucc == 0)
    return word;
  else
    {
      struct conx_word *next = conx_random_related (word->nsucc, word->succ);
      if (conx_rand (conx_bounce) != 0)
	return next;
      return conx_random_succ (conx_random_related (next->npred, next->pred));
    }
}

static void
conx_sentence ()
{
  static int x = 0;
  struct conx_word *word = 0;
  int first_p = 1;
  int done = 0;
  int count = 0;
  while (!done)
    {
      int punc;
      char *text;
      int L;
      if (word)
	word = conx_random_succ (word);
      else
	word = &words [conx_rand (countof (words))];
      count++;
      punc = conx_rand (word->count);
      text = T + word->text;
      L = strlen (text);
      if (x + L > 70)
	{
	  putchar ('\\n');
	  x = 0;
	}
      x += L+1;

      if (first_p || (word->count == word->cap))
	{
	  putchar ((*text >= 'a' && *text <= 'z') ? *text + ('A'-'a') : *text);
	  fputs (text+1, stdout);
	  first_p = 0;
	}
      else
	fputs (text, stdout);

      if (punc < word->comma)
	{
	  fputs (\", \", stdout);
	  x++;
	}
      else if ((punc -= word->comma) < word->period)
	{
	  x++;
	  if (count > 120 || conx_rand (5) != 0)
	    {
	      done = 1;
	      fputs (\".  \", stdout);
	      x++;
	    }
	  else
	    {
	      word = 0;
	      if (conx_rand (4) == 0)
		fputs (\": \", stdout);
	      else
		fputs (\"; \", stdout);
	    }
	}
      else if ((punc -= word->period) < word->quem)
	{
	  done = 1;
	  fputs (\"?  \", stdout);
	  x += 2;
	}
      else if ((punc -= word->quem) < word->bang)
	{
	  done = 1;
	  fputs (\"!  \", stdout);
	  x += 2;
	}
      else
	{
	  if (word->nsucc == 0)
	    {
	      fputs (\".  \", stdout);
	      x += 2;
	      done = 1;
	    }
	  else
	    putchar (' ');
	}
    }
  if (conx_rand (3) == 0)
    {
      fputs (\"\\n\\n\", stdout);
      x = 0;
    }
}

main (argc, argv)
     int argc;
     char **argv;
{
  unsigned int howmany, delay;
  char dummy;
  if (argc == 1)
    {
      howmany = 1;
      delay = 0;
    }
  else if (argc == 2 &&
      1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy))
    delay = 0;
  else if (argc == 3 &&
	   1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy) &&
	   1 == sscanf (argv[2], \"%ud%c\", &delay, &dummy))
    ;
  else
    {
      fprintf (stderr, \"usage: %s [count [delay]]\\n\", argv [0]);
      exit (1);
    }

  srandom (time (0));
  if (howmany == 0)
    howmany = ~0;
  while (howmany > 0)
    {
      conx_sentence ();
      fflush (stdout);
      howmany--;
      if (delay) sleep (delay);
    }
  putchar ('\\n');
  exit (0);
}
")

(defun conx-emit-c (file &optional non-ansi-p)
  "Write the current CONX database to a file as C source code.
The generated program will have the same effect as M-x conx,
except that it runs without emacs.

With a prefix argument, write K&R C instead of ANSI C.  ANSI is
the default because, without a certain ANSI feature, large databases
will overflow static limits in most K&R preprocessors."
  (interactive "FWrite C file: \nP")
  (find-file file)
  (erase-buffer)
  (let ((buffer-undo-list t))
    (insert conx-c-prolog)
    (if (not non-ansi-p)
	(insert "\n#if !__STDC__\n"
		"error! this file requires an ANSI C compiler\n"
		"#endif\n\n"))
    (conx-emit-c-data (not non-ansi-p))
    (insert conx-c-code))
  (goto-char (point-min)))


;;; Reporting stats

(defun conx-stats ()
  (set-buffer (get-buffer-create "*conx-stats*"))
  (delete-region (point-min) (point-max))
  (mapatoms (function (lambda (x)
	      (or (not (boundp x))
		  (progn
		    (insert (format "%s" (conx-count (symbol-value x))))
		    (insert "\t\t")
		    (insert (symbol-name x))
		    (insert "\n")))))
	    conx-words-hashtable)
  (sort-numeric-fields -1 (point-min) (point-max)))

;;; conx.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.