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.76 2003/05/11 21:48:31 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 (defmacro ensure-cui-integer (cui)
160 (setq ,cui (parse-cui ,cui))
163 (defmacro ensure-lui-integer (lui)
165 (setq ,lui (parse-lui ,lui))
168 (defmacro ensure-sui-integer (sui)
170 (setq ,sui (parse-sui ,sui))
173 (defmacro ensure-tui-integer (tui)
175 (setq ,tui (parse-tui ,tui))
178 (defmacro ensure-eui-integer (eui)
180 (setq ,eui (parse-eui ,eui))
183 (defun find-ucon-cui (cui &key (srl *current-srl*))
184 "Find ucon for a cui"
185 (ensure-cui-integer cui)
186 (with-umlisp-query (mrcon (kpfstr kcuilrl) srl cui cui :single t)
187 (make-instance 'ucon :cui cui :pfstr kpfstr
188 :lrl (ensure-integer kcuilrl))))
190 (defun find-ucon-cui-sans-pfstr (cui &key (srl *current-srl*))
191 "Find ucon for a cui"
192 (ensure-cui-integer cui)
193 (with-umlisp-query (mrcon (kcuilrl) srl cui cui :single t)
194 (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
197 (defun find-pfstr-cui (cui &key (srl *current-srl*))
198 "Find preferred string for a cui"
199 (ensure-cui-integer cui)
200 (with-umlisp-query (mrcon (kpfstr) srl cui cui :single t)
203 (defun find-ucon-lui (lui &key (srl *current-srl*))
204 "Find list of ucon for lui"
205 (ensure-lui-integer lui)
206 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl lui lui
208 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
209 :lrl (ensure-integer kcuilrl))))
211 (defun find-ucon-sui (sui &key (srl *current-srl*))
212 "Find list of ucon for sui"
213 (ensure-sui-integer sui)
214 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl sui sui :distinct t)
215 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
216 :lrl (ensure-integer kcuilrl))))
218 (defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
219 "Find ucon for cui/sui"
220 (ensure-cui-integer cui)
221 (ensure-sui-integer sui)
223 (with-umlisp-query (mrcon (kpfstr kcuilrl) srl kcuisui
224 (make-cuisui cui sui))
225 (make-instance 'ucon :cui cui
227 :lrl (ensure-integer kcuilrl)))))
229 (defun find-ucon-str (str &key (srl *current-srl*))
230 "Find ucon that are exact match for str"
231 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl str str :distinct t)
232 (make-instance 'ucon :cui (ensure-integer cui) :pfstr kpfstr
233 :lrl (ensure-integer kcuilrl))))
235 (defun find-ucon-all (&key (srl *current-srl*))
236 "Return list of all ucon's"
237 (with-sql-connection (db)
240 #'(lambda (cui pfstr cuilrl)
241 (make-instance 'ucon :cui (ensure-integer cui)
243 :lrl (ensure-integer cuilrl)))
244 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil
245 :order (cui asc) :distinct t)
248 (defun find-ucon-all2 (&key (srl *current-srl*))
249 "Return list of all ucon's"
250 (with-umlisp-query (mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
252 (make-instance 'ucon :cui (ensure-integer cui)
254 :lrl (ensure-integer kcuilrl))))
256 (defun map-ucon-all (fn &key (srl *current-srl*))
257 "Map a function over all ucon's"
258 (with-sql-connection (db)
261 #'(lambda (cui pfstr cuilrl)
263 (make-instance 'ucon :cui (ensure-integer cui)
265 :lrl (ensure-integer cuilrl))))
266 (query-string mrcon (cui kpfstr kcuilrl) srl nil nil :order (cui asc)
271 (defun find-udef-cui (cui &key (srl *current-srl*))
272 "Return a list of udefs for cui"
273 (ensure-cui-integer cui)
274 (with-umlisp-query (mrdef (sab def) srl cui cui :lrl "KSRL")
275 (make-instance 'udef :sab sab :def def)))
277 (defun find-usty-cui (cui &key (srl *current-srl*))
278 "Return a list of usty for cui"
279 (ensure-cui-integer cui)
280 (with-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
281 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
283 (defun find-usty-word (word &key (srl *current-srl*))
284 "Return a list of usty that match word"
285 (with-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
287 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
289 (defun find-urel-cui (cui &key (srl *current-srl*))
290 "Return a list of urel for cui"
291 (ensure-cui-integer cui)
292 (with-umlisp-query (mrrel (rel cui2 rela sab sl mg kpfstr2) srl cui1
294 (make-instance 'urel :cui1 cui :rel rel
295 :cui2 (ensure-integer cui2) :rela rela :sab sab :sl sl
296 :mg mg :pfstr2 kpfstr2)))
298 (defun find-urel-cui2 (cui2 &key (srl *current-srl*))
299 "Return a list of urel for cui2"
300 (ensure-cui-integer cui2)
301 (with-umlisp-query (mrrel (rel cui1 rela sab sl mg kpfstr2) srl cui2
303 (make-instance 'urel :cui2 cui2 :rel rel
304 :cui1 (ensure-integer cui1) :rela rela :sab sab :sl sl
305 :mg mg :pfstr2 kpfstr2)))
307 (defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
308 (ensure-cui-integer cui2)
309 (loop for cui in (remove-duplicates
310 (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
311 collect (find-ucon-cui cui :srl srl)))
313 (defun find-ucoc-cui (cui &key (srl *current-srl*))
314 "Return a list of ucoc for cui"
315 (ensure-cui-integer cui)
316 (with-umlisp-query (mrcoc (cui2 soc cot cof coa kpfstr2) srl cui1
317 cui :lrl klrl :order (cof asc))
318 (setq cui2 (ensure-integer cui2))
319 (when (zerop cui2) (setq cui2 nil))
320 (make-instance 'ucoc :cui1 cui :cui2 (ensure-integer cui2)
321 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
324 (defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
325 "Return a list of ucoc for cui2"
326 (ensure-cui-integer cui2)
327 (with-umlisp-query (mrcoc (cui1 soc cot cof coa kpfstr2) srl cui2
328 cui2 :lrl klrl :order (cof asc))
329 (when (zerop cui2) (setq cui2 nil))
330 (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
331 :soc soc :cot cot :cof (ensure-integer cof) :coa coa
334 (defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
335 "List of ucon with co-occurance cui2"
336 (ensure-cui-integer cui2)
338 #'(lambda (cui) (find-ucon-cui cui :srl srl))
339 (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))
341 (defun find-ulo-cui (cui &key (srl *current-srl*))
342 "Return a list of ulo for cui"
343 (ensure-cui-integer cui)
344 (with-umlisp-query (mrlo (isn fr un sui sna soui) srl cui cui
346 (make-instance 'ulo :isn isn :fr (ensure-integer fr) :un un
347 :sui (ensure-integer sui) :sna sna :soui soui)))
349 (defun find-uatx-cui (cui &key (srl *current-srl*))
350 "Return a list of uatx for cui"
351 (ensure-cui-integer cui)
352 (with-umlisp-query (mratx (sab rel atx) srl cui cui :lrl ksrl)
353 (make-instance 'uatx :sab sab :rel rel :atx atx)))
356 (defun find-uterm-cui (cui &key (srl *current-srl*))
357 "Return a list of uterm for cui"
358 (ensure-cui-integer cui)
359 (with-umlisp-query (mrcon (lui lat ts kluilrl) srl cui cui
360 :lrl kluilrl :distinct t)
361 (make-instance 'uterm :lui (ensure-integer lui) :cui cui
362 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
364 (defun find-uterm-lui (lui &key (srl *current-srl*))
365 "Return a list of uterm for lui"
366 (ensure-lui-integer lui)
367 (with-umlisp-query (mrcon (cui lat ts kluilrl) srl lui lui
368 :lrl kluilrl :distinct t)
369 (make-instance 'uterm :cui (ensure-integer cui) :lui lui
370 :lat lat :ts ts :lrl (ensure-integer kluilrl))))
372 (defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
373 "Return single uterm for cui/lui"
374 (ensure-cui-integer cui)
375 (ensure-lui-integer lui)
376 (with-umlisp-query (mrcon (lat ts kluilrl) srl kcuilui
377 (make-cuilui cui lui)
378 :lrl kluilrl :single t)
379 (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
380 :lrl (ensure-integer kluilrl))))
382 (defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
383 "Return a list of ustr for cui/lui"
384 (ensure-cui-integer cui)
385 (ensure-lui-integer lui)
386 (with-umlisp-query (mrcon (sui stt str lrl) srl kcuilui
387 (make-cuilui cui lui) :lrl lrl)
388 (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
389 :cuisui (make-cuisui cui sui) :stt stt :str str
390 :lrl (ensure-integer lrl))))
392 (defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
393 "Return the single ustr for cuisui"
394 (ensure-cui-integer cui)
395 (ensure-sui-integer sui)
396 (with-umlisp-query (mrcon (lui stt str lrl) srl kcuisui
397 (make-cuisui cui sui) :lrl lrl :single t)
398 (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
399 :lui (ensure-integer lui) :stt stt :str str
400 :lrl (ensure-integer lrl))))
402 (defun find-ustr-sui (sui &key (srl *current-srl*))
403 "Return the list of ustr for sui"
404 (ensure-sui-integer sui)
405 (with-umlisp-query (mrcon (cui lui stt str lrl) srl sui sui
407 (make-instance 'ustr :sui sui :cui cui :stt stt :str str
408 :cuisui (make-cuisui (ensure-integer cui) sui)
409 :lui (ensure-integer lui) :lrl (ensure-integer lrl))))
411 (defun find-ustr-sab (sab &key (srl *current-srl*))
412 "Return the list of ustr for sab"
413 (with-umlisp-query (mrso (kcuisui) srl sab sab :lrl srl)
414 (let ((cuisui (ensure-integer kcuisui)))
415 (apply #'find-ustr-cuisui
417 (multiple-value-list (decompose-cuisui cuisui))
420 (defun find-ustr-all (&key (srl *current-srl*))
421 "Return list of all ustr's"
422 (with-sql-connection (db)
425 #'(lambda (cui lui sui stt lrl pfstr)
426 (make-instance 'ustr :cui (ensure-integer cui)
427 :lui (ensure-integer lui) :sui (ensure-integer sui)
429 :cuisui (make-cuisui (ensure-integer cui)
430 (ensure-integer sui))
431 :lrl (ensure-integer lrl)))
432 (query-string mrcon (cui lui sui stt lrl kpfstr) srl nil nil :lrl lrl
437 (defun find-string-sui (sui &key (srl *current-srl*))
438 "Return the string associated with sui"
439 (ensure-sui-integer sui)
440 (with-umlisp-query (mrcon (str) srl sui sui :lrl lrl :single t)
443 (defun find-uso-cuisui (cui sui &key (srl *current-srl*))
444 (ensure-sui-integer sui)
445 (ensure-cui-integer cui)
446 (with-umlisp-query (mrso (sab code srl tty) srl kcuisui
447 (make-cuisui cui sui) :lrl srl)
448 (make-instance 'uso :sab sab :code code :srl srl :tty tty)))
450 (defun find-ucxt-cuisui (cui sui &key (srl *current-srl*))
451 (ensure-cui-integer cui)
452 (ensure-sui-integer sui)
453 (with-umlisp-query (mrcxt (sab code cxn cxl rnk cxs cui2 hcd rela xc)
454 srl kcuisui (make-cuisui cui sui) :lrl ksrl)
455 (make-instance 'ucxt :sab sab :code code
456 :cxn (ensure-integer cxn) :cxl cxl :cxs cxs :hcd hcd
457 :rela rela :xc xc :rnk (ensure-integer rnk)
458 :cui2 (ensure-integer cui2))))
460 (defun find-usat-ui (cui &optional (lui nil) (sui nil) &key (srl *current-srl*))
461 (ensure-cui-integer cui)
462 (ensure-lui-integer lui)
463 (ensure-sui-integer sui)
464 (let ((ls (format nil "select CODE,ATN,SAB,ATV from MRSAT where ")))
466 (sui (string-append ls (format nil "KCUISUI=~D" (make-cuisui cui sui))))
467 (lui (string-append ls (format nil "KCUILUI=~D and sui=0"
468 (make-cuilui cui lui))))
469 (t (string-append ls (format nil "cui=~D and lui=0 and sui=0" cui))))
471 (string-append ls (format nil " and KSRL <= ~D" srl)))
472 (loop for tuple in (mutex-sql-query ls) collect
473 (destructuring-bind (code atn sab atv) tuple
474 (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))
476 (defun find-usty-tui (tui)
478 (ensure-tui-integer tui)
479 (with-umlisp-query (mrsty (sty) nil tui tui :single t)
480 (make-instance 'usty :tui tui :sty sty)))
482 (defun find-usty-sty (sty)
483 "Find usty for a sty"
484 (with-umlisp-query (mrsty (tui) nil sty sty :single t)
485 (make-instance 'usty :tui (ensure-integer tui) :sty sty)))
487 (defun find-usty-all ()
488 "Return list of usty's for all semantic types"
489 (with-umlisp-query (mrsty (tui) nil nil nil :distinct t)
490 (find-usty-tui tui)))
492 (defun find-usab-all ()
493 "Find usab for a key"
494 (with-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver mstart mend imeta
495 rmeta slc scc srl tfr cfr cxty ttyl atnl lat
496 cenc curver sabin) nil nil nil)
497 (make-instance 'usab :vcui (ensure-integer vcui)
498 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
499 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
500 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
501 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
502 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
503 :curver curver :sabin sabin)))
505 (defun find-usab-by-key (key-name key)
506 "Find usab for a key"
507 (with-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver mstart
508 mend imeta rmeta slc scc srl tfr cfr cxty
509 ttyl atnl lat cenc curver sabin)
510 nil key-name key :single t)
511 (make-instance 'usab :vcui (ensure-integer vcui)
512 :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
513 :sf sf :sver sver :mstart mstart :mend mend :imeta imeta
514 :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
515 :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
516 :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
517 :curver curver :sabin sabin)))
519 (defun find-usab-rsab (rsab)
521 (find-usab-by-key 'rsab rsab))
523 (defun find-usab-vsab (vsab)
525 (find-usab-by-key 'vsab vsab))
527 (defun find-cui-max ()
528 (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))
530 ;;;; Cross table find functions
532 (defun find-ucon-tui (tui &key (srl *current-srl*))
533 "Find list of ucon for tui"
534 (ensure-tui-integer tui)
535 (with-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
536 (find-ucon-cui (ensure-integer cui) :srl srl)))
538 (defun find-ucon-word (word &key (srl *current-srl*) (like nil))
539 "Return list of ucons that match word. Optionally, use SQL's LIKE syntax"
540 (with-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
541 :lrl 'klrl :order '(cui asc))
542 (find-ucon-cui cui :srl srl)))
544 (defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
545 "Return list of ucons that match word, optionally use SQL's LIKE syntax"
546 (with-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
547 :lrl 'klrl :order '(cui asc))
548 (find-ucon-cui cui :srl srl)))
550 (defun find-ustr-word (word &key (srl *current-srl*))
551 "Return list of ustrs that match word"
552 (with-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
553 :order (cui asc sui asc))
554 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
556 (defun find-ustr-normalized-word (word &key (srl *current-srl*))
557 "Return list of ustrs that match word"
558 (with-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
559 :order (cui asc sui asc))
560 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
562 (defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
563 "Return list of ucons that match non-english word"
564 (with-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
565 :distinct t :lrl 'klrl :order '(cui asc))
566 (find-ucon-cui cui :srl srl)))
568 (defun find-ustr-noneng-word (word &key (srl *current-srl*))
569 "Return list of ustrs that match non-english word"
570 (with-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
571 :order (cui asc sui asc))
572 (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))
576 (defun find-usrl-all ()
577 (with-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
578 (make-instance 'usrl :sab sab :srl (ensure-integer srl))))
580 ;;; Multiword lookup and score functions
582 (defun find-ucon-multiword (str &key (srl *current-srl*))
583 "Return sorted list of ucon's that match a multiword string"
584 (let* ((words (delimited-string-to-list str #\space))
587 (setq ucons (append ucons (find-ucon-word word :srl srl))))
588 (sort-score-ucon-str str (delete-duplicates ucons :test #'eql :key #'cui))))
590 (defun find-ustr-multiword (str &key (srl *current-srl*))
591 "Return sorted list of ustr's that match a multiword string"
592 (let* ((words (delimited-string-to-list str #\space))
595 (setq ustrs (append ustrs (find-ustr-word word :srl srl))))
596 (sort-score-ustr-str str (delete-duplicates ustrs :test #'eql :key #'cui))))
598 (defun sort-score-ucon-str (str ucons)
599 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
600 (sort-score-umlsclass-str ucons str #'pfstr))
602 (defun sort-score-ustr-str (str ustrs)
603 "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
604 (sort-score-umlsclass-str ustrs str #'str))
606 (defun sort-score-umlsclass-str (objs str lookup-func)
607 "Sort a list of objects based on scoring to a string"
610 (push (list obj (score-multiword-match str (funcall lookup-func obj)))
612 (mapcar #'car (sort scored #'> :key #'cadr))))
614 (defun score-multiword-match (s1 s2)
615 "Score a match between two strings with s1 being reference string"
616 (let* ((word-list-1 (delimited-string-to-list s1 #\space))
617 (word-list-2 (delimited-string-to-list s2 #\space))
618 (n1 (length word-list-1))
619 (n2 (length word-list-2))
624 short-list long-list)
625 (declare (fixnum n1 n2 nshort nlong score unmatched))
630 (setq long-list word-list-1)
631 (setq short-list word-list-2))
635 (setq long-list word-list-2)
636 (setq short-list word-list-1)))
637 (decf score (- nlong nshort)) ;; reduce score for extra words
638 (dotimes (iword nshort)
639 (declare (fixnum iword))
640 (kmrcl:aif (position (nth iword short-list) long-list :test #'string-equal)
642 (incf score (- 10 (abs (- kmrcl::it iword))))
644 (decf score (* 2 unmatched))
648 ;;; LEX SQL functions
650 (defun find-lexterm-eui (eui)
651 (ensure-eui-integer eui)
652 (with-umlisp-query (lrwd (wrd) nil eui eui :single t)
653 (make-instance 'lexterm :eui eui :wrd wrd)))
655 (defun find-lexterm-word (wrd)
656 (with-umlisp-query (lrwd (eui) nil wrd wrd)
657 (make-instance 'lexterm :eui (ensure-integer eui)
658 :wrd (copy-seq wrd))))
660 ;; LEX SQL Read functions
662 (defun find-labr-eui (eui)
663 (ensure-eui-integer eui)
664 (with-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
665 (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
666 :eui2 (ensure-integer eui2))))
668 (defun find-labr-bas (bas)
669 (with-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
670 (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
671 :bas (copy-seq bas) :eui2 (ensure-integer eui2))))
673 (defun find-lagr-eui (eui)
674 (ensure-eui-integer eui)
675 (with-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
676 (make-instance 'lagr :eui eui :str str :sca sca :agr agr
679 (defun find-lcmp-eui (eui)
680 (ensure-eui-integer eui)
681 (with-umlisp-query (lrcmp (bas sca com) nil eui eui)
682 (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))
684 (defun find-lmod-eui (eui)
685 (ensure-eui-integer eui)
686 (with-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
687 (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))
689 (defun find-lnom-eui (eui)
690 (ensure-eui-integer eui)
691 (with-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
692 (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
693 :eui2 (ensure-integer eui2))))
695 (defun find-lprn-eui (eui)
696 (ensure-eui-integer eui)
697 (with-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
698 (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
699 :cas cas :pos pos :qnt qnt :fea fea)))
701 (defun find-lprp-eui (eui)
702 (ensure-eui-integer eui)
703 (with-umlisp-query (lrprp (bas str sca fea) nil eui eui)
704 (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))
706 (defun find-lspl-eui (eui)
707 (ensure-eui-integer eui)
708 (with-umlisp-query (lrspl (spv bas) nil eui eui)
709 (make-instance 'lspl :eui eui :spv spv :bas bas)))
711 (defun find-ltrm-eui (eui)
712 (ensure-eui-integer eui)
713 (with-umlisp-query (lrtrm (bas gen) nil eui eui)
714 (make-instance 'ltrm :eui eui :bas bas :gen gen)))
716 (defun find-ltyp-eui (eui)
717 (ensure-eui-integer eui)
718 (with-umlisp-query (lrtyp (bas sca typ) nil eui eui)
719 (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))
721 (defun find-lwd-wrd (wrd)
722 (make-instance 'lwd :wrd
723 :euilist (with-umlisp-query (lrwd (eui) nil wrd wrd)
724 (ensure-integer eui))))
726 ;;; Semantic Network SQL access functions
728 (defun find-sdef-ui (ui)
729 (with-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
731 (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
732 :def def :ex ex :un un :rh rh :abr abr :rin rin)))
734 (defun find-sstre1-ui (ui)
735 (with-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
736 (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
737 :ui3 (ensure-integer ui3))))
739 (defun find-sstre1-ui2 (ui2)
740 (with-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
741 (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
742 :ui3 (ensure-integer ui3))))
744 (defun find-sstr-rl (rl)
745 (with-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
746 (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))
748 (defun find-sstre2-sty (sty)
749 (with-umlisp-query (srstre2 (rl sty2) nil sty sty)
750 (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))
752 (defun find-sstr-styrl (styrl)
753 (with-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
754 (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))