-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro-icp.lisp
417 lines (331 loc) · 15.4 KB
/
micro-icp.lisp
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
(in-package "ICP")
;;------------------------------------------------------------------------------
;;
;; File: MICRO-ICP.LISP
;; Created: 10/20/94
;; Author: Will Fitzgerald
;;
;; Description: Micro version of indexed concept parsing.
;;
;;------------------------------------------------------------------------------
(eval-when (load eval compile)
(unless (find-package :icp)
(make-package :icp)))
(in-package :icp)
(use-package :frames)
(use-package :dmap)
(use-package :tables)
(use-package :log)
(export '(def-assoc clear-icp-memory icp information-value
best-results *icp-results* print-icp-log m-reference-concept m-root
score target-concept index-concepts ticp))
;;------------------------------------------------------------------------------
;; Data structures for index concepts, target concepts, and their relationships
;;------------------------------------------------------------------------------
;; an index set is a target concept and its associated index concepts
(defclass index-set ()
((target-concept :initarg :target-concept :accessor target-concept)
(indices :initarg :indices :accessor indices)))
(defmethod print-object ((self index-set) stream)
(print-unreadable-object (self stream :type t :identity t)
(format stream "~s " (target-concept self))
(format stream "~s"(indices self))))
;; Data tables:
;; whether an object is a target concept;
;; from an index to all of the index sets it participates in;
;; from an index to all of the target concepts for which it is associated;
;; from an index to the number of target concepts it's associated with (for
;; calculating information value)
(deftable target-concept-p)
(deftable index->index-sets)
(deftable index->target-concepts)
(deftable index->target-concepts-cardinality)
(defun target-concept-cardinality ()
"How many target concepts -- for calculating information value"
(hash-table-count (target-concept-p)))
;;------------------------------------------------------------------------------
;; Installation of index sets.
;;------------------------------------------------------------------------------
(defmethod equal-instance-p ((index-set1 index-set) (index-set2 index-set))
(and (eql (target-concept index-set1)
(target-concept index-set2))
(equal (indices index-set1)
(indices index-set2))))
(defun set-index->target-concepts-cardinality (index)
(if (index->target-concepts-cardinality index)
(incf (index->target-concepts-cardinality index))
(setf (index->target-concepts-cardinality index) 1)))
(defmethod install ((index-set index-set))
(with-slots (target-concept indices) index-set
(dolist (index indices)
(unless (frame-of index)
(warn "~S does not name a frame." index))
(unless (member index (index->target-concepts index))
(push target-concept (index->target-concepts index))
(set-index->target-concepts-cardinality index))
(pushnew index-set (index->index-sets index) :test 'equal-instance-p))))
(defun add-index-set (target-concept indices)
(setf (target-concept-p target-concept) t)
(install (make-instance 'index-set
:target-concept target-concept
:indices indices))
target-concept)
(defmacro def-assoc (name &rest indices)
`(progn
(define-frame ',name '(m-reference-concept) nil)
(add-index-set ',name ',indices)))
;;------------------------------------------------------------------------------
;; Class for result from the parser
;;------------------------------------------------------------------------------
(defclass icp-result ()
((score :initarg :score :initform 0 :accessor score)
(target-concept :initarg :target-concept :initform nil :accessor target-concept)
(index-concepts :initarg :index-concepts :initform nil :accessor index-concepts)))
(defmethod print-object ((self icp-result) stream)
(with-slots (score target-concept index-concepts) self
(print-unreadable-object (self stream :type t :identity t)
(format stream "~4,2F ~S ~S" score target-concept index-concepts))))
(defun make-icp-result (score target-concept index-concepts)
(make-instance 'icp-result
:score score
:target-concept target-concept
:index-concepts index-concepts))
(defmethod score ((result null)) 0)
(defmethod target-concept ((result null)) nil)
(defmethod index-concepts ((result null)) nil)
;;------------------------------------------------------------------------------
;; ICP proper
;;------------------------------------------------------------------------------
(defvar *icp-results* () "A place to store the results of the parser")
(defun icp (words &optional (match-fn 'words->indices))
(setf *icp-results*
(remove-duplicates
(sort (score-index-sets (find-indices words match-fn))
#'> :key #'score)
:key 'target-concept :from-end t))
(first *icp-results*))
(defun best-results (&optional n)
(if n (first-n *icp-results* n) *icp-results*))
(defun find-indices (words match-fn)
(funcall match-fn words))
(defun score-index-sets (found-indices)
(loop for index-set in (candidate-index-sets found-indices)
collect (make-icp-result
(index-set-score index-set found-indices)
(target-concept index-set)
(indices index-set))))
;;------------------------------------------------------------------------------
;; Find all candidate index sets from the index concepts seen
;;------------------------------------------------------------------------------
(defun candidate-index-sets (found-indices)
(remove-duplicates
(loop for index in (all-absts-in found-indices)
append (index->index-sets index))))
(defun all-absts-in (concepts)
(remove-duplicates
(loop for concept in concepts
append (all-abstractions (frame-of concept)))))
;;------------------------------------------------------------------------------
;; Calculate the scores for each candidate index set
;; The real work is done by the appraiser functions. INDEX-SET-SCORE
;; just adds them up. Appraisers with no votes are not called.
;;------------------------------------------------------------------------------
(defun index-set-score (index-set found-indices)
(let ((score 0))
(map-table #'(lambda (appraiser votes)
(unless (zerop votes)
(incf score
(call-appraiser appraiser
index-set found-indices))))
(appraiser-votes))
(log:record-log (target-concept index-set)
"Total score for target ~S = ~5,3F"
(target-concept index-set) score)
(log:record-log (target-concept index-set)
"Associated index concepts: ~S~%~&~75,,,'-<~>~%"
(indices index-set))
score))
(defun call-appraiser (appraiser index-set found-indices)
(let ((score
(* (funcall appraiser index-set found-indices)
(appraiser-weight appraiser))))
(log:record-log (target-concept index-set)
"~A score = ~5,3F (Raw score * ~5,3F weighting)~%"
appraiser score (appraiser-weight appraiser))
score))
;;------------------------------------------------------------------------------
;; Information value functions
;;------------------------------------------------------------------------------
(defun probability-of-index (index)
(let ((cardinality (index->target-concepts-cardinality index)))
(if (null cardinality) least-positive-short-float
(/ cardinality
(target-concept-cardinality)))))
(defun information-value (index)
(- (log (probability-of-index index) 2)))
;;------------------------------------------------------------------------------
;; Appraisers
;;------------------------------------------------------------------------------
;;; An appraiser is a function assigned a non-zero number of votes.
;;; The function should take an index-set and an found-indices and return
;;; a score between 0 and 1 inclusive. The score is then multiplied by
;;; the appraiser's weight, which is the number of votes associated
;;; with the appraiser divided by the total number of votes for all
;;; appraisers.
;;; (ASSIGN-VOTES name [votes]) => name
;;; Assigns the given number of votes to an appraiser. If no votes
;;; are specified, 1 is assumed.
;;; Bookkeeping:
;;;
;;; As votes are assigned, we keep track of the total votes, to speed
;;; up calculating relative weights at parse time.
(deftable appraiser-votes)
(defvar *total-votes* 0
"Total number of votes for appraisers.")
(defun clear-appraisers ()
(clear-table (appraiser-votes))
(setf *total-votes* 0))
(defun assign-votes (name &optional (votes 1))
(setf (appraiser-votes name) votes)
(tally-votes)
name)
(defun tally-votes ()
(setq *total-votes* 0)
(map-table #'(lambda (name votes)
(declare (ignore name))
(incf *total-votes* votes))
(appraiser-votes)))
(defun appraiser-weight (appraiser)
(/ (appraiser-votes appraiser) *total-votes*))
;;------------------------------------------------------------------------------
;; Default appraiser functions
;;------------------------------------------------------------------------------
;;; Each of these appraisers compares a given index set against the
;;; pool of indices actually seen in the input:
;;;
;;; PREDICTED-SCORE -- how many predicted items were seen?
;;; UNPREDICTED-SCORE -- how many items were not predicted?
;;; UNSEEN-SCORE -- how many predicted items were not seen?
(defun predicted-score (index-set found-indices)
(let* ((predicted (indices index-set))
(predicted-items (predicted-items found-indices predicted))
(score
(/ (summed-value (target-concept index-set) predicted-items 'identity)
(summed-value (target-concept index-set) predicted 'identity))))
(log:record-log (target-concept index-set)
"Predicted raw score = ~5,3F (successfully predicted / predicted)"
score)
score))
(defun unpredicted-score (index-set found-indices)
(let* ((predicted (indices index-set))
(unpredicted-items (unpredicted-items found-indices predicted))
(score
(- 1 (/ (summed-value (target-concept index-set) unpredicted-items 'identity)
(summed-value (target-concept index-set) found-indices 'identity)))))
(log:record-log (target-concept index-set)
"Unpredicted raw score = ~5,3F (1 - unpredicted / seen)"
score)
score))
(defun unseen-score (index-set found-indices)
(let* ((predicted (indices index-set))
(unseen-items (unseen-items found-indices predicted))
(score
(- 1 (/ (summed-value (target-concept index-set) unseen-items 'identity)
(summed-value (target-concept index-set) predicted 'identity)))))
(log:record-log (target-concept index-set)
"Unseen raw score = ~5,3F (1 - unseen / predicted)"
score)
score))
(defun remove-parts (l)
"remove index concepts that form part of another index concept"
(remove-if #'(lambda (item) (member item l :test 'part-of))
l))
(defun predicted-items (seen-set predicted-set)
(intersection predicted-set seen-set :test 'abst-or-whole-of))
(defun unpredicted-items (seen-set predicted-set)
(set-difference seen-set predicted-set :test 'spec-or-part-of))
(defun unpredicted-items (seen-set predicted-set)
(set-difference (remove-parts seen-set) predicted-set :test 'specp))
(defun unseen-items (seen-set predicted-set)
(set-difference predicted-set seen-set :test 'abst-or-whole-of))
(defun summed-value (base predicted-set fn)
(let ((val
(loop for item in predicted-set
sum (funcall fn (information-value item)))))
(log:record-log base "Summed value of ~S~:[ using ~A~;~*~] => ~5,3F"
predicted-set (eql fn 'identity) fn val)
val))
;;------------------------------------------------------------------------------
;; This is an example of how to write an expection appraiser, although this
;; isn't used by default.
;;------------------------------------------------------------------------------
(defvar *expectations* nil
"A list of target concepts predicted")
(defun add-expectation (target-concept)
(pushnew target-concept *expectations*))
(defun clear-expectations ()
(setf *expectations* nil))
(defun set-expectations (target-concepts)
(setf *expectations* target-concepts))
(defun expected-p (target-concept)
(and (member target-concept *expectations* ) t))
(defun expected-score (index-set found-indices)
(declare (ignore found-indices))
(let* ((target-concept (target-concept index-set))
(found (expected-p target-concept))
(score (if found 1 0)))
(log:record-log target-concept
"Expected raw score = ~5,3F (1 if expected, 0 otherwise)"
score)
score))
;;------------------------------------------------------------------------------
;; Weight the appraisers
;;------------------------------------------------------------------------------
(clear-appraisers)
(assign-votes 'predicted-score 2)
(assign-votes 'unpredicted-score)
(assign-votes 'unseen-score)
;;------------------------------------------------------------------------------
;; Clearing memory
;;------------------------------------------------------------------------------
(defun clear-icp-memory ()
(clear-frame-memory)
(clear-predictions :all)
(clear-table (target-concept-p))
(clear-table (index->target-concepts))
(clear-table (index->index-sets))
(clear-table (index->target-concepts-cardinality))
t)
;;------------------------------------------------------------------------------
;; A DMAP-based index concept recognizer
;;------------------------------------------------------------------------------
(defun words->indices (sent)
(reset-parser)
(let (concepts)
(setf (call-backs 'm-root)
(list #'(lambda (item start end)
(record-log 'DMAP "DMAP referenced ~S from ~S"
item (subseq sent (1- start) end))
(push item concepts))))
(parse sent)
(setf concepts (mapcar #'frames::name concepts))
(record-log 'WORDS->INDICES
"~S ~%produced the index pool ~S~%~75,,,'=<~>~%"
sent concepts)
concepts))
;;------------------------------------------------------------------------------
;; Logging & testing functions
;;------------------------------------------------------------------------------
(defun first-n (sequence n)
(loop for i from 1 to n
for el in sequence
collect el))
(defun print-icp-log (&optional (n 7) (stream *standard-output*))
(print-log 'WORDs->INDICES stream)
(loop for result in (first-n *icp-results* n)
doing
(print-log (target-concept result) stream)))
(defmacro ticp (&rest words)
`(with-logging
(icp ',words)
(print-icp-log)))