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