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.81 2003/05/15 19:50:08 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 (prefixed-fixnum-string ,where-value #\= 10))
49 (concatenate 'string "=" (write-to-string ,where-value)))
53 (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
54 (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
55 ,@(when %%order (list %%order))
56 ,@(when single (list " limit 1")))))
58 (defun query-string-eval (table fields &optional srl where-name where-value
59 &key (lrl "KCUILRL") single distinct order like)
62 (format nil "select ~A~{~:@(~A~)~^,~} from ~:@(~A~)"
63 (if distinct "distinct " "") fields table)
64 (if where-name (format nil " where ~:@(~A~)" where-name) "")
71 (if like " like '%~A%""='~A'")))
74 (if srl (format nil " and ~:@(~A~)<=~D" lrl srl) "")
75 (if order (format nil " order by ~{~:@(~A~) ~(~A~)~^,~}" order) "")
76 (if single " limit 1" "")))
79 (defmacro umlisp-query (table fields srl where-name where-value
80 &key (lrl "KCUILRL") single distinct order like)
81 "Query the UMLisp database. Return a list of umlisp objects whose name
82 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
84 (query-string ,table ,fields ,srl ,where-name ,where-value
85 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
87 (defmacro umlisp-query-eval (table fields srl where-name where-value
88 &key (lrl "KCUILRL") single distinct order like)
89 "Query the UMLisp database. Return a list of umlisp objects whose name
90 is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
92 (query-string-eval ,table ,fields ,srl ,where-name ,where-value
93 :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like)))
95 ;; only WHERE-VALUE and SRL are evaluated
96 (defmacro with-umlisp-query ((table fields srl where-name where-value
97 &key (lrl "KCUILRL") distinct single
100 (let ((value (gensym))
103 `(let* ((,value ,where-value)
104 (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
105 :lrl ,lrl :single ,single
106 :distinct ,distinct :order ,order
108 ,@(unless where-name `((declare (ignore ,value))))
110 (destructuring-bind ,fields tuple
112 `(let ((,value ,where-value))
113 ,@(unless where-name `((declare (ignore ,value))))
115 (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
116 :lrl ,lrl :single ,single :distinct ,distinct
117 :order ,order :like ,like))
118 (push (destructuring-bind ,fields tuple ,@body) ,r))
122 (umlisp-query ,table ,fields ,srl ,where-name ,value
123 :lrl ,lrl :single ,single :distinct ,distinct
124 :order ,order :like ,like)
125 collect (destructuring-bind ,fields tuple ,@body))))))
127 (defmacro with-umlisp-query-eval ((table fields srl where-name where-value
128 &key (lrl "KCUILRL") distinct single
131 (let ((value (gensym))
133 (eval-fields (cadr fields)))
135 `(let* ((,value ,where-value)
136 (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
137 :lrl ,lrl :single ,single
138 :distinct ,distinct :order ,order
141 (destructuring-bind ,eval-fields tuple
143 `(let ((,value ,where-value)
145 (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
146 :lrl ,lrl :single ,single :distinct ,distinct
147 :order ,order :like ,like))
148 (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
152 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
153 :lrl ,lrl :single ,single :distinct ,distinct
154 :order ,order :like ,like)
155 collect (destructuring-bind ,eval-fields tuple ,@body))))))
158 ;;; Read from SQL database
160 (defmacro ensure-cui-integer (cui)
162 (setq ,cui (parse-cui ,cui))
165 (defmacro ensure-lui-integer (lui)
167 (setq ,lui (parse-lui ,lui))
170 (defmacro ensure-sui-integer (sui)
172 (setq ,sui (parse-sui ,sui))
175 (defmacro ensure-tui-integer (tui)
177 (setq ,tui (parse-tui ,tui))
180 (defmacro ensure-eui-integer (eui)
182 (setq ,eui (parse-eui ,eui))
185 (defun find-ucon-cui (cui &key (srl *current-srl*))
186 "Find ucon for a cui"
187 (ensure-cui-integer cui)
188 (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui cui :single t)
189 (make-instance 'ucon :cui cui :pfstr kpfstr
190 :lrl (ensure-integer kcuilrl))))
192 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
193 "Find ucon for a cui"
194 (ensure-cui-integer cui)
195 (with-umlisp-query (mrcon (kcuilrl) srl cui cui :single t)
196 (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
199 (defun find-pfstr-cui (cui &key (srl *current-srl*))
200 "Find preferred string for a cui"
201 (ensure-cui-integer cui)
202 (with-umlisp-query (mrcon (kpfstr) srl cui cui :single t)
205 (defun find-ucon-lui (lui &key (srl *current-srl*))
206 "Find list of ucon for lui"
207 (ensure-lui-integer lui)
208 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui lui
210 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
211 :lrl (ensure-integer kcuilrl))))
213 (defun find-ucon-sui (sui &key (srl *current-srl*))
214 "Find list of ucon for sui"
215 (ensure-sui-integer sui)
216 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui sui :distinct t)
217 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
218 :lrl (ensure-integer kcuilrl))))
220 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
221 "Find ucon for cui/sui"
222 (ensure-cui-integer cui)
223 (ensure-sui-integer sui)
225 (with-umlisp-query (mrcon (kpfstr kcuilrl) srl kcuisui
226 (make-cuisui cui sui))
227 (make-instance 'ucon :cui cui
229 :lrl (ensure-integer kcuilrl)))))
231 (defun find-ucon-str (str &key (srl *current-srl*))
232 "Find ucon that are exact match for str"
233 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
234 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
235 :lrl (ensure-integer kcuilrl))))
237 (defun find-ucon-all (&key (srl *current-srl*))
238 "Return list of all ucon's"
239 (with-sql-connection (db)
242 #'(lambda (cui pfstr cuilrl)
243 (make-instance 'ucon :cui (ensure-integer cui)
245 :lrl (ensure-integer cuilrl)))
246 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
247 :order (cui asc) :distinct t)
250 (defun find-ucon-all2 (&key (srl *current-srl*))
251 "Return list of all ucon's"
252 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
254 (make-instance 'ucon :cui (ensure-integer cui)
256 :lrl (ensure-integer kcuilrl))))
258 (defun map-ucon-all (fn &key (srl *current-srl*))
259 "Map a function over all ucon's"
260 (with-sql-connection (db)
263 #'(lambda (cui pfstr cuilrl)
265 (make-instance 'ucon :cui (ensure-integer cui)
267 :lrl (ensure-integer cuilrl))))
268 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
273 (defun find-udef-cui (cui &key (srl *current-srl*))
274 "Return a list of udefs for cui"
275 (ensure-cui-integer cui)
276 (with-umlisp-query (mrdef (sab def) srl cui cui :lrl "KSRL")
277 (make-instance 'udef :sab sab :def def)))
279 (defun find-usty-cui (cui &key (srl *current-srl*))
280 "Return a list of usty for cui"
281 (ensure-cui-integer cui)
282 (with-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
283 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
285 (defun find-usty-word (word &key (srl *current-srl*))
286 "Return a list of usty that match word"
287 (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
289 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
291 (defun find-urel-cui (cui &key (srl *current-srl*))
292 "Return a list of urel for cui"
293 (ensure-cui-integer cui)
294 (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
296 (make-instance 'urel :cui1 cui :rel rel
297 :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
298 :mg mg :pfstr2 kpfstr2)))
300 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
301 "Return a list of urel for cui2"
302 (ensure-cui-integer cui2)
303 (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
305 (make-instance 'urel :cui2 cui2 :rel rel
306 :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
307 :mg mg :pfstr2 kpfstr2)))
309 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
310 (ensure-cui-integer cui2)
311 (loop for cui in (remove-duplicates
312 (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
313 collect (find-ucon-cui cui :srl srl)))
315 (defun find-ucoc-cui (cui &key (srl *current-srl*))
316 "Return a list of ucoc for cui"
317 (ensure-cui-integer cui)
318 (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
319 cui :lrl klrl :order (cof asc))
320 (setq cui2 (ensure-integer cui2))
321 (when (zerop cui2) (setq cui2 nil))
322 (make-instance 'ucoc :cui1 cui :cui2 (ensure-integer cui2)
323 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
326 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
327 "Return a list of ucoc for cui2"
328 (ensure-cui-integer cui2)
329 (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
330 cui2 :lrl klrl :order (cof asc))
331 (when (zerop cui2) (setq cui2 nil))
332 (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
333 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
336 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
337 "List of ucon with co-occurance cui2"
338 (ensure-cui-integer cui2)
340 #'(lambda (cui) (find-ucon-cui cui :srl srl))
341 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
343 (defun find-ulo-cui (cui &key (srl *current-srl*))
344 "Return a list of ulo for cui"
345 (ensure-cui-integer cui)
346 (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui cui
348 (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
349 :sui (ensure-integer sui) :sna sna :soui soui)))
351 (defun find-uatx-cui (cui &key (srl *current-srl*))
352 "Return a list of uatx for cui"
353 (ensure-cui-integer cui)
354 (with-umlisp-query (mratx (sab rel atx) srl cui cui :lrl ksrl)
355 (make-instance 'uatx :sab sab :rel rel :atx atx)))
358 (defun find-uterm-cui (cui &key (srl *current-srl*))
359 "Return a list of uterm for cui"
360 (ensure-cui-integer cui)
361 (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui cui
362 :lrl kluilrl :distinct t)
363 (make-instance 'uterm :lui (ensure-integer lui) :cui cui
364 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
366 (defun find-uterm-lui (lui &key (srl *current-srl*))
367 "Return a list of uterm for lui"
368 (ensure-lui-integer lui)
369 (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui lui
370 :lrl kluilrl :distinct t)
371 (make-instance 'uterm :cui (ensure-integer cui) :lui lui
372 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
374 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
375 "Return single uterm for cui/lui"
376 (ensure-cui-integer cui)
377 (ensure-lui-integer lui)
378 (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
379 (make-cuilui cui lui)
380 :lrl kluilrl :single t)
381 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
382 :lrl (ensure-integer kluilrl))))
384 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
385 "Return a list of ustr for cui/lui"
386 (ensure-cui-integer cui)
387 (ensure-lui-integer lui)
388 (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
389 (make-cuilui cui lui) :lrl lrl)
390 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
391 :cuisui (make-cuisui cui sui) :stt stt :str str
392 :lrl (ensure-integer lrl))))
394 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
395 "Return the single ustr for cuisui"
396 (ensure-cui-integer cui)
397 (ensure-sui-integer sui)
398 (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
399 (make-cuisui cui sui) :lrl lrl :single t)
400 (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
401 :lui (ensure-integer lui) :stt stt :str str
402 :lrl (ensure-integer lrl))))
404 (defun find-ustr-sui (sui &key (srl *current-srl*))
405 "Return the list of ustr for sui"
406 (ensure-sui-integer sui)
407 (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui sui
409 (make-instance 'ustr :sui sui :cui cui :stt stt :str str
410 :cuisui (make-cuisui (ensure-integer cui) sui)
411 :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
413 (defun find-ustr-sab (sab &key (srl *current-srl*))
414 "Return the list of ustr for sab"
415 (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
416 (let ((cuisui (ensure-integer kcuisui)))
417 (apply #'find-ustr-cuisui
419 (multiple-value-list (decompose-cuisui cuisui))
422 (defun find-ustr-all (&key (srl *current-srl*))
423 "Return list of all ustr's"
424 (with-sql-connection (db)
427 #'(lambda (cui lui sui stt lrl pfstr)
428 (make-instance 'ustr :cui (ensure-integer cui)
429 :lui (ensure-integer lui) :sui (ensure-integer sui)
431 :cuisui (make-cuisui (ensure-integer cui)
432 (ensure-integer sui))
433 :lrl (ensure-integer lrl)))
434 (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
439 (defun find-string-sui (sui &key (srl *current-srl*))
440 "Return the string associated with sui"
441 (ensure-sui-integer sui)
442 (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
445 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
446 (ensure-sui-integer sui)
447 (ensure-cui-integer cui)
448 (with-umlisp-query (mrso (sab code srl tty) srl kcuisui
449 (make-cuisui cui sui) :lrl srl)
450 (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
452 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
453 (ensure-cui-integer cui)
454 (ensure-sui-integer sui)
455 (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
456 srl kcuisui (make-cuisui cui sui) :lrl ksrl)
457 (make-instance 'ucxt :sab sab :code code
458 :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
459 :rela rela :xc xc :rnk (ensure-integer rnk)
460 :cui2 (ensure-integer cui2))))
462 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
463 (ensure-cui-integer cui)
464 (ensure-lui-integer lui)
465 (ensure-sui-integer sui)
466 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
468 (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
469 (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
470 (make-cuilui cui lui))))
471 (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
473 (string-append ls (format nil " and KSRL <= ~D" srl)))
474 (loop for tuple in (mutex-sql-query ls) collect
475 (destructuring-bind (code atn sab atv) tuple
476 (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
478 (defun find-usty-tui (tui)
480 (ensure-tui-integer tui)
481 (with-umlisp-query (mrsty (sty) nil tui tui :single t)
482 (make-instance 'usty :tui tui :sty sty)))
484 (defun find-usty-sty (sty)
485 "Find usty for a sty"
486 (with-umlisp-query (mrsty (tui) nil sty sty :single t)
487 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
489 (defun find-usty-all ()
490 "Return list of usty's for all semantic types"
491 (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
492 (find-usty-tui tui)))
494 (defun find-usab-all ()
495 "Find usab for a key"
496 (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta
497 rmeta slc scc srl tfr cfr cxty ttyl atnl lat
498 cenc curver sabin) nil nil nil)
499 (make-instance 'usab :vcui (ensure-integer vcui)
500 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
501 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
502 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
503 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
504 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
505 :curver curver :sabin sabin)))
507 (defun find-usab-by-key (key-name key)
508 "Find usab for a key"
509 (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart
510 mend imeta rmeta slc scc srl tfr cfr cxty
511 ttyl atnl lat cenc curver sabin)
512 nil key-name key :single t)
513 (make-instance 'usab :vcui (ensure-integer vcui)
514 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
515 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
516 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
517 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
518 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
519 :curver curver :sabin sabin)))
521 (defun find-usab-rsab (rsab)
523 (find-usab-by-key 'rsab rsab))
525 (defun find-usab-vsab (vsab)
527 (find-usab-by-key 'vsab vsab))
529 (defun find-cui-max ()
530 (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
532 ;;;; Cross table find functions
534 (defun find-ucon-tui (tui &key (srl *current-srl*))
535 "Find list of ucon for tui"
536 (ensure-tui-integer tui)
537 (with-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
538 (find-ucon-cui (ensure-integer cui) :srl srl)))
540 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
541 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
542 (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
543 :lrl 'klrl :order '(cui asc))
544 (find-ucon-cui cui :srl srl)))
546 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
547 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
548 (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
549 :lrl 'klrl :order '(cui asc))
550 (find-ucon-cui cui :srl srl)))
552 (defun find-ustr-word (word &key (srl *current-srl*))
553 "Return list of ustrs that match word"
554 (with-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
555 :order (cui asc sui asc))
556 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
558 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
559 "Return list of ustrs that match word"
560 (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
561 :order (cui asc sui asc))
562 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
564 (defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
565 "Return list of ucons that match non-english word"
566 (with-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
567 :distinct t :lrl 'klrl :order '(cui asc))
568 (find-ucon-cui cui :srl srl)))
570 (defun find-ustr-noneng-word (word &key (srl *current-srl*))
571 "Return list of ustrs that match non-english word"
572 (with-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
573 :order (cui asc sui asc))
574 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
578 (defun find-usrl-all ()
579 (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
580 (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
582 ;;; Multiword lookup and score functions
584 (defun find-ucon-multiword (str &key (srl *current-srl*))
585 "Return sorted list of ucon's that match a multiword string"
586 (let* ((words (delimited-string-to-list str #\space))
589 (setq ucons (append ucons (find-ucon-word word :srl srl))))
590 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
592 (defun find-ustr-multiword (str &key (srl *current-srl*))
593 "Return sorted list of ustr's that match a multiword string"
594 (let* ((words (delimited-string-to-list str #\space))
597 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
598 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
600 (defun sort-score-ucon-str (str ucons)
601 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
602 (sort-score-umlsclass-str ucons str #'pfstr))
604 (defun sort-score-ustr-str (str ustrs)
605 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
606 (sort-score-umlsclass-str ustrs str #'str))
608 (defun sort-score-umlsclass-str (objs str lookup-func)
609 "Sort a list of objects based on scoring to a string"
612 (push (list obj (score-multiword-match str (funcall lookup-func obj)))
614 (mapcar #'car (sort scored #'> :key #'cadr))))
616 (defun score-multiword-match (s1 s2)
617 "Score a match between two strings with s1 being reference string"
618 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
619 (word-list-2 (delimited-string-to-list s2 #\space))
620 (n1 (length word-list-1))
621 (n2 (length word-list-2))
626 short-list long-list)
627 (declare (fixnum n1 n2 nshort nlong score unmatched))
632 (setq long-list word-list-1)
633 (setq short-list word-list-2))
637 (setq long-list word-list-2)
638 (setq short-list word-list-1)))
639 (decf score (- nlong nshort)) ;; reduce score for extra words
640 (dotimes (iword nshort)
641 (declare (fixnum iword))
642 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
644 (incf score (- 10 (abs (- kmrcl::it iword))))
646 (decf score (* 2 unmatched))
650 ;;; LEX SQL functions
652 (defun find-lexterm-eui (eui)
653 (ensure-eui-integer eui)
654 (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
655 (make-instance 'lexterm :eui eui :wrd wrd)))
657 (defun find-lexterm-word (wrd)
658 (with-umlisp-query (lrwd (eui) nil wrd wrd)
659 (make-instance 'lexterm :eui (ensure-integer eui)
660 :wrd (copy-seq wrd))))
662 ;; LEX SQL Read functions
664 (defun find-labr-eui (eui)
665 (ensure-eui-integer eui)
666 (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
667 (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
668 :eui2 (ensure-integer eui2))))
670 (defun find-labr-bas (bas)
671 (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
672 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
673 :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
675 (defun find-lagr-eui (eui)
676 (ensure-eui-integer eui)
677 (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
678 (make-instance 'lagr :eui eui :str str :sca sca :agr agr
681 (defun find-lcmp-eui (eui)
682 (ensure-eui-integer eui)
683 (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
684 (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
686 (defun find-lmod-eui (eui)
687 (ensure-eui-integer eui)
688 (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
689 (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
691 (defun find-lnom-eui (eui)
692 (ensure-eui-integer eui)
693 (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
694 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
695 :eui2 (ensure-integer eui2))))
697 (defun find-lprn-eui (eui)
698 (ensure-eui-integer eui)
699 (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
700 (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
701 :cas cas :pos pos :qnt qnt :fea fea)))
703 (defun find-lprp-eui (eui)
704 (ensure-eui-integer eui)
705 (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
706 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
708 (defun find-lspl-eui (eui)
709 (ensure-eui-integer eui)
710 (with-umlisp-query (lrspl (spv bas) nil eui eui)
711 (make-instance 'lspl :eui eui :spv spv :bas bas)))
713 (defun find-ltrm-eui (eui)
714 (ensure-eui-integer eui)
715 (with-umlisp-query (lrtrm (bas gen) nil eui eui)
716 (make-instance 'ltrm :eui eui :bas bas :gen gen)))
718 (defun find-ltyp-eui (eui)
719 (ensure-eui-integer eui)
720 (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
721 (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
723 (defun find-lwd-wrd (wrd)
724 (make-instance 'lwd :wrd
725 :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
726 (ensure-integer eui))))
728 ;;; Semantic Network SQL access functions
730 (defun find-sdef-ui (ui)
731 (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
733 (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
734 :def def :ex ex :un un :rh rh :abr abr :rin rin)))
736 (defun find-sstre1-ui (ui)
737 (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
738 (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
739 :ui3 (ensure-integer ui3))))
741 (defun find-sstre1-ui2 (ui2)
742 (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
743 (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
744 :ui3 (ensure-integer ui3))))
746 (defun find-sstr-rl (rl)
747 (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
748 (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
750 (defun find-sstre2-sty (sty)
751 (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
752 (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
754 (defun find-sstr-styrl (styrl)
755 (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
756 (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))