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