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