From 1c69a398361736f0ca591ee390f90b2d660c401b Mon Sep 17 00:00:00 2001 From: William Moore Date: Tue, 7 Jan 2025 12:35:35 -0600 Subject: [PATCH] Updates to correct errors --- sent-struct.lsp | 3 ++- splork.lsp | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/sent-struct.lsp b/sent-struct.lsp index f35e535..99c1813 100644 --- a/sent-struct.lsp +++ b/sent-struct.lsp @@ -99,7 +99,7 @@ (if (<= 5 (random 10)) (setf sentence (format nil "~A ~A" sentence (find-pos words "ADJECTIVE" 'false))) ) - (setf sentence (format nil "~A ~A; ~A ~A ~A" sentence (find-noun-with-article words) (find-noun-with-article words) (find-pos words "PREPOSITION" 'false) (find-place-noun words))) + (setf sentence (format nil "~A ~A; ~A ~A" sentence (find-noun-with-article words) (find-noun-with-article words) (find-pos words "PREPOSITION" 'false) (find-place-noun words))) sentence)) (lambda () "You must meet at and retrieve the [random(, .5)] " @@ -118,3 +118,4 @@ ) (setf sentence (format nil "~A ~A, ~A ~A ~A ~A!" sentence (find-pos words "NOUN" 'false) (find-noun-with-article words) (find-pos words "VERB" 'false) (find-pos words "PREPOSITION" 'false) (find-place-noun words))) sentence)))) + diff --git a/splork.lsp b/splork.lsp index 13d4ab9..e218559 100644 --- a/splork.lsp +++ b/splork.lsp @@ -43,6 +43,17 @@ (nth 0 (nth (random (length new-words)) new-words)) )) +(defun replace-substring (string substring replacement) + "Replaces all occurrences of substring in string with replacement." + (let ((result "")) + (loop while (position substring string) + do (let ((pos (position substring string))) + (setf result (concatenate 'string result + (subseq string 0 pos) + replacement)) + (setf string (subseq string (+ pos (length substring)))))) + (concatenate 'string result string))) + (defun find-noun-with-article (words) "Generates any articles necessary for a noun" (let* ((new-words '()) @@ -52,11 +63,11 @@ (if (string-equal "PROPER" (nth 2 word)) (nth 0 word) - (if (string-equal "PLACE" (nth 2 word)) - (nth 0 word) - (cons "the " (nth 0 word)) + (if (string-equal "PLACE" (nth 2 word)) + (nth 0 word) + (format nil "the ~A" (nth 0 word)) + ) ) - ) )) (defun exec-lambda (x) @@ -66,7 +77,8 @@ (let* ((compiled-results '()) (generated-sentence "")) (setf compiled-results (mapcar #'exec-lambda *sentence-structures*)) - (setf generated-sentence (nth (random (length compiled-results)) compiled-results)) + (setf sent-index (random (length compiled-results))) + (setf generated-sentence (nth sent-index compiled-results)) (let* ( (interjection (find-pos words "INTERJECTION" 'false)) ) (if (<= 10 (random 10)) (if (string-equal "!" interjection) @@ -80,6 +92,8 @@ (if (not (string-equal "!" generated-sentence)) (setf generated-sentence (format nil "~A." generated-sentence)) ) + (setf generated-sentence (replace-substring generated-sentence " " " ")) + (setf generated-sentence (replace-substring generated-sentence "the the" "the")) generated-sentence)) (princ (splork))