1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: parse-macros.lisp
6 ;;;; Purpose: Macros for UMLS file parsing
7 ;;;; Author: Kevin M. Rosenberg
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2004 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 ;;;; *************************************************************************
21 (defun read-umls-line (strm &optional (eof 'eof))
22 "Read a line from a UMLS stream, split into fields"
23 (let ((line (read-line strm nil eof)))
26 (delimited-string-to-list line #\| t))))
28 (defun source-files (path)
32 (directory (make-pathname :defaults path
34 :name (concatenate 'string (pathname-name path)
35 (aif (pathname-type path)
36 (concatenate 'string "." it)
39 (string-lessp (pathname-type a) (pathname-type b))))))
41 (defmacro with-buffered-reading-umls-file ((line path) &body body)
42 "Opens a UMLS and processes each parsed line with (body) argument"
43 (let ((ustream (gensym "STRM-"))
44 (buffer (gensym "BUF-"))
46 (files (gensym "FILES-")))
47 `(let ((,eof (gensym "EOFSYM-"))
48 (,buffer (make-fields-buffer))
49 (,files (source-files ,path)))
50 (with-open-file (,ustream (first ,files) :direction :input)
51 (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof)
52 (read-buffered-fields ,buffer ,ustream #\| ,eof)))
54 (setq ,line (coerce ,line 'list))
58 (defmacro with-reading-umls-file ((line path) &body body)
59 "Opens a UMLS and processes each parsed line with (body) argument"
60 (let ((ustream (gensym "STRM-"))
62 (files (gensym "FILES-")))
63 `(let ((,eof (gensym "EOFSYM-"))
64 (,files (source-files ,path)))
65 (with-open-file (,ustream (first ,files) :direction :input)
66 (do ((,line (read-umls-line ,ustream ,eof)
67 (read-umls-line ,ustream ,eof)))
71 (defmacro with-umls-ufile ((line ufile) &body body)
72 "Opens a UMLS and processes each parsed line with (body) argument"
73 `(with-reading-umls-file (,line (ufile-pathname ,ufile))
76 (defmacro with-umls-file ((line ufile) &body body)
77 "Opens a UMLS and processes each parsed line with (body) argument"
78 "Opens a UMLS and processes each parsed line with (body) argument"
79 `(with-reading-umls-file (,line (umls-pathname ,ufile))
82 (defmacro with-buffered-umls-file ((line filename) &body body)
83 "Opens a UMLS and processes each parsed line with (body) argument"
84 (let ((ustream (gensym "STRM-"))
85 (buffer (gensym "BUF-"))
86 (eof (gensym "EOF-")))
87 `(let ((,buffer (make-fields-buffer))
88 (,eof (gensym "EOFSYM-")))
90 (,ustream (umls-pathname ,filename) :direction :input)
91 (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof)
92 (read-buffered-fields ,buffer ,ustream #\| ,eof)))