Created
March 17, 2014 00:11
-
-
Save gmcclure/9591779 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage :verbquiz | |
(:use :common-lisp)) | |
(in-package :verbquiz) | |
(defparameter *dictionary* (make-hash-table :test #'equal)) | |
(defun save-dictionary (&key (filename "dictionary.db")) | |
"Save the program *dictionary* in utf-8 format." | |
(with-open-file (out filename | |
:direction :output | |
:if-exists :supersede | |
:external-format :utf-8) | |
(with-standard-io-syntax | |
(print *dictionary* out)))) | |
(defun load-dictionary (&key (filename "dictionary.db")) | |
"Load the program *dictionary* in utf-8 format." | |
(with-open-file (in filename | |
:external-format :utf-8) | |
(with-standard-io-syntax | |
(setf *dictionary* (read in))))) | |
(defun check-if-dictionary-has (verb) | |
"Check for the presence of supplied verb." | |
(not (not (gethash verb *dictionary*)))) | |
(defun make-conjugation-list (&key (1s "-") (2s "-") (3s "-") (1p "-") (2p "-") (3p "-")) | |
"Create a list of verb conjugations with consistent defaults." | |
(list :1s 1s :2s 2s :3s 3s :1p 1p :2p 2p :3p 3p)) | |
(defun make-tense (verb tense conjugation-list) | |
"Returns a hash that associates a tense label with a conjugation list; adds list to *dictionary*." | |
(multiple-value-bind (queried-verb present) (gethash verb *dictionary*) | |
(if present | |
(setf (gethash tense queried-verb) conjugation-list) | |
(progn | |
(setf (gethash verb *dictionary*) (make-hash-table :test #'equal)) | |
(setf (gethash tense (gethash verb *dictionary*)) conjugation-list))))) | |
(defun get-conjugation-list (verb tense) | |
"Return a property list of verb conjugations according to verb and tense." | |
(gethash tense (gethash verb *dictionary*))) | |
(defun get-conjugation (verb tense conjugation) | |
"Return a conjugation string for a specific verb, tense, and conjugation." | |
(getf (get-conjugation-list verb tense) conjugation)) | |
(defun get-sorted-verb-list () | |
(let ((verbs nil)) | |
(loop for key being the hash-keys of *dictionary* | |
do (push key verbs)) | |
(sort verbs #'string-lessp))) | |
(defun list-verbs () | |
(let ((verblist (get-sorted-verb-list))) | |
(format t "~{~a~%~}" verblist))) | |
(defun print-conjugations (verb tense) | |
"Print a conjugation list for a given verb and tense." | |
(format t "~a: ~a~%" verb tense) | |
(let ((conjugation-list (get-conjugation-list verb tense))) | |
(format t "1s: ~a ~20t 1p: ~a~%" (getf conjugation-list :1s) (getf conjugation-list :1p)) | |
(format t "2s: ~a ~20t 2p: ~a~%" (getf conjugation-list :2s) (getf conjugation-list :2p)) | |
(format t "3s: ~a ~20t 3p: ~a~%" (getf conjugation-list :3s) (getf conjugation-list :3p)))) | |
(defun prompt-read (prompt) | |
"Return a read value after issuing supplied prompt." | |
(format *query-io* "~a: " prompt) | |
(force-output *query-io*) | |
(read-line *query-io*)) | |
(defun prompt-for-conjugation-list () | |
"Use the prompt-read function to read in a conjugation list." | |
(make-conjugation-list | |
:1s (prompt-read "1s") | |
:2s (prompt-read "2s") | |
:3s (prompt-read "3s") | |
:1p (prompt-read "1p") | |
:2p (prompt-read "2p") | |
:3p (prompt-read "3p"))) | |
(defun add-tense-to-dictionary () | |
"Read in a conjugation list, using prompt-for-conjugation-list, and add to *dictionary*." | |
(let ((verb (prompt-read "Verb")) | |
(tense (prompt-read "Tense")) | |
(conjugation-list (prompt-for-conjugation-list))) | |
(make-tense verb tense conjugation-list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment