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