annotate lisp/games/conx.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children b82b59fe008d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; -*- Mode:Emacs-Lisp; Blat:Foop -*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;;; conx.el: Yet Another Dissociator.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;;; Original design by Skef Wholey <skef@cs.cmu.edu>;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;;; ported to Emacs-Lisp by Jamie Zawinski <jwz@lucid.com>, 5-mar-91.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 (defconst conx-version "1.6, 6-may-94.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;; Run this compiled. It will be an order of magnitude faster.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;;; Select a buffer with a lot of text in it. Say M-x conx-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; or M-x conx-region. Repeat on as many other bodies of text as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;; you like.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;; M-x conx will use the word-frequency tree the above generated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; to produce random sentences in a popped-up buffer. It will pause
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;; at the end of each paragraph for two seconds; type ^G to stop it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;;; M-x conx-init will clear the data structures so you can start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;;; over. Note that if you run it twice consecutively on the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;; body of text, word sequences in that buffer will be twice as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;;; likely to be generated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Once you have sucked in a lot of text and like the kinds of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; sentences conx is giving you, you can save the internal data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; structures to a file with the M-x conx-save command. Loading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; this file with M-x conx-load will be a lot faster and easier
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; than re-absorbing all of the text files. Beware that loading a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; saved conx-file clears the conx database in memory.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; M-x conx-emit-c will write out C source code which, when compiled,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; will produce a standalone program which generates sentences from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; a copy of the database currently loaded.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; Ideas for future improvement:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; o It would be nice if we could load in more than one saved
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; file at a time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; o use it to collect statistics on newsgroup conversations by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; examining the tree for the most common words and phrases
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; o when replying to mail, insert an X-CONX: header field which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; contains a sentence randomly generated from the body of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; message being replied to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; o It could stand to be faster...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (defvar conx-bounce 10) ; 1/x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (defvar conx-hashtable-size 9923) ; 9923 is prime
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (defconst conx-words-hashtable nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (defconst conx-words-vector nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (defconst conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (defconst conx-last-word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (defvar conx-files nil "FYI")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (defun conx-init ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 "Forget the current word-frequency tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (if (and conx-words-hashtable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (>= (length conx-words-hashtable) conx-hashtable-size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (fillarray conx-words-hashtable 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (setq conx-words-hashtable (make-vector conx-hashtable-size 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (if conx-words-vector
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (fillarray conx-words-vector nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (setq conx-words-vector (make-vector 1000 nil))) ; this grows
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (setq conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (setq conx-last-word nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 conx-files nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (defun conx-rehash ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;; misnomer; this just grows the linear vector, growing the hash table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;; is too hard.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (message "Rehashing...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (let* ((L (length conx-words-vector))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (v2 (make-vector (+ L L) nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (while (< 0 L)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (aset v2 (1- L) (aref conx-words-vector (setq L (1- L)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (setq conx-words-vector v2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (message "Rehashing...done"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (defmacro conx-count (word) (list 'aref word 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (defmacro conx-cap (word) (list 'aref word 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (defmacro conx-comma (word) (list 'aref word 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (defmacro conx-period (word) (list 'aref word 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (defmacro conx-quem (word) (list 'aref word 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (defmacro conx-bang (word) (list 'aref word 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (defmacro conx-succ (word) (list 'aref word 6))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (defmacro conx-pred (word) (list 'aref word 7))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (defmacro conx-succ-c (word) (list 'aref word 8))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (defmacro conx-pred-c (word) (list 'aref word 9))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (defconst conx-length 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (defmacro conx-make-word ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 '(copy-sequence '[1 0 0 0 0 0 nil nil 0 0]))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (defmacro conx-setf (form val) ; mind-numbingly simple
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (setq form (macroexpand form (and (boundp 'byte-compile-macro-environment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 byte-compile-macro-environment)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (cond ((symbolp form) (list 'setq form val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ((eq (car form) 'aref) (cons 'aset (append (cdr form) (list val))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ((eq (car form) 'cdr) (list 'setcdr (nth 1 form) val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ((eq (car form) 'car) (list 'setcar (nth 1 form) val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (t (error "can't setf %s" form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (defmacro conx-push (thing list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (list 'conx-setf list (list 'cons thing list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (defconst conx-most-positive-fixnum (lsh -1 -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 "The largest positive integer that can be represented in this emacs.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defmacro conx-rand (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (list '% (list 'logand 'conx-most-positive-fixnum '(random)) n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (defmacro conx-relate-succ (word related)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (` (let ((vec (symbol-value (, word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (conx-setf (conx-succ-c vec) (1+ (conx-succ-c vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (let ((rel (assq (, related) (conx-succ vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (if rel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (setcdr rel (1+ (cdr rel)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (conx-push (cons (, related) 1) (conx-succ vec)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (defmacro conx-relate-pred (word related)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (` (let ((vec (symbol-value (, word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (conx-setf (conx-pred-c vec) (1+ (conx-pred-c vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (let ((rel (assq (, related) (conx-pred vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (if rel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (setcdr rel (1+ (cdr rel)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (conx-push (cons (, related) 1) (conx-pred vec)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (defmacro conx-add-word (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (` (let* ((word (, word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (fc (aref word 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (setq word (intern (downcase word) conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (let ((vec (and (boundp word) (symbol-value word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (if vec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (conx-setf (conx-count vec) (1+ (conx-count vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (if (= conx-words-vector-fp (length conx-words-vector))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (conx-rehash))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (set word (setq vec (conx-make-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (aset conx-words-vector conx-words-vector-fp word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (setq conx-words-vector-fp (1+ conx-words-vector-fp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (or (< fc ?A) (> fc ?Z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (conx-setf (conx-cap vec) (1+ (conx-cap vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if conx-last-word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (conx-relate-succ conx-last-word word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (conx-relate-pred word conx-last-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (setq conx-last-word word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (defmacro conx-punx (char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (` (if conx-last-word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (let ((char (, char))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (vec (symbol-value conx-last-word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (cond ((eq char ?\,)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (conx-setf (conx-comma vec) (1+ (conx-comma vec))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ((or (eq char ?\.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (eq char ?\;))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (conx-setf (conx-period vec) (1+ (conx-period vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (setq conx-last-word nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ((eq char ?\?)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (conx-setf (conx-quem vec) (1+ (conx-quem vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (setq conx-last-word nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 ((eq char ?\!)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (conx-setf (conx-bang vec) (1+ (conx-bang vec)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (setq conx-last-word nil)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (defun conxify-internal ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (let (p w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (skip-chars-forward "^A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (while (memq (following-char) '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?\'))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 ;; ignore words beginning with digits
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (skip-chars-forward "A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (skip-chars-forward "^A-Za-z0-9'"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (setq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (skip-chars-forward "A-Za-z0-9'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (if (= ?\' (preceding-char)) (forward-char -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (if (eq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (setq w (buffer-substring p (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (if (equal "nil" w) ; hey, nil is totally magic, this doesn't work!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (conx-add-word w)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (setq n (1+ n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (skip-chars-forward " \t\n\r")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (if (memq (setq p (following-char)) '(?\, ?\. ?\! ?\? ?\;))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (conx-punx p)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (defun conx-buffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 "Absorb the text in the current buffer into the tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (or conx-words-vector (conx-init))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (let ((i conx-words-vector-fp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (n 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (pm (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (while (< (setq p (point)) pm)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (search-forward "\n\n" pm 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (narrow-to-region p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (goto-char (prog1 p (setq p (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (conxify-internal)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (message "%d%%..." (/ (* p 100) (point-max))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (if buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (setq conx-files (nconc conx-files (list buffer-file-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (message "%s words, %d unique" n (- conx-words-vector-fp i))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (defun conx-region (p m)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 "Absorb the text in the current region into the tree."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (interactive "r")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (narrow-to-region p m)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (conx-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (defun conx-mail-buffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 "Conxify a buffer in /bin/mail format."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (skip-chars-forward "\n \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (let ((case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (buffer-file-name nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 p p2 p3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (or (looking-at "^From ") (error "not in /bin/mail format"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (search-forward "\n\n" nil 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (setq p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (search-forward "\nFrom " nil 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (setq p3 (setq p2 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;; don't count ".signature" sections.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (and (re-search-backward "\n--+\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (< (count-lines (point) p2) 9)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (setq p2 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (conx-region p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (goto-char p3)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (if buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (setq conx-files (nconc conx-files (list buffer-file-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 ;;; output
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (defun conx-random-related (count list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (let ((foll (if (= 0 count) 0 (conx-rand count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ans)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (if (<= foll (cdr (car list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (setq ans (car (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 list nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (setq foll (- foll (cdr (car list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 list (cdr list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 ans))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (defun conx-random-succ (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (if (= 0 (conx-succ-c (symbol-value word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (let ((next (conx-random-related
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (conx-succ-c (symbol-value word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (conx-succ (symbol-value word)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (if (= 0 (conx-rand conx-bounce))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (conx-random-succ
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (conx-random-related
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (conx-pred-c (symbol-value next))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (conx-pred (symbol-value next))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 next))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (defun conx-sentence ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (or (> conx-words-vector-fp 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (error "no conx data is loaded; see `conx-buffer'."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (let* ((word (aref conx-words-vector (conx-rand conx-words-vector-fp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (first-p t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 vec punc str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (while word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (setq punc (conx-rand (conx-count (setq vec (symbol-value word)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (if (or first-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 ;; (< (conx-rand (conx-count vec)) (conx-cap vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (= (conx-count vec) (conx-cap vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (setq first-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (setq str (symbol-name word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (insert (+ (- ?A ?a) (aref str 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (insert (substring str 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (insert (symbol-name word)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (cond ((< punc (conx-comma vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (insert ", "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 ((< (setq punc (- punc (conx-comma vec))) (conx-period vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (if (= 0 (conx-rand 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (if (= 0 (conx-rand 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (insert ": ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (insert "; "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (insert ". ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 ((< (setq punc (- punc (conx-period vec))) (conx-quem vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (insert "? "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 ((< (setq punc (- punc (conx-quem vec))) (conx-bang vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (setq word nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (insert "! "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (if (= 0 (conx-succ-c vec)) (setq word nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (if word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (setq word (conx-random-succ word))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (fill-region-as-paragraph (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (goto-char p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (if (= (preceding-char) ?\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (if (= 0 (conx-rand 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (delete-char -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (insert " "))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (defun conx ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 "Generate some random sentences in the *conx* buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (display-buffer (set-buffer (get-buffer-create "*conx*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (select-window (get-buffer-window "*conx*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (message "type ^G to stop.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (while t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (sit-for (if (= (preceding-char) ?\n) 2 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (conx-sentence)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 ;;; GNUS interface; grab words from the current message.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (defun conx-gnus-snarf ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 "For use as a gnus-select-article-hook."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (set-buffer gnus-article-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (search-forward "\n\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (conx-region (point) (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 ;;(add-hook 'gnus-select-article-hook 'conx-gnus-snarf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (defun psychoanalyze-conx ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 "Mr. Random goes to the analyst."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (doctor) ; start the psychotherapy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (message "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (switch-to-buffer "*doctor*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (sit-for 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (while (not (input-pending-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (conx-sentence)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (if (= (random 2) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (conx-sentence))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (sit-for 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (doctor-ret-or-read 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 ;;; Saving the database
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (defun conx-save (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 "Save the current CONX database to a file for future retrieval.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 You can re-load this database with the \\[conx-load] command."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (interactive "FSave CONX corpus to file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (let (b)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 (set-buffer (setq b (get-buffer-create "*conx-save-tmp*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 (delete-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 (insert ";;; -*- Mode:Emacs-Lisp -*-\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (insert ";;; This is a CONX database file. Load it with `conx-load'.\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (if conx-files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (insert ";;; Corpus: " (mapconcat 'identity conx-files ", ") "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (insert ";;; Date: " (current-time-string) "\n\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 ;; The file format used here is such a cute hack that I'm going to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 ;; leave it as an excercise to the reader to figure it out.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (let ((p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (fill-column 78)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (fill-prefix "\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (i 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 (insert "(!! [\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (while (< i conx-words-vector-fp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 (prin1 (aref conx-words-vector i) (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (setq i (1+ i)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 (insert "])\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (fill-region-as-paragraph p (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 (insert "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 (mapatoms (function (lambda (sym)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (if (not (boundp sym))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 (insert "\(! ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 (prin1 sym (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 (insert " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 (prin1 (symbol-value sym) (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (insert "\)\n"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 (while (re-search-forward "\\bnil\\b" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 (replace-match "()"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 (set-visited-file-name file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 (save-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (and b (kill-buffer b)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (defun conx-load (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 "Load in a CONX database written by the \\[conx-save] command.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 This clears the database currently in memory."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (interactive "fLoad CONX corpus from file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 (conx-init)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 (fset (intern "!!" conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 (function (lambda (vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (setq conx-words-vector vec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 conx-words-vector-fp (length vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (fset (intern "!" conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (symbol-function 'setq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (let ((obarray conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (load file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;;; Emitting C code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (defun conx-emit-c-data (&optional ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 (let ((all '())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (standard-output (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (after-change-functions nil) ; turning off font-lock speeds it up x2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (before-change-functions nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 (after-change-function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (before-change-function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 (float-output-format "%.2f")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 count total total100)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (or conx-words-hashtable (error "no words"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 (let ((i 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (mapatoms (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 (if (boundp x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 (setq all (cons (cons i x) all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 i (1+ i)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 conx-words-hashtable))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (setq all (nreverse all))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (setq total (* 4 (length all))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 total100 (max 1 (if (featurep 'lisp-float-type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (/ (float total) 100)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (/ total 100)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (i 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (insert "static unsigned short D[] = {")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (setq word (symbol-value (cdr (car rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (setq rest2 (conx-pred word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (while rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (princ (cdr (car rest2))) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (princ (car (rassq (car (car rest2)) all)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (setq i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (cond ((> i 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (setq i 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 (setq rest2 (cdr rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (setq rest2 (conx-succ word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (while rest2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (princ (cdr (car rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (princ (car (rassq (car (car rest2)) all)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (setq i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (cond ((> i 10)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (setq i 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (setq rest2 (cdr rest2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (setq rest (cdr rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 (insert "0};\nstatic char T[] = \"")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 (i 0) (j 20)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 k word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (setq word (symbol-name (cdr (car rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 (setq k (1+ (length word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 i (+ i k)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 j (+ j k 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (cond ((> j 77)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (insert (if ansi-p "\"\n\"" "\\\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (setq j (+ k 3))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (insert word) ; assumes word has no chars needing backslashes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (insert "\\000")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (setq rest (cdr rest))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (insert "\";\nstatic struct conx_word words [] = {")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (let ((rest all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (i 0) (j 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 cons name word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (while rest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (setq cons (car rest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 name (symbol-name (cdr cons))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 word (symbol-value (cdr cons)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (insert "{") (princ (conx-count word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (insert ",") (princ (conx-cap word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 (insert ",") (princ (conx-comma word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (insert ",") (princ (conx-period word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (insert ",") (princ (conx-quem word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (insert ",") (princ (conx-bang word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 (if (null (conx-pred word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 (insert ",0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 (princ i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (setq i (+ i (* 2 (length (conx-pred word))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 (if (null (conx-succ word))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 (insert ",0,")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (princ i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (setq i (+ i (* 2 (length (conx-succ word))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 (princ (conx-pred-c word)) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (princ (conx-succ-c word)) (insert ",")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (princ j)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 (setq j (+ j (length name) 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 (insert (if (cdr rest) (if (= 0 (% (car cons) 2)) "},\n" "},") "}"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (message "Writing C code... %s%%" (/ count total100))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (setq count (1+ count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 (setq rest (cdr rest))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (insert "};\n#define conx_bounce ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (princ conx-bounce)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (insert "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (message "Writing C code... done.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (defvar conx-c-prolog "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 #if __STDC__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 #include <stddef.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 extern long random (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 extern void srandom (int);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 extern void abort (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 #include <time.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 struct conx_word {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 unsigned short count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 unsigned short cap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 unsigned short comma;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 unsigned short period;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 unsigned short quem;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 unsigned short bang;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 unsigned short pred;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 unsigned short succ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 unsigned short npred;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 unsigned short nsucc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 unsigned short text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 (defvar conx-c-code "\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 #define countof(x) (sizeof((x)) / sizeof(*(x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 #define conx_rand(n) (random()%(n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 static struct conx_word *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 conx_random_related (count, which_list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 unsigned short count, which_list;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 unsigned short *list = D + which_list;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 int i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 unsigned short foll = (count == 0 ? 0 : conx_rand (count));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 if (foll <= list [i * 2])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 if ((list [i * 2 + 1]) > countof (words))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 return &words [list [i * 2 + 1]];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 foll -= list [i * 2];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 i++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 static struct conx_word *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 conx_random_succ (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 struct conx_word *word;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 if (word->nsucc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 return word;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 struct conx_word *next = conx_random_related (word->nsucc, word->succ);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 if (conx_rand (conx_bounce) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 return next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 return conx_random_succ (conx_random_related (next->npred, next->pred));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 conx_sentence ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 static int x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 struct conx_word *word = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 int first_p = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 int done = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 int count = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 while (!done)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 int punc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 char *text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 int L;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 if (word)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 word = conx_random_succ (word);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 word = &words [conx_rand (countof (words))];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 count++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 punc = conx_rand (word->count);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 text = T + word->text;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 L = strlen (text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 if (x + L > 70)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 putchar ('\\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 x += L+1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 if (first_p || (word->count == word->cap))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 putchar ((*text >= 'a' && *text <= 'z') ? *text + ('A'-'a') : *text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 fputs (text+1, stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 first_p = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 fputs (text, stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 if (punc < word->comma)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 fputs (\", \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 else if ((punc -= word->comma) < word->period)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 if (count > 120 || conx_rand (5) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 fputs (\". \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 word = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 if (conx_rand (4) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 fputs (\": \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 fputs (\"; \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 else if ((punc -= word->period) < word->quem)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 fputs (\"? \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 else if ((punc -= word->quem) < word->bang)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 fputs (\"! \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 if (word->nsucc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 fputs (\". \", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 x += 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 done = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 putchar (' ');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 if (conx_rand (3) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 fputs (\"\\n\\n\", stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 x = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 main (argc, argv)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 int argc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 char **argv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 unsigned int howmany, delay;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 char dummy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 if (argc == 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 howmany = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 delay = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 else if (argc == 2 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 delay = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 else if (argc == 3 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 1 == sscanf (argv[1], \"%ud%c\", &howmany, &dummy) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 1 == sscanf (argv[2], \"%ud%c\", &delay, &dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 fprintf (stderr, \"usage: %s [count [delay]]\\n\", argv [0]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 srandom (time (0));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 if (howmany == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 howmany = ~0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 while (howmany > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 conx_sentence ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 howmany--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 if (delay) sleep (delay);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 putchar ('\\n');
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 (defun conx-emit-c (file &optional non-ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 "Write the current CONX database to a file as C source code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 The generated program will have the same effect as M-x conx,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 except that it runs without emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 With a prefix argument, write K&R C instead of ANSI C. ANSI is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 the default because, without a certain ANSI feature, large databases
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 will overflow static limits in most K&R preprocessors."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 (interactive "FWrite C file: \nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 (find-file file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 (let ((buffer-undo-list t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 (insert conx-c-prolog)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 (if (not non-ansi-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 (insert "\n#if !__STDC__\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 "error! this file requires an ANSI C compiler\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 "#endif\n\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 (conx-emit-c-data (not non-ansi-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 (insert conx-c-code))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 (goto-char (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 ;;; Reporting stats
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 (defun conx-stats ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 (set-buffer (get-buffer-create "*conx-stats*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 (delete-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 (mapatoms (function (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 (or (not (boundp x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 (insert (format "%s" (conx-count (symbol-value x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 (insert "\t\t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 (insert (symbol-name x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 (insert "\n")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 conx-words-hashtable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 (sort-numeric-fields -1 (point-min) (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779