r3474: *** empty log message ***
[umlisp.git] / composite.lisp
1 ;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          composite.lisp
6 ;;;; Purpose:       Composite Classes for UMLisp
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: composite.lisp,v 1.16 2002/11/25 07:45:36 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 (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
21
22
23 ;;; Semantic type constants
24
25 (defun find-tui-word (words)
26   (aif (car (find-usty-word words))
27        (tui it)
28        nil))
29 (memoize 'find-tui-word)
30
31 (defun tui-disease-or-syndrome ()
32   (find-tui-word "disease or syndrome"))
33 (defun tui-sign-or-symptom () 
34   (find-tui-word "sign or symptom"))
35 (defun tui-finding ()
36   (find-tui-word "finding"))
37
38
39 ;;;; Related concepts with specific tui lookup functions
40
41 (defun ucon-is-tui? (ucon tui)
42   "Returns t if ucon has a semantic type of tui"
43   (find tui (s#sty ucon) :key #'tui))
44
45 (defun find-ucon2-tui (ucon tui cui2-func related-con-func)
46   "Returns a list of related ucons that have specific tui"
47   (remove-duplicates 
48    (filter
49     #'(lambda (c) 
50         (aif (funcall cui2-func c)
51              (let ((ucon2 (find-ucon-cui it)))
52                (when (ucon-is-tui? ucon2 tui)
53                  ucon2))
54              nil))
55     (funcall related-con-func ucon))
56    :key #'cui))
57
58 (defun find-ucon2-coc-tui (ucon tui)
59   "Return list of ucon's that have co-occuring concepts of semantic type tui"
60   (find-ucon2-tui ucon tui #'cui2 #'s#coc))
61   
62 (defun find-ucon2-rel-tui (ucon tui)
63   "Return list of ucon's that have related concepts to ucon and semantic type tui"
64   (find-ucon2-tui ucon tui #'cui2 #'s#rel))
65
66 ;;; Composite Objects
67
68 (defclass freq (hyperobject)
69   ((freq :type integer :initarg :freq :accessor freq :print-formatter fmt-comma-integer))
70   (:metaclass hyperobject-class)
71   (:default-initargs :freq 0)
72   (:title "Frequency class")
73   (:print-slots freq)
74   (:description "Base class containing frequency slot, used for multi-inherited objects"))
75
76 (defclass ucon_freq (ucon freq)
77   ()
78   (:metaclass hyperobject-class)
79   (:title "Concept and Count")
80   (:print-slots cui freq pfstr)
81   (:description "Composite object of ucon/freq"))
82
83 (defclass ustr_freq (ustr freq)
84   ()
85   (:metaclass hyperobject-class)
86   (:title "String and Count")
87   (:print-slots sui freq stt lrl str)
88   (:description "Composite object of ustr/freq"))
89
90 (defclass usty_freq (usty freq)
91   ((freq :type fixnum :initarg :freq :accessor freq))
92   (:metaclass hyperobject-class)
93   (:title "Semantic Type and Count")
94   (:print-slots tui freq sty)
95   (:description "Composite object of usty/freq"))
96
97 (defun find-usty_freq-all ()
98   (let ((usty_freqs '()))
99     (dolist (tuple (mutex-sql-query "select distinct TUI from MRSTY"))
100       (let* ((tui (car tuple))
101              (freq (ensure-integer 
102                      (caar (mutex-sql-query 
103                             (format nil "select count(*) from MRSTY where TUI=~a" tui))))))
104         (push (make-instance 'usty_freq :usty (find-usty-tui tui) :freq freq) usty_freqs)))
105     (sort usty_freqs #'> :key #'freq)))
106
107
108 (defclass usrl_freq (usrl freq)
109   ()
110   (:metaclass hyperobject-class)
111   (:title "Source and Count")
112   (:print-slots sab freq srl)
113   (:description "Composite object of usrl/freq"))
114
115 ;; Frequency finding functions
116
117 (defun find-usrl_freq-all ()
118   (let ((freqs '()))
119     (dolist (usrl (find-usrl-all))
120       (let ((freq (ensure-integer 
121                    (caar (mutex-sql-query 
122                           (format nil "select count(*) from MRSO where SAB='~a'" 
123                                   (sab usrl)))))))
124         (push (make-instance 'usrl_freq :usrl usrl :freq freq) freqs)))
125     (sort freqs #'> :key #'freq)))
126
127 (defun find-ucon2_freq-coc-tui (ucon tui)
128 "Return sorted list of tuples with ucon and freq that have co-occuring concepts of semantic type tui" 
129   (let ((ucon_freqs '())) 
130     (dolist (ucoc (s#coc ucon)) 
131       (aif (cui2 ucoc) 
132            (let ((ucon2 (find-ucon-cui it))) 
133              (when (ucon-is-tui? ucon2 tui)
134                (push (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
135                                     :pfstr (pfstr ucon2) :freq (cof ucoc)) 
136                      ucon_freqs)))))
137     (setq ucon_freqs (delete-duplicates ucon_freqs :key #'cui))
138     (sort ucon_freqs #'> :key #'freq)))
139  
140 (defun find-ucon2-str&sty (str sty lookup-func)
141   "Call lookup-func for ucon and usty for given str and sty"
142   (let ((ucon (car (find-ucon-str str)))
143         (usty (car (find-usty-word sty))))
144     (if (and ucon usty)
145         (funcall lookup-func ucon (tui usty))
146       nil)))
147   
148 (defun find-ucon2-coc-str&sty (str sty)
149   "Find all ucons that are a co-occuring concept for concept named str
150    and that have semantic type of sty"
151   (find-ucon2-str&sty str sty #'find-ucon2-coc-tui))
152
153 (defun find-ucon2-rel-str&sty (str sty)
154   "Find all ucons that are a relationship to concept named str
155    and that have semantic type of sty"
156   (find-ucon2-str&sty str sty #'find-ucon2-rel-tui))
157
158 ;;; Most common relationships, co-occurances
159
160 (defun find-ucon2_freq-tui-all (tui ucon2-tui-func)
161   "Return sorted list of all ucon2 that have a semantic type tui with ucon that is also has sty of tui"
162   (let ((ucon_freqs (make-array (1+ (find-cui-max)) :initial-element nil)))
163     (dolist (ucon (find-ucon-tui tui)) ;; for all disease-or-syn
164       (dolist (ucon2 (funcall ucon2-tui-func ucon tui)) ;; for each related disease
165         (aif (aref ucon_freqs (cui ucon2))
166              (setf (freq it) (1+ (freq it)))
167              (setf (aref ucon_freqs (cui ucon2)) 
168                (make-instance 'ucon_freq :cui (cui ucon2) :lrl (lrl ucon2)
169                               :pfstr (pfstr ucon2) :freq 1)))))
170     (let ((ucon_freq-list '()))
171       (dotimes (i (find-cui-max))
172         (declare (fixnum i))
173         (awhen (aref ucon_freqs i)
174              (push it ucon_freq-list)))
175       (sort ucon_freq-list #'> :key #'freq))))
176
177 (defun find-ucon2_freq-rel-tui-all (tui)
178   "Sorted list of ucon_freq with semantic type tui that are rel's of ucons with semantic type tui"
179   (find-ucon2_freq-tui-all tui #'find-ucon2-rel-tui))
180
181 (defun find-ucon2_freq-coc-tui-all (tui)
182   (find-ucon2_freq-tui-all tui #'find-ucon2-coc-tui))
183
184 #+(or cmu scl)
185 (dolist (c '(ucon_freq ustr_freq usty_freq usrl_freq))
186   (let ((cl #+cmu (pcl:find-class c)
187             #+scl (find-class c)))
188     #+cmu (pcl:finalize-inheritance cl)
189     #+scl (clos:finalize-inheritance cl)))