(defun eucliddist (row1 row2 &optional (fss (thecols)) (tbl (thetable))) (dist (mapcar (selectcols fss) (row-cells row1) (row-cells row2) (table-cols tbl)))) (defun euclidtest (n m) (eucliddist (nth n (therows)) (nth m (therows)))) (defun dist (lst) (let ((r^2 0)) (dolist (x lst (sqrt r^2)) (incf r^2 (* x x))))) (defun selectcols (fss) (lambda (el1 el2 coll) (if (memberp coll fss) (coldist el1 el2 coll) 0))) (defmethod coldist (el1 el2 (coll sym)) (cond ((col-goalp coll) 0) ((unknownp el1) 0) ((unknownp el2) 0) ((eql el1 el2) 0) (t 1))) (defmethod coldist (el1 el2 (coll num)) (cond ((col-goalp coll) 0) ((unknownp el1) 0) ((unknownp el2) 0) (t (- (normalized el1 coll) (normalized el2 coll))))) (defun normalized (el coll) (/ (- el (num-min coll)) (- (num-max coll) (num-min coll)))) (defun unknownp (cell) (eql cell #\?)) (defun ABE0 (testcase k &optional (fss #'table-cols) (tbl (thetable))) (let (closest (retclass (car (table-klasses tbl))) (counts (make-hash-table)) (important (apply fss (list tbl)))) (dolist (rowx (table-rows tbl)) (cond ((< (length closest) k) (setf closest (cons (cons rowx (eucliddist rowx testcase important tbl)) closest))) (t (consider rowx testcase closest important tbl)))) (dolist (x closest) (if (gethash (row-class (car x)) counts) (incf (gethash (row-class (car x)) counts)) (setf (gethash (row-class (car x)) counts) 1))) (dolist (y (table-klasses tbl) retclass) (if (and (gethash (klass-name y) counts) (or (not (gethash (klass-name retclass) counts)) (> (gethash (klass-name y) counts) (gethash (klass-name retclass) counts)))) (setf retclass y))))) (defun consider (row1 testcase closest fss tbl) (let ((toadd (cons row1 (eucliddist row1 testcase fss tbl))) (i 0) n (addp nil) (maxim 0)) (dolist (row2 closest) (if (< (cdr toadd) (cdr row2)) (setf addp t)) (if (> (cdr row2) maxim) (progn (setf maxim (cdr row2)) (setf n i))) (incf i)) (if addp (setf (nth n closest) toadd))))