Page 1 of 1

User Input Parsing Help?

Posted: Fri Mar 15, 2013 5:44 pm
by kkuro
I feel awful for asking about what I assumed to be such a simple task, but I cannot for the life of me get a simple system in place that takes user input and parses it for specific words, returning the word(s) it finds if it does. I've tried many, many things and have searched the internet as much as I can, but all my efforts end up to be fruitless.

My implementation is CLisp, by the way. I've only just started programming not long ago and I've only written so many little toy programs in my life.

Here's some code I have tried, it's incredibly messy and sort of just butchered from what it originally was as I tried to duct tape more mthods on to it to make it work.

Code: Select all

(defun splitspace (str) ;I didn't write this, I found it somewhere. Sorry I couldn't give credit.
	(loop for i = 0 then (1+ j)
		as j = (position #\Space str :start i)
		collect (subseq str i j)
		while j))

(defun ffind (var)
	(eval `(find ,var ',(splitspace (read)))))

(defun inp (what) ;(find) isn't working
	(let ((in-list (splitspace (read))))
		(find what in-list)))

(defun imp (str rstr) ;nothing's working
	(eval `(map 'list #'string-equal ,str ',(splitspace rstr))))
I'm quite frankly exhausted and reached the end or my rope, I'm completely stuck. I'm just trying to write a chatbot, ultimately what I'd like for the input is a system that allows the user to simply type in a reply without putting quotes around it, then have it parse the input for specific words, and return those words as strings if it find them, maybe in a list. I've been trying to figure this out for days, I quickly gave up on the quote thing.
Also, I don't know how common this is, but I'm using a small custom REPL and (read-line) simply returns a blank string (like "") instead of prompting for input like it does when invoked in CLISP's REPL. So that hasn't been an option for me. And of course (read) freaks out when given more than one argument so I have to put quotes around the input.

I'm sorry if this is somewhat stupid or simple, as I've said, I've been trying this and searching for days. All I wanted was a simple chatbot! Any help at all will be nice.

Re: User Input Parsing Help?

Posted: Sat Mar 16, 2013 3:31 am
by sylwester
First of all ffind is uneccesary. inp would be sufficient. I see two problems here:

1. find uses a test to see if the needle is the same as one element in the haystack.
In order for find to work with strings you need to do this:
(find "is" '("this" "is" "a" "test") :test #'equal)

2. read can return other things than a string. You probably want (read-line) together with your splitspace function

Code: Select all

(defun ffind (what) ;(find) isn't working
   (let ((in-list (splitspace (read-line))))
      (find what in-list :test #'equal)))

;; tests
(with-input-from-string 
   (*STANDARD-INPUT* "This is a test")
   (ffind "test")) ; ==> "test" 

(with-input-from-string 
   (*STANDARD-INPUT* "This is a control")
   (ffind "test")) ; ==> NIL 

Re: User Input Parsing Help?

Posted: Sat Mar 16, 2013 1:19 pm
by kkuro
Ah, so that's how find works. Well I feel dumb now. Thank you sly, that works rather nicely. I actually fixed ffind last night with a unsettling cluster of ifs and cars and cdrs, but using (find) now the proper way is a lot more elegent. Again, thank you, I'm slowly sharpening my skills.