;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; This file contains utility macros and functions for helping read ;; strings ;;; A simple macro for reading in lines from a file. It ;;; is designed to look like (dotimes) and (dolist) ;;; Example: ;;; (dolines (line "testfile") ;;; (format t "~a~%" line)) (defmacro dolines ((var filename &optional ret) &body body) (let ((stream (gensym))) `(with-open-file (,stream ,filename :direction :input) (while (setf ,var (read-line ,stream nil nil)) ,@body) ,ret))) ;;; A simple function for getting all the keys from a hashtable (defun get-hash-keys (hashtable) (let ((ret nil)) (maphash #'(lambda (key val) (push key ret)) hashtable) ret)) ;;; A simple macro for groveling over the contents of a hash table. It ;;; is designed to look like (dotimes) and (dolist) ;;; Example: ;;; (dohash (key val hashtable) ;;; (format t "~a -> ~a~%" key val)) (defmacro dohash ((key val ht &optional ret) &body body) (let ((keys (gensym))) `(let ((,keys (get-hash-keys ,ht))) (dolist (,key ,keys ,ret) (let ((,val (gethash ,key ,ht))) ,@body))))) ;;; A simple function that takes a character and returns a value from ;;; 0-27, where 1-26 maps to A-Z (case insensitive) and everything else ;;; maps to 0 (defun character-to-index (char) (if (alpha-char-p char) (+ (- (char-int (char-downcase char)) (char-int #\a)) 1) 0)) ;;; A simple function that takes a string and returns an array ;;; of the character-to-index values (defun string-to-index (string) (let* ((stop (length string)) (ret (make-array stop))) (dotimes (i stop ret) (setf (aref ret i) (character-to-index (aref string i)))))) ;;;; Some sample runs I did: ;;;; Notice that the sentences begin with a " ". ;;;; It actually makes a difference. Check out the difference between ;;;; "hello" and " hello". Also, notice that these 'sentences' are just ;;;; words. You'd really want to test with sentences. #| (predict " hola") (("spanish" 0.6765656) ("english" 0.1993123) ("hiphop" 0.118951924) ("french" 0.0051645134) ("lisp" 5.6704903e-6)) (predict " hello") (("hiphop" 0.5174973) ("english" 0.45761773) ("spanish" 0.02424061) ("french" 6.437539e-4) ("lisp" 5.318387e-7)) (predict "hello") (("english" 0.50267637) ("hiphop" 0.44940448) ("spanish" 0.039834227) ("french" 0.007994719) ("lisp" 9.016024e-5)) (predict " death") (("english" 0.6923835) ("hiphop" 0.30310503) ("french" 0.003657153) ("lisp" 5.40979e-4) ("spanish" 3.1325946e-4)) (predict " rhyme") (("hiphop" 0.9995462) ("english" 3.4074156e-4) ("lisp" 9.9734716e-5) ("french" 1.3035273e-5) ("spanish" 4.2282585e-7)) (predict " defun") (("lisp" 0.99575996) ("french" 0.003839893) ("english" 2.776178e-4) ("spanish" 8.14848e-5) ("hiphop" 4.0968436e-5)) (predict " charles") (("french" 0.5482738) ("spanish" 0.22672346) ("english" 0.22445887) ("hiphop" 5.437327e-4) ("lisp" 1.1742197e-7)) (predict " ashok") (("english" 0.7762349) ("hiphop" 0.223395) ("spanish" 2.2333201e-4) ("french" 1.4665788e-4) ("lisp" 4.945446e-8)) (predict "charles") (("english" 0.43924308) ("french" 0.40395173) ("spanish" 0.15613052) ("hiphop" 6.744762e-4) ("lisp" 1.7011958e-7)) (predict "ashok") (("english" 0.75808936) ("hiphop" 0.24133347) ("spanish" 4.655121e-4) ("french" 1.1154039e-4) ("lisp" 5.4603685e-8)) (predict " french") (("english" 0.62157315) ("french" 0.19474714) ("spanish" 0.11906079) ("hiphop" 0.064609304) ("lisp" 9.547511e-6)) (predict " fries") (("french" 0.5804049) ("spanish" 0.23300804) ("english" 0.16763599) ("hiphop" 0.01885327) ("lisp" 9.777243e-5)) |#