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