;;;; Author: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
-;;;; $Id: sql-classes.lisp,v 1.82 2003/05/16 03:10:18 kevin Exp $
+;;;; $Id: sql-classes.lisp,v 1.88 2003/07/21 00:53:27 kevin Exp $
;;;;
;;;; This file, part of UMLisp, is
-;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
+;;;; Copyright (c) 2000-2003 by Kevin M. Rosenberg, M.D.
;;;;
;;;; UMLisp users are granted the rights to distribute and use this software
;;;; as governed by the terms of the GNU General Public License.
;;;; *************************************************************************
-(in-package :umlisp)
-(declaim (optimize (compilation-speed 0) (debug 3)))
+(in-package #:umlisp)
(defvar *current-srl* nil)
(ensure-cui-integer cui)
(ensure-lui-integer lui)
(ensure-sui-integer sui)
- (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
+ (let ((ls "select CODE,ATN,SAB,ATV from MRSAT where "))
(cond
- (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
- (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
- (make-cuilui cui lui))))
- (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
+ (sui (string-append ls "KCUISUI="
+ (integer-string (make-cuisui cui sui) 14)))
+ (lui (string-append ls "KCUILUI="
+ (integer-string (make-cuilui cui lui) 14)
+ " and sui=0"))
+ (t (string-append ls "cui=" (prefixed-fixnum-string cui nil 7)
+ " and lui=0 and sui=0")))
(when srl
- (string-append ls (format nil " and KSRL <= ~D" srl)))
+ (string-append ls " and KSRL<=" (prefixed-fixnum-string srl nil 3)))
(loop for tuple in (mutex-sql-query ls) collect
(destructuring-bind (code atn sab atv) tuple
(make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
:order (cui asc sui asc))
(find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
+(defun find-uterm-word (word &key (srl *current-srl*))
+ "Return list of uterms that match word"
+ (collect-umlisp-query (mrxw_eng (cui lui) srl wd word :lrl klrl
+ :order (cui asc lui asc))
+ (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
+
+(defun find-uterm-normalized-word (word &key (srl *current-srl*))
+ "Return list of uterms that match word"
+ (collect-umlisp-query (mrxnw_eng (cui lui) srl nwd word :lrl klrl
+ :order (cui asc lui asc))
+ (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))
+
(defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
"Return list of ucons that match non-english word"
(collect-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
;;; Multiword lookup and score functions
-(defun find-ucon-multiword (str &key (srl *current-srl*))
- "Return sorted list of ucon's that match a multiword string"
- (let* ((words (delimited-string-to-list str #\space))
- (ucons '()))
- (dolist (word words)
- (setq ucons (append ucons (find-ucon-word word :srl srl))))
- (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
-
-(defun find-ustr-multiword (str &key (srl *current-srl*))
- "Return sorted list of ustr's that match a multiword string"
- (let* ((words (delimited-string-to-list str #\space))
- (ustrs '()))
- (dolist (word words)
- (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
- (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
+(defun find-uobj-multiword (str obj-lookup-fun sort-fun key srl
+ only-exact-if-match)
+ (let ((uobjs '()))
+ (dolist (word (delimited-string-to-list str #\space))
+ (setq uobjs (append uobjs (funcall obj-lookup-fun word :srl srl))))
+ (let ((sorted
+ (funcall sort-fun str
+ (delete-duplicates uobjs :test #'= :key key))))
+ (if (and (plusp (length sorted))
+ only-exact-if-match
+ (multiword-match str (pfstr (first sorted))))
+ (first sorted)
+ sorted))))
+
+(defun find-ucon-multiword (str &key (srl *current-srl*)
+ (only-exact-if-match t))
+ (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str
+ #'cui srl only-exact-if-match))
+
+(defun find-uterm-multiword (str &key (srl *current-srl*)
+ (only-exact-if-match t))
+ (find-uobj-multiword str #'find-uterm-word #'sort-score-pfstr-str
+ #'lui srl only-exact-if-match))
+
+(defun find-ustr-multiword (str &key (srl *current-srl*)
+ (only-exact-if-match t))
+ (find-uobj-multiword str #'find-ustr-word #'sort-score-ustr-str
+ #'sui srl only-exact-if-match))
-(defun sort-score-ucon-str (str ucons)
+(defun sort-score-pfstr-str (str uobjs)
"Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
- (sort-score-umlsclass-str ucons str #'pfstr))
+ (sort-score-umlsclass-str uobjs str #'pfstr))
(defun sort-score-ustr-str (str ustrs)
"Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
scored))
(mapcar #'car (sort scored #'> :key #'cadr))))
-(defun score-multiword-match (s1 s2)
- "Score a match between two strings with s1 being reference string"
- (let* ((word-list-1 (delimited-string-to-list s1 #\space))
- (word-list-2 (delimited-string-to-list s2 #\space))
- (n1 (length word-list-1))
- (n2 (length word-list-2))
- (unmatched n1)
- (score 0)
- (nlong 0)
- (nshort 0)
- short-list long-list)
- (declare (fixnum n1 n2 nshort nlong score unmatched))
- (if (> n1 n2)
- (progn
- (setq nlong n1)
- (setq nshort n2)
- (setq long-list word-list-1)
- (setq short-list word-list-2))
- (progn
- (setq nlong n2)
- (setq nshort n1)
- (setq long-list word-list-2)
- (setq short-list word-list-1)))
- (decf score (- nlong nshort)) ;; reduce score for extra words
- (dotimes (iword nshort)
- (declare (fixnum iword))
- (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
- (progn
- (incf score (- 10 (abs (- kmrcl::it iword))))
- (decf unmatched))))
- (decf score (* 2 unmatched))
- score))
-
;;; LEX SQL functions