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