1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
10 ;;;; $Id: sql-classes.lisp,v 1.72 2003/05/06 02:41:01 kevin Exp $
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2002 by Kevin M. Rosenberg, M.D.
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 ;;;; *************************************************************************
20 (declaim (optimize (compilation-speed 0) (debug 3)))
23 (defvar *current-srl* nil)
26 (defun current-srl! (srl)
27 (setq *current-srl* srl))
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~)~^,~}"
36 (%%lrl (format nil " and ~:@(~A~)<=" lrl))
37 (%%where (when where-name
38 (format nil " where ~:@(~A~)~A" where-name
39 (if like " like " "")))))
43 ,@(when %%where (list %%where))
45 `((typecase ,where-value
47 (concatenate 'string "=" (write-to-string ,where-value)))
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")))))
56 (defun query-string-eval (table fields &optional srl where-name where-value
57 &key (lrl "KCUILRL") single distinct order like)
60 (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
61 (if distinct "distinct " "") fields table)
62 (if where-name (format nil " where ~:@(~A~)" where-name) "")
69 (if like " like '%~A%""='~A'")))
72 (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
73 (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
74 (if single " limit 1" "")))
77 (defmacro umlisp-query (table fields srl where-name where-value
78 &key (lrl "KCUILRL") single distinct order like)
79 "Query the UMLisp database. Return a list of umlisp objects whose name
80 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
82 (query-string ,table ,fields ,srl ,where-name ,where-value
83 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
85 (defmacro umlisp-query-eval (table fields srl where-name where-value
86 &key (lrl "KCUILRL") single distinct order like)
87 "Query the UMLisp database. Return a list of umlisp objects whose name
88 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
90 (query-string-eval ,table ,fields ,srl ,where-name ,where-value
91 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
93 ;; only WHERE-VALUE and SRL are evaluated
94 (defmacro with-umlisp-query ((table fields srl where-name where-value
95 &key (lrl "KCUILRL") distinct single
98 (let ((value (gensym))
101 `(let* ((,value ,where-value)
102 (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
103 :lrl ,lrl :single ,single
104 :distinct ,distinct :order ,order
106 ,@(unless where-name `((declare (ignore ,value))))
108 (destructuring-bind ,fields tuple
110 `(let ((,value ,where-value))
111 ,@(unless where-name `((declare (ignore ,value))))
113 (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
114 :lrl ,lrl :single ,single :distinct ,distinct
115 :order ,order :like ,like))
116 (push (destructuring-bind ,fields tuple ,@body) ,r))
120 (umlisp-query ,table ,fields ,srl ,where-name ,value
121 :lrl ,lrl :single ,single :distinct ,distinct
122 :order ,order :like ,like)
123 collect (destructuring-bind ,fields tuple ,@body))))))
125 (defmacro with-umlisp-query-eval ((table fields srl where-name where-value
126 &key (lrl "KCUILRL") distinct single
129 (let ((value (gensym))
131 (eval-fields (cadr fields)))
133 `(let* ((,value ,where-value)
134 (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
135 :lrl ,lrl :single ,single
136 :distinct ,distinct :order ,order
139 (destructuring-bind ,eval-fields tuple
141 `(let ((,value ,where-value)
143 (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
144 :lrl ,lrl :single ,single :distinct ,distinct
145 :order ,order :like ,like))
146 (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
150 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
151 :lrl ,lrl :single ,single :distinct ,distinct
152 :order ,order :like ,like)
153 collect (destructuring-bind ,eval-fields tuple ,@body))))))
156 ;;; Read from SQL database
158 (defun find-ucon-cui (cui &key (srl *current-srl*))
159 "Find ucon for a cui"
160 (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui (parse-cui cui) :single t)
161 (make-instance 'ucon :cui (parse-cui cui) :pfstr kpfstr
162 :lrl (ensure-integer kcuilrl))))
164 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
165 "Find ucon for a cui"
166 (with-umlisp-query (mrcon (kcuilrl) srl cui (parse-cui cui) :single t)
167 (make-instance 'ucon :cui (parse-cui cui) :lrl (ensure-integer kcuilrl)
170 (defun find-pfstr-cui (cui &key (srl *current-srl*))
171 "Find preferred string for a cui"
172 (with-umlisp-query (mrcon (kpfstr) srl cui (parse-cui cui) :single t)
175 (defun find-ucon-lui (lui &key (srl *current-srl*))
176 "Find list of ucon for lui"
177 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui (parse-lui lui)
179 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
180 :lrl (ensure-integer kcuilrl))))
182 (defun find-ucon-sui (sui &key (srl *current-srl*))
183 "Find list of ucon for sui"
184 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui (parse-sui sui)
186 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
187 :lrl (ensure-integer kcuilrl))))
189 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
190 "Find ucon for cui/sui"
192 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl kcuisui
193 (make-cuisui (parse-cui cui) (parse-sui sui)))
194 (make-instance 'ucon :cui (ensure-integer cui)
196 :lrl (ensure-integer kcuilrl)))))
198 (defun find-ucon-str (str &key (srl *current-srl*))
199 "Find ucon that are exact match for str"
200 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
201 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
202 :lrl (ensure-integer kcuilrl))))
204 (defun find-ucon-all (&key (srl *current-srl*))
205 "Return list of all ucon's"
206 (with-sql-connection (db)
209 #'(lambda (cui pfstr cuilrl)
210 (make-instance 'ucon :cui (ensure-integer cui)
212 :lrl (ensure-integer cuilrl)))
213 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
214 :order (cui asc) :distinct t)
217 (defun find-ucon-all2 (&key (srl *current-srl*))
218 "Return list of all ucon's"
219 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
221 (make-instance 'ucon :cui (ensure-integer cui)
223 :lrl (ensure-integer kcuilrl))))
225 (defun map-ucon-all (fn &key (srl *current-srl*))
226 "Map a function over all ucon's"
227 (with-sql-connection (db)
230 #'(lambda (cui pfstr cuilrl)
232 (make-instance 'ucon :cui (ensure-integer cui)
234 :lrl (ensure-integer cuilrl))))
235 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
240 (defun find-udef-cui (cui &key (srl *current-srl*))
241 "Return a list of udefs for cui"
242 (with-umlisp-query (mrdef (sab def) srl cui (parse-cui cui) :lrl "KSRL")
243 (make-instance 'udef :sab sab :def def)))
245 (defun find-usty-cui (cui &key (srl *current-srl*))
246 "Return a list of usty for cui"
247 (with-umlisp-query (mrsty (tui sty) srl cui (parse-cui cui) :lrl "KLRL")
248 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
250 (defun find-usty-word (word &key (srl *current-srl*))
251 "Return a list of usty that match word"
252 (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
254 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
256 (defun find-urel-cui (cui &key (srl *current-srl*))
257 "Return a list of urel for cui"
258 (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
259 (parse-cui cui) :lrl "KSRL")
260 (make-instance 'urel :cui1 (parse-cui cui) :rel rel
261 :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
262 :mg mg :pfstr2 kpfstr2)))
264 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
265 "Return a list of urel for cui2"
266 (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
267 (parse-cui cui2) :lrl "KSRL")
268 (make-instance 'urel :cui2 (parse-cui cui2) :rel rel
269 :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
270 :mg mg :pfstr2 kpfstr2)))
272 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
274 #'(lambda (cui) (find-ucon-cui cui :srl srl))
275 (remove-duplicates (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))))
277 (defun find-ucoc-cui (cui &key (srl *current-srl*))
278 "Return a list of ucoc for cui"
279 (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
280 (parse-cui cui) :lrl "KSRL" :order (cof asc))
281 (setq cui2 (ensure-integer cui2))
282 (when (zerop cui2) (setq cui2 nil))
283 (make-instance 'ucoc :cui1 (parse-cui cui) :cui2 (ensure-integer cui2)
284 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
287 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
288 "Return a list of ucoc for cui2"
289 (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
290 (parse-cui cui2) :lrl "KSRL" :order (cof asc))
291 (setq cui2 (ensure-integer cui2))
292 (when (zerop cui2) (setq cui2 nil))
293 (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 (parse-cui cui2)
294 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
297 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
298 "List of ucon with co-occurance cui2"
300 #'(lambda (cui) (find-ucon-cui cui :srl srl))
301 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
303 (defun find-ulo-cui (cui &key (srl *current-srl*))
304 "Return a list of ulo for cui"
305 (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui (parse-cui cui)
307 (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
308 :sui (ensure-integer sui) :sna sna :soui soui)))
310 (defgeneric suistr (lo))
311 (defmethod suistr ((lo ulo))
312 "Return the string for a ulo object"
313 (find-string-sui (sui lo)))
315 (defun find-uatx-cui (cui &key (srl *current-srl*))
316 "Return a list of uatx for cui"
317 (with-umlisp-query (mratx (sab rel atx) srl cui (parse-cui cui) :lrl ksrl)
318 (make-instance 'uatx :sab sab :rel rel :atx atx)))
321 (defun find-uterm-cui (cui &key (srl *current-srl*))
322 "Return a list of uterm for cui"
323 (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui (parse-cui cui)
324 :lrl kluilrl :distinct t)
325 (make-instance 'uterm :lui (ensure-integer lui) :cui (parse-cui cui)
326 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
328 (defun find-uterm-lui (lui &key (srl *current-srl*))
329 "Return a list of uterm for lui"
330 (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui (parse-lui lui)
331 :lrl kluilrl :distinct t)
332 (make-instance 'uterm :cui (ensure-integer cui) :lui (parse-lui lui)
333 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
335 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
336 "Return single uterm for cui/lui"
337 (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
338 (make-cuilui (parse-cui cui) (parse-lui lui))
339 :lrl kluilrl :single t)
340 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
341 :lrl (ensure-integer kluilrl))))
343 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
344 "Return a list of ustr for cui/lui"
345 (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
346 (make-cuilui cui lui) :lrl lrl)
347 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
348 :cuisui (make-cuisui cui sui) :stt stt :str str
349 :lrl (ensure-integer lrl))))
351 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
352 "Return the single ustr for cuisui"
353 (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
354 (make-cuisui cui sui) :lrl lrl :single t)
355 (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
356 :lui (ensure-integer lui) :stt stt :str str
357 :lrl (ensure-integer lrl))))
359 (defun find-ustr-sui (sui &key (srl *current-srl*))
360 "Return the list of ustr for sui"
361 (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui (parse-sui sui)
363 (make-instance 'ustr :sui sui :cui cui :stt stt :str str
364 :cuisui (make-cuisui (ensure-integer cui) (parse-sui sui))
365 :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
367 (defun find-ustr-sab (sab &key (srl *current-srl*))
368 "Return the list of ustr for sab"
369 (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
370 (let ((cuisui (ensure-integer kcuisui)))
371 (apply #'find-ustr-cuisui
373 (multiple-value-list (decompose-cuisui cuisui))
376 (defun find-ustr-all (&key (srl *current-srl*))
377 "Return list of all ustr's"
378 (with-sql-connection (db)
381 #'(lambda (cui lui sui stt lrl pfstr)
382 (make-instance 'ustr :cui (ensure-integer cui)
383 :lui (ensure-integer lui) :sui (ensure-integer sui)
385 :cuisui (make-cuisui (ensure-integer cui)
386 (ensure-integer sui))
387 :lrl (ensure-integer lrl)))
388 (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
393 (defun find-string-sui (sui &key (srl *current-srl*))
394 "Return the string associated with sui"
395 (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
398 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
399 (with-umlisp-query (mrso (sab code srl tty) srl kcuisui
400 (make-cuisui cui sui) :lrl srl)
401 (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
403 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
404 (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
405 srl kcuisui (make-cuisui cui sui) :lrl ksrl)
406 (make-instance 'ucxt :sab sab :code code
407 :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
408 :rela rela :xc xc :rnk (ensure-integer rnk)
409 :cui2 (ensure-integer cui2))))
411 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
412 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
414 (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
415 (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
416 (make-cuilui cui lui))))
417 (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
419 (string-append ls (format nil " and KSRL <= ~D" srl)))
420 (loop for tuple in (mutex-sql-query ls) collect
421 (destructuring-bind (code atn sab atv) tuple
422 (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
424 (defun find-usty-tui (tui)
426 (with-umlisp-query (mrsty (sty) nil tui (parse-tui tui) :single t)
427 (make-instance 'usty :tui (parse-tui tui) :sty sty)))
429 (defun find-usty-sty (sty)
430 "Find usty for a sty"
431 (with-umlisp-query (mrsty (tui) nil sty sty :single t)
432 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
434 (defun find-usty-all ()
435 "Return list of usty's for all semantic types"
436 (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
437 (find-usty-tui tui)))
439 (defun find-usab-all ()
440 "Find usab for a key"
441 (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta
442 rmeta slc scc srl tfr cfr cxty ttyl atnl lat
443 cenc curver sabin) nil nil nil)
444 (make-instance 'usab :vcui (ensure-integer vcui)
445 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
446 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
447 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
448 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
449 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
450 :curver curver :sabin sabin)))
452 (defun find-usab-by-key (key-name key)
453 "Find usab for a key"
454 (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart
455 mend imeta rmeta slc scc srl tfr cfr cxty
456 ttyl atnl lat cenc curver sabin)
457 nil key-name key :single t)
458 (make-instance 'usab :vcui (ensure-integer vcui)
459 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
460 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
461 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
462 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
463 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
464 :curver curver :sabin sabin)))
466 (defun find-usab-rsab (rsab)
468 (find-usab-by-key 'rsab rsab))
470 (defun find-usab-vsab (vsab)
472 (find-usab-by-key 'vsab vsab))
474 (defun find-cui-max ()
475 (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
477 ;;;; Cross table find functions
479 (defun find-ucon-tui (tui &key (srl *current-srl*))
480 "Find list of ucon for tui"
481 (with-umlisp-query (mrsty (cui) srl tui (parse-tui tui) :lrl klrl :order (cui asc))
482 (find-ucon-cui (ensure-integer cui) :srl srl)))
484 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
485 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
486 (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
487 :lrl 'klrl :order '(cui asc))
488 (find-ucon-cui cui :srl srl)))
490 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
491 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
492 (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
493 :lrl 'klrl :order '(cui asc))
494 (find-ucon-cui cui :srl srl)))
496 (defun find-ustr-word (word &key (srl *current-srl*))
497 "Return list of ustrs that match word"
498 (with-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
499 :order (cui asc sui asc))
500 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
502 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
503 "Return list of ustrs that match word"
504 (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
505 :order (cui asc sui asc))
506 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
510 (defun find-usrl-all ()
511 (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
512 (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
514 ;;; Multiword lookup and score functions
516 (defun find-ucon-multiword (str &key (srl *current-srl*))
517 "Return sorted list of ucon's that match a multiword string"
518 (let* ((words (delimited-string-to-list str #\space))
521 (setq ucons (append ucons (find-ucon-word word :srl srl))))
522 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
524 (defun find-ustr-multiword (str &key (srl *current-srl*))
525 "Return sorted list of ustr's that match a multiword string"
526 (let* ((words (delimited-string-to-list str #\space))
529 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
530 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
532 (defun sort-score-ucon-str (str ucons)
533 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
534 (sort-score-umlsclass-str ucons str #'pfstr))
536 (defun sort-score-ustr-str (str ustrs)
537 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
538 (sort-score-umlsclass-str ustrs str #'str))
540 (defun sort-score-umlsclass-str (objs str lookup-func)
541 "Sort a list of objects based on scoring to a string"
544 (push (list obj (score-multiword-match str (funcall lookup-func obj)))
546 (mapcar #'car (sort scored #'> :key #'cadr))))
548 (defun score-multiword-match (s1 s2)
549 "Score a match between two strings with s1 being reference string"
550 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
551 (word-list-2 (delimited-string-to-list s2 #\space))
552 (n1 (length word-list-1))
553 (n2 (length word-list-2))
558 short-list long-list)
559 (declare (fixnum n1 n2 nshort nlong score unmatched))
564 (setq long-list word-list-1)
565 (setq short-list word-list-2))
569 (setq long-list word-list-2)
570 (setq short-list word-list-1)))
571 (decf score (- nlong nshort)) ;; reduce score for extra words
572 (dotimes (iword nshort)
573 (declare (fixnum iword))
574 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
576 (incf score (- 10 (abs (- kmrcl::it iword))))
578 (decf score (* 2 unmatched))
582 ;;; LEX SQL functions
584 (defun find-lexterm-eui (eui)
585 (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
586 (make-instance 'lexterm :eui eui :wrd wrd)))
588 (defun find-lexterm-word (wrd)
589 (with-umlisp-query (lrwd (eui) nil wrd wrd)
590 (make-instance 'lexterm :eui (ensure-integer eui)
591 :wrd (copy-seq wrd))))
593 ;; LEX SQL Read functions
595 (defun find-labr-eui (eui)
596 (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
597 (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
598 :eui2 (ensure-integer eui2))))
600 (defun find-labr-bas (bas)
601 (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
602 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
603 :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
605 (defun find-lagr-eui (eui)
606 (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
607 (make-instance 'lagr :eui eui :str str :sca sca :agr agr
610 (defun find-lcmp-eui (eui)
611 (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
612 (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
614 (defun find-lmod-eui (eui)
615 (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
616 (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
618 (defun find-lnom-eui (eui)
619 (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
620 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
621 :eui2 (ensure-integer eui2))))
623 (defun find-lprn-eui (eui)
624 (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
625 (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
626 :cas cas :pos pos :qnt qnt :fea fea)))
628 (defun find-lprp-eui (eui)
629 (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
630 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
632 (defun find-lspl-eui (eui)
633 (with-umlisp-query (lrspl (spv bas) nil eui eui)
634 (make-instance 'lspl :eui eui :spv spv :bas bas)))
636 (defun find-ltrm-eui (eui)
637 (with-umlisp-query (lrtrm (bas gen) nil eui eui)
638 (make-instance 'ltrm :eui eui :bas bas :gen gen)))
640 (defun find-ltyp-eui (eui)
641 (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
642 (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
644 (defun find-lwd-wrd (wrd)
645 (make-instance 'lwd :wrd
646 :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
647 (ensure-integer eui))))
649 ;;; Semantic Network SQL access functions
651 (defun find-sdef-ui (ui)
652 (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
654 (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
655 :def def :ex ex :un un :rh rh :abr abr :rin rin)))
657 (defun find-sstre1-ui (ui)
658 (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
659 (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
660 :ui3 (ensure-integer ui3))))
662 (defun find-sstre1-ui2 (ui2)
663 (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
664 (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
665 :ui3 (ensure-integer ui3))))
667 (defun find-sstr-rl (rl)
668 (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
669 (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
671 (defun find-sstre2-sty (sty)
672 (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
673 (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
675 (defun find-sstr-styrl (styrl)
676 (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
677 (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))