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