X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=parse-macros.lisp;h=490934fd79fa3836794cceac703a6ef665390849;hb=28aeae6f894ac1e2b4ded59af9371b373e38a701;hp=902b3cf5f2f6d1e4c73627fea618068b932d3a90;hpb=2038ce2f480179fd23cb3ded6fa1250e61e22029;p=umlisp.git diff --git a/parse-macros.lisp b/parse-macros.lisp index 902b3cf..490934f 100644 --- a/parse-macros.lisp +++ b/parse-macros.lisp @@ -2,15 +2,13 @@ ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; -;;;; Name: parse-macros.lisp -;;;; Purpose: Macros for UMLS file parsing -;;;; Author: Kevin M. Rosenberg -;;;; Date Started: Apr 2000 -;;;; -;;;; $Id: parse-macros.lisp,v 1.8 2003/06/10 22:30:16 kevin Exp $ +;;;; Name: parse-macros.lisp +;;;; Purpose: Macros for UMLS file parsing +;;;; Author: Kevin M. Rosenberg +;;;; Created: Apr 2000 ;;;; ;;;; This file, part of UMLisp, is -;;;; Copyright (c) 2000-2003 by Kevin M. Rosenberg, M.D. +;;;; Copyright (c) 2000-2010 by Kevin M. Rosenberg, M.D. ;;;; ;;;; UMLisp users are granted the rights to distribute and use this software ;;;; as governed by the terms of the GNU General Public License. @@ -18,43 +16,105 @@ (in-package #:umlisp) -(defmacro with-umls-file ((line filename) &body body) -"Opens a UMLS and processes each parsed line with (body) argument" +(defun read-umls-line (strm &optional (eof 'eof)) + "Read a line from a UMLS stream, split into fields" + (let ((line (read-line strm nil eof))) + (if (eq line eof) + eof + (delimited-string-to-list line #\| t)))) + +(defun source-files (path) + (if (probe-file path) + (list path) + (sort + (directory (make-pathname :defaults path + :type :wild + :name (concatenate 'string (pathname-name path) + (aif (pathname-type path) + (concatenate 'string "." it) + "")))) + #'(lambda (a b) + (string-lessp (pathname-type a) (pathname-type b)))))) + +(defmacro with-buffered-reading-umls-file ((line path) &body body) + "Opens a UMLS and processes each parsed line with (body) argument" (let ((ustream (gensym "STRM-")) - (eof (gensym "EOF-"))) - `(let ((,eof (gensym "EOFSYM-"))) - (with-open-file - (,ustream (umls-pathname ,filename) :direction :input) - (do ((,line (read-umls-line ,ustream ,eof) - (read-umls-line ,ustream ,eof))) - ((eq ,line ,eof) t) - ,@body))))) + (buffer (gensym "BUF-")) + (eof (gensym "EOF-")) + (files (gensym "FILES-"))) + `(let ((,eof (gensym "EOFSYM-")) + (,buffer (make-fields-buffer)) + (,files (source-files ,path))) + (with-open-file (,ustream (first ,files) :direction :input + #+(and sbcl sb-unicode) :external-format + #+(and sbcl sb-unicode) :UTF-8 + #+(and allegro ics) :external-format + #+(and allegro ics) :UTF-8 + #+lispworks :external-format + #+lispworks :UTF-8 + #+(and clisp unicode) :external-format + #+(and clisp unicode) charset:utf-8) + (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof) + (read-buffered-fields ,buffer ,ustream #\| ,eof))) + ((eq ,line ,eof) t) + (setq ,line (coerce ,line 'list)) + (print ,line) + ,@body))))) -(defmacro with-buffered-umls-file ((line filename) &body body) +(defmacro with-reading-umls-file ((line path) &body body) "Opens a UMLS and processes each parsed line with (body) argument" (let ((ustream (gensym "STRM-")) - (buffer (gensym "BUF-")) - (eof (gensym "EOF-"))) - `(let ((,buffer (make-fields-buffer)) - (,eof (gensym "EOFSYM-"))) - (with-open-file - (,ustream (umls-pathname ,filename) :direction :input) - (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof) - (read-buffered-fields ,buffer ,ustream #\| ,eof))) - ((eq ,line ,eof) t) - ,@body))))) + (eof (gensym "EOF-")) + (files (gensym "FILES-"))) + `(let ((,eof (gensym "EOFSYM-")) + (,files (source-files ,path))) + (unless ,files + (error "Can't find files for ~A~%" (namestring ,path))) + (with-open-file (,ustream (first ,files) :direction :input + #+(and sbcl sb-unicode) :external-format + #+(and sbcl sb-unicode) :UTF-8 + #+(and allegro ics) :external-format + #+(and allegro ics) :UTF-8 + #+lispworks :external-format + #+lispworks :UTF-8 + #+(and clisp unicode) :external-format + #+(and clisp unicode) charset:utf-8) + (do ((,line (read-umls-line ,ustream ,eof) + (read-umls-line ,ustream ,eof))) + ((eq ,line ,eof) t) + (locally (declare (type list ,line)) + ,@body)))))) -(defmacro with-buffered2-umls-file ((line filename) &body body) +(defmacro with-umls-ufile ((line ufile) &body body) + "Opens a UMLS and processes each parsed line with (body) argument" + `(with-reading-umls-file (,line (ufile-pathname ,ufile)) + ,@body)) + +(defmacro with-umls-file ((line ufile) &body body) + "Opens a UMLS and processes each parsed line with (body) argument" + `(with-reading-umls-file (,line (umls-pathname ,ufile)) + ,@body)) + +(defmacro with-buffered-umls-file ((line filename) &body body) "Opens a UMLS and processes each parsed line with (body) argument" (let ((ustream (gensym "STRM-")) - (buffer (gensym "BUF-")) - (eof (gensym "EOF-"))) - `(let ((,buffer (make-fields-buffer2)) - (,eof (gensym "EOFSYM-"))) + (buffer (gensym "BUF-")) + (eof (gensym "EOF-"))) + `(let ((,buffer (make-fields-buffer)) + (,eof (gensym "EOFSYM-"))) (with-open-file - (,ustream (umls-pathname ,filename) - :direction :input :if-exists :overwrite) - (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof) - (read-buffered-fields ,buffer ,ustream #\| ,eof))) - ((eq ,line ,eof) t) - ,@body))))) + (,ustream (umls-pathname ,filename) :direction :input + #+(and sbcl sb-unicode) :external-format + #+(and sbcl sb-unicode) :UTF-8 + #+(and allegro ics) :external-format + #+(and allegro ics) :UTF-8 + #+lispworks :external-format + #+lispworks :UTF-8 + #+(and clisp unicode) :external-format + #+(and clisp unicode) charset:utf-8) + (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof) + (read-buffered-fields ,buffer ,ustream #\| ,eof))) + ((eq ,line ,eof) t) + ,@body))))) + +