r4780: Auto commit for Debian build
[umlisp.git] / sql-classes.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          sql-classes.lisp
6 ;;;; Purpose:       Routines for reading UMLS objects from SQL database
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: sql-classes.lisp,v 1.54 2003/05/03 22:38:22 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UMLisp, is
13 ;;;;    Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
14 ;;;;
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
18
19 (in-package :umlisp)
20 (declaim (optimize (compilation-speed 0) (debug 3)))
21
22
23 (defvar *current-srl* nil)
24 (defun current-srl ()
25   *current-srl*)
26 (defun current-srl! (srl)
27   (setq *current-srl* srl))
28
29 ;; only WHERE-VALUE and SRL are evaluated
30 (defmacro with-umlisp-query ((table fields srl where-name where-value
31                                     &key (lrl "KCUILRL") distinct single
32                                     order like)
33                              &body body)
34   (if single
35     `(unless (and ,where-name (not ,where-value)) 
36       (let ((tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,where-value
37                                       :lrl ,lrl :single ,single
38                                       :distinct ,distinct :order ,order :like ,like))))
39         (when tuple
40           (destructuring-bind ,fields tuple
41             ,@body))))
42     `(unless (and ,where-name (not ,where-value)) 
43       (loop for tuple in
44        (umlisp-query ,table ,fields ,srl ,where-name ,where-value
45         :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)
46        collect (destructuring-bind ,fields tuple
47                  ,@body)))))
48
49 (defmacro umlisp-query (table fields srl where-name where-value
50                      &key (lrl "KCUILRL") single distinct order like)
51   "Query the UMLisp database. Return a list of umlisp objects whose name
52 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
53   `(when (or (not ,where-name) ,where-value)
54     (mutex-sql-query
55      (query-string-macro ,table ,fields ,srl ,where-name ,where-value 
56       :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like))))
57
58   
59 (defmacro query-string-macro (table fields &optional srl where-name where-value
60                         &key (lrl "KCUILRL") single distinct order like)
61   (let* ((%%fields (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
62                            (if distinct "distinct " "") fields table))
63          (%%order (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) ""))
64          (%%lrl (format nil " and ~:@(~A~)<=" lrl))
65          (%%where (when where-name
66                     (format nil " where ~:@(~A~)~A" where-name
67                           (if like " like " "=")))))
68     `(concatenate
69       'string
70       ,%%fields
71       ,@(when %%where (list %%where))
72       ,@(when %%where
73               `((if (numberp ,where-value)
74                     (write-to-string ,where-value)
75                     (format nil ,(if like "'%~A%'" "'~A'") ,where-value))))
76       (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
77       ,@(when %%order (list %%order))
78       ,@(when single (list " limit 1")))))
79
80 (defun query-string (table fields &optional srl where-name where-value
81                      &key (lrl "KCUILRL") single distinct order like)
82   (concatenate
83    'string
84    (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)" 
85            (if distinct "distinct " "") fields table)
86    (if where-name
87        (format nil
88                (if (stringp where-value)
89                    (if like
90                        " where ~A like '%~A%'"
91                        " where ~A='~A'")
92                    " where ~A=~A")
93                where-name  where-value)
94        "")
95    (if srl (format nil " and ~:@(~A~) <= ~D" lrl srl) "")
96    (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
97    (if single " limit 1" "")))
98
99 (defun find-ucon-cui (cui &key (srl *current-srl*))
100   "Find ucon for a cui"
101   (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui (parse-cui cui) :single t)
102     (make-instance 'ucon :cui (parse-cui cui)
103                    :pfstr kpfstr
104                    :lrl (ensure-integer kcuilrl))))
105
106 (defun find-ucon-cui-old (cui &key (srl *current-srl*))
107   "Find ucon for a cui"
108   (when (stringp cui) (setq cui (parse-cui cui)))
109   (when cui
110     (let ((ls (format nil "select KPFSTR,KCUILRL from MRCON where CUI=~d" cui)))
111       (when srl
112         (string-append ls (format nil " and KCUILRL <= ~d" srl)))
113       (string-append ls " limit 1")
114       (let ((tuple (car (mutex-sql-query ls))))
115         (destructuring-bind (kpfstr kcuilrl) tuple
116           (make-instance 'ucon :cui cui :pfstr kpfstr
117                          :lrl (ensure-integer kcuilrl)))))))
118
119 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
120   "Find ucon for a cui"
121   (with-umlisp-query (mrcon (kcuilrl) srl cui (parse-cui cui) :single t)
122     (make-instance 'ucon :cui (parse-cui cui)
123                    :lrl (ensure-integer kcuilrl)
124                    :pfstr nil)))
125
126 (defun find-pfstr-cui (cui &key (srl *current-srl*))
127   "Find preferred string for a cui"
128   (with-umlisp-query (mrcon (kpfstr) srl cui (parse-cui cui) :single t)
129     kpfstr))
130
131 (defun find-ucon-lui (lui &key (srl *current-srl*))
132   "Find list of ucon for lui"
133   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui (parse-lui lui) :distinct t)
134     (make-instance 'ucon :cui (ensure-integer cui)
135                    :pfstr kpfstr
136                    :lrl (ensure-integer kcuilrl))))
137
138 (defun find-ucon-sui (sui &key (srl *current-srl*))
139   "Find list of ucon for sui"
140   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui (parse-sui sui) :distinct t)
141     (make-instance 'ucon :cui (ensure-integer cui)
142                    :pfstr kpfstr
143                    :lrl (ensure-integer kcuilrl))))
144
145 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
146   "Find ucon for cui/sui"
147   (when (and cui sui)
148     (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl kcuisui 
149                                (make-cuisui (parse-cui cui) (parse-sui sui)))
150       (make-instance 'ucon :cui (ensure-integer cui)
151                      :pfstr kpfstr
152                      :lrl (ensure-integer kcuilrl)))))
153
154 (defun find-ucon-str (str &key (srl *current-srl*))
155   "Find ucon that are exact match for str"
156   (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
157     (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
158                    :lrl (ensure-integer kcuilrl))))
159
160 (defun find-ucon-all (&key (srl *current-srl*))
161   "Return list of all ucon's"
162   (with-sql-connection (db)
163     (clsql:map-query 
164      'list
165      #'(lambda (cui pfstr cuilrl)
166          (make-instance 'ucon :cui (ensure-integer cui)
167                         :pfstr pfstr
168                         :lrl (ensure-integer cuilrl)))
169      (query-string 'mrcon '(cui kpfstr kcuilrl) srl nil nil
170                    :order (cui asc) :distinct t)
171      :database db)))
172
173 (defun map-ucon-all (fn &key (srl *current-srl*))
174   "Map a function over all ucon's"
175   (with-sql-connection (db)
176     (clsql:map-query 
177      nil
178      #'(lambda (cui pfstr cuilrl)
179          (funcall fn
180                   (make-instance 'ucon :cui (ensure-integer cui)
181                                  :pfstr pfstr
182                                  :lrl (ensure-integer cuilrl))))
183      (query-string 'mrcon '(cui kpfstr kcuilrl) srl nil nil :order (cui asc) :distinct t)
184      :database db)))
185
186
187 (defun find-udef-cui (cui &key (srl *current-srl*))
188   "Return a list of udefs for cui"
189   (with-umlisp-query (mrdef (sab def) srl cui (parse-cui cui) :lrl "KSRL")
190     (make-instance 'udef :sab sab :def def)))
191
192 (defun find-usty-cui (cui &key (srl *current-srl*))
193   "Return a list of usty for cui"
194   (with-umlisp-query (mrsty (tui sty) srl cui (parse-cui cui) :lrl "KLRL")
195     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
196
197 (defun find-usty-word (word &key (srl *current-srl*))
198   "Return a list of usty that match word"
199   (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t :distinct t)
200     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
201
202 (defun find-urel-cui (cui &key (srl *current-srl*))
203   "Return a list of urel for cui"
204   (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1 (parse-cui cui) :lrl "KSRL")
205     (make-instance 'urel :cui1 (parse-cui cui) :rel rel :cui2 (ensure-integer cui2) :rela rela
206                    :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
207
208 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
209   "Return a list of urel for cui2"
210   (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2 (parse-cui cui2) :lrl "KSRL")
211     (make-instance 'urel :cui2 (parse-cui cui2) :rel rel :cui1 (ensure-integer cui1) :rela rela
212                    :sab sab :sl sl :mg mg :pfstr2 kpfstr2)))
213
214 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
215   (mapcar 
216    #'(lambda (cui) (find-ucon-cui cui :srl srl))
217    (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
218
219 (defun find-ucoc-cui (cui &key (srl *current-srl*))
220   "Return a list of ucoc for cui"
221   (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1 (parse-cui cui) 
222                              :lrl "KSRL" :order (cof asc))
223     (setq cui2 (ensure-integer cui2))
224     (when (zerop cui2) (setq cui2 nil))
225     (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2) :soc soc :cot cot
226                    :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
227
228 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
229   "Return a list of ucoc for cui2"
230   (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2 (parse-cui cui2) 
231                              :lrl "KSRL" :order (cof asc))
232     (setq cui2 (ensure-integer cui2))
233     (when (zerop cui2) (setq cui2 nil))
234     (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2) :soc soc :cot cot
235                    :cof (ensure-integer cof) :coa coa :pfstr2 kpfstr2)))
236
237 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
238   "List of ucon with co-occurance cui2"
239   (mapcar 
240    #'(lambda (cui) (find-ucon-cui cui :srl srl))
241    (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
242
243 (defun find-ulo-cui (cui &key (srl *current-srl*))
244   "Return a list of ulo for cui"
245   (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui (parse-cui cui) :lrl "KLRL")
246     (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un :sui (ensure-integer sui) :sna sna
247                    :soui soui)))
248
249 (defgeneric suistr (lo))
250 (defmethod suistr ((lo ulo))
251   "Return the string for a ulo object"
252   (find-string-sui (sui lo)))
253
254 (defun find-uatx-cui (cui &key (srl *current-srl*))
255   "Return a list of uatx for cui"
256   (with-umlisp-query (mratx (sab rel atx) srl cui (parse-cui cui) :lrl ksrl)
257     (make-instance 'uatx :sab sab :rel rel :atx atx)))
258
259
260 (defun find-uterm-cui (cui &key (srl *current-srl*))
261   "Return a list of uterm for cui"
262   (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui (parse-cui cui) :lrl kluilrl
263                              :distinct t)
264     (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui)
265                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
266
267 (defun find-uterm-lui (lui &key (srl *current-srl*))
268   "Return a list of uterm for lui"
269   (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui (parse-lui lui) 
270                              :lrl kluilrl :distinct t)
271     (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui)
272                    :lat lat :ts ts :lrl (ensure-integer kluilrl))))
273
274 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
275   "Return single uterm for cui/lui"
276   (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
277                              (make-cuilui (parse-cui cui) (parse-lui lui))
278                              :lrl kluilrl :single t)
279     (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts :lrl (ensure-integer kluilrl))))
280
281 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
282   "Return a list of ustr for cui/lui"
283   (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui (make-cuilui cui lui) :lrl lrl)
284     (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
285                    :cuisui (make-cuisui cui sui) :stt stt :str str
286                    :lrl (ensure-integer lrl))))
287
288 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
289   "Return the single ustr for cuisui"
290   (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui (make-cuisui cui sui) :lrl lrl :single t)
291     (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
292                    :lui (ensure-integer lui) :stt stt :str str :lrl (ensure-integer lrl))))
293
294 (defun find-ustr-sui (sui &key (srl *current-srl*))
295   "Return the list of ustr for sui"
296   (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui (parse-sui sui) :lrl lrl)
297     (make-instance 'ustr :sui sui :cui cui :stt stt :str str
298                    :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui))
299                    :lui (ensure-integer lui)
300                    :lrl (ensure-integer lrl))))
301       
302 (defun find-ustr-sab (sab &key (srl *current-srl*))
303   "Return the list of ustr for sab"
304   (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
305     (let ((cuisui (ensure-integer kcuisui)))
306       (apply #'find-ustr-cuisui 
307              (append
308               (multiple-value-list (decompose-cuisui cuisui)) (list :srl srl))))))
309
310 (defun find-ustr-all (&key (srl *current-srl*))
311   "Return list of all ustr's"
312     (with-sql-connection (db)
313       (clsql:map-query 
314        'list
315        #'(lambda (cui lui sui stt lrl pfstr)
316            (setq cui (ensure-integer cui))
317            (setq lui (ensure-integer lui))
318            (setq sui (ensure-integer sui))      
319            (setq lrl (ensure-integer lrl))
320            (make-instance 'ustr :cui cui
321                           :lui lui
322                           :sui sui
323                           :cuisui (make-cuisui cui sui)
324                           :stt stt
325                           :lrl lrl
326                           :str pfstr))
327        (query-string 'mrcon '(cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl :distinct t
328                      :order (sui asc))
329        :database db)))
330
331 (defun find-string-sui (sui &key (srl *current-srl*))
332   "Return the string associated with sui"
333   (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
334     str))
335
336 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
337   (with-umlisp-query (mrso (sab code srl tty) srl kcuisui (make-cuisui cui sui) :lrl srl)
338       (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
339
340 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
341   (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc) srl kcuisui
342                              (make-cuisui cui sui) :lrl ksrl)
343     (make-instance 'ucxt :sab sab :code code
344                    :cxn (ensure-integer cxn)
345                    :cxl cxl :cxs cxs :hcd hcd :rela rela :xc xc
346                    :rnk (ensure-integer rnk)
347                    :cui2 (ensure-integer cui2))))
348
349 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
350   (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
351     (cond
352       (sui (string-append ls (format nil "KCUISUI=~d" (make-cuisui cui sui))))
353       (lui (string-append ls (format nil "KCUILUI=~d and sui=0" (make-cuilui cui lui))))
354       (t (string-append ls (format nil "cui=~d and lui=0 and sui=0" cui))))
355     (when srl
356       (string-append ls (format nil " and KSRL <= ~d" srl)))
357     (loop for tuple in (mutex-sql-query ls) collect 
358           (destructuring-bind (code atn sab atv) tuple
359             (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
360
361 (defun find-usty-tui (tui)
362   "Find usty for tui"
363   (with-umlisp-query (mrsty (sty) nil tui (parse-tui tui) :single t)
364     (make-instance 'usty :tui (parse-tui tui) :sty sty)))
365
366 (defun find-usty-sty (sty)
367   "Find usty for a sty"
368   (with-umlisp-query (mrsty (tui) nil sty sty :single t)
369     (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
370
371 (defun find-usty-all ()
372   "Return list of usty's for all semantic types"
373   (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
374     (find-usty-tui tui)))
375
376 (defun find-usab-all ()
377   "Find usab for a key"
378   (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil nil nil)
379     (make-instance 'usab :vcui (ensure-integer vcui) 
380                    :rcui (ensure-integer rcui)
381                    :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
382                    :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
383                    :srl (ensure-integer srl) 
384                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
385                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
386                    :curver curver :sabin sabin)))
387
388 (defun find-usab-by-key (key-name key)
389   "Find usab for a key"
390   (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta rmeta slc scc srl tfr cfr cxty ttyl atnl lat cenc curver sabin) nil key-name key :single t)
391     (make-instance 'usab :vcui (ensure-integer vcui) 
392                    :rcui (ensure-integer rcui)
393                    :vsab vsab :rsab rsab :son son :sf sf :sver sver :mstart mstart
394                    :mend mend :imeta imeta :rmeta rmeta :slc slc :scc scc
395                    :srl (ensure-integer srl) 
396                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
397                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
398                    :curver curver :sabin sabin)))
399
400 (defun find-usab-rsab (rsab)
401   "Find usab for rsab"
402   (find-usab-by-key "RSAB" rsab))
403
404 (defun find-usab-vsab (vsab)
405   "Find usab for vsab"
406   (find-usab-by-key "VSAB" vsab))
407
408 (defun find-cui-max ()
409   (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
410
411 ;;;; Cross table find functions
412
413 (defun find-ucon-tui (tui &key (srl *current-srl*))
414   "Find list of ucon for tui"
415   (with-umlisp-query (mrsty (cui) srl tui (parse-tui tui) :lrl klrl
416                              :order (cui asc))
417     (find-ucon-cui (ensure-integer cui) :srl srl)))
418   
419 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
420   "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
421   (with-umlisp-query (mrxw_eng (cui) srl wd word :like like :distinct t
422                                 :lrl klrl :order (cui asc))
423     (find-ucon-cui cui :srl srl)))
424
425 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
426   "Return list of ucons that match word, optionally use SQL's LIKE syntax"
427   (with-umlisp-query (mrxnw_eng (cui) srl nwd word :like like :distinct t
428                                 :lrl klrl :order (cui asc))
429     (find-ucon-cui cui :srl srl)))
430
431 (defun find-ustr-word (word &key (srl *current-srl*))
432   "Return list of ustrs that match word"
433   (with-umlisp-query (mrxw_eng (cui sui) srl wd word
434                                 :lrl klrl
435                                 :order (cui asc sui asc))
436     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
437
438 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
439   "Return list of ustrs that match word"
440   (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
441                                  :order (cui asc sui asc))
442     (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
443
444 ;; Special tables
445
446 (defun find-usrl-all ()
447   (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
448     (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
449
450 ;;; Multiword lookup and score functions
451
452 (defun find-ucon-multiword (str &key (srl *current-srl*))
453   "Return sorted list of ucon's that match a multiword string"
454   (let* ((words (delimited-string-to-list str #\space))
455          (ucons '()))
456     (dolist (word words)
457       (setq ucons (append ucons (find-ucon-word word :srl srl))))
458     (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
459
460 (defun find-ustr-multiword (str &key (srl *current-srl*))
461   "Return sorted list of ustr's that match a multiword string"
462   (let* ((words (delimited-string-to-list str #\space))
463          (ustrs '()))
464     (dolist (word words)
465       (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
466     (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
467         
468 (defun sort-score-ucon-str (str ucons)
469   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
470   (sort-score-umlsclass-str ucons str #'pfstr))
471
472 (defun sort-score-ustr-str (str ustrs)
473   "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
474   (sort-score-umlsclass-str ustrs str #'str))
475
476 (defun sort-score-umlsclass-str (objs str lookup-func)
477   "Sort a list of objects based on scoring to a string"
478   (let ((scored '()))
479     (dolist (obj objs)
480       (push 
481        (list obj 
482              (score-multiword-match str (funcall lookup-func obj))) 
483        scored))
484     (mapcar #'car (sort scored #'> :key #'cadr))))
485
486 (defun score-multiword-match (s1 s2)
487   "Score a match between two strings with s1 being reference string"
488   (let* ((word-list-1 (delimited-string-to-list s1 #\space))
489          (word-list-2 (delimited-string-to-list s2 #\space))
490          (n1 (length word-list-1))
491          (n2 (length word-list-2))
492          (unmatched n1)
493          (score 0)
494          (nlong 0)
495          (nshort 0)
496          short-list long-list)
497     (declare (fixnum n1 n2 nshort nlong score unmatched))
498     (if (> n1 n2)
499         (progn
500           (setq nlong n1)
501           (setq nshort n2)
502           (setq long-list word-list-1)
503           (setq short-list word-list-2))
504       (progn
505         (setq nlong n2)
506         (setq nshort n1)
507         (setq long-list word-list-2)
508         (setq short-list word-list-1)))
509     (decf score (- nlong nshort)) ;; reduce score for extra words
510     (dotimes (iword nshort)
511       (declare (fixnum iword))
512       (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
513            (progn
514              (incf score (- 10 (abs (- kmrcl::it iword))))
515              (decf unmatched))))
516     (decf score (* 2 unmatched))
517     score))
518
519
520 ;;; LEX SQL functions
521
522 (defun find-lexterm-eui (eui)
523   (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
524     (make-instance 'lexterm :eui eui :wrd wrd)))
525
526 (defun find-lexterm-word (wrd)
527   (with-umlisp-query (lrwd (eui) nil wrd wrd)
528     (make-instance 'lexterm :eui (ensure-integer eui)
529                    :wrd (copy-seq wrd))))
530
531 ;; LEX SQL Read functions
532
533 (defun find-labr-eui (eui)
534   (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui) 
535     (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
536                    :eui2 (ensure-integer eui2))))
537
538 (defun find-labr-bas (bas)
539   (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
540     (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
541                    :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
542
543 (defun find-lagr-eui (eui)
544   (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
545     (make-instance 'lagr :eui eui :str str :sca sca :agr agr
546                    :cit cit :bas bas)))
547
548 (defun find-lcmp-eui (eui)
549   (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
550     (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
551
552 (defun find-lmod-eui (eui)
553   (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
554     (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
555
556 (defun find-lnom-eui (eui)
557   (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
558     (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
559                    :eui2 (ensure-integer eui2))))
560
561 (defun find-lprn-eui (eui)
562   (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
563     (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
564                    :cas cas :pos pos :qnt qnt :fea fea)))
565
566 (defun find-lprp-eui (eui)
567   (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
568     (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
569
570 (defun find-lspl-eui (eui)
571   (with-umlisp-query (lrspl (spv bas) nil eui eui)
572     (make-instance 'lspl :eui eui :spv spv :bas bas)))
573
574 (defun find-ltrm-eui (eui)
575   (with-umlisp-query (lrtrm (bas gen) nil eui eui) 
576     (make-instance 'ltrm :eui eui :bas bas :gen gen)))
577
578 (defun find-ltyp-eui (eui)
579   (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
580     (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
581
582 (defun find-lwd-wrd (wrd)
583   (make-instance 'lwd :wrd
584                  :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
585                             (ensure-integer eui))))
586
587 ;;; Semantic Network SQL access functions
588
589 (defun find-sdef-ui (ui)
590   (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
591                             nil ui ui :single t)
592     (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
593                    :def def :ex ex :un un :rh rh :abr abr :rin rin)))
594
595 (defun find-sstre1-ui (ui)
596   (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
597     (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
598                    :ui3 (ensure-integer ui3))))
599
600 (defun find-sstre1-ui2 (ui2)
601   (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
602     (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
603                    :ui3 (ensure-integer ui3))))
604
605 (defun find-sstr-rl (rl)
606   (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
607     (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
608
609 (defun find-sstre2-sty (sty)
610   (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
611     (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
612
613 (defun find-sstr-styrl (styrl)
614   (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
615     (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))