David
David

Reputation: 11

How to search for a string in CLIPS using functions

I am super new to CLIPS and I am writing an interactive wine detection system. The system works with inputs like yes and no. But I want to take it one more level and make it accept values like. "I am not sure".

Basically, I want to convert the following pseudocode to CLIPS.

def ask_for_input():

    user_inp = input("gimme dat")

    if "yes" in user_inp:
        return "yes"
    elif "no" in user_inp:
        return "no"
    elif "know" in user_inp:
        return "idk"
    else :
        return "invalid"
        # ask for input again now

Upvotes: 1

Views: 545

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

Use str-index to search for one string within another. Since "know" contains the substring "no", you'll want to search for "know" first:

CLIPS> 
(deffunction ask_for_input ()
   (bind ?user_inp (readline))
   (if (str-index "yes" ?user_inp)
      then (return "yes")
      else 
      (if (str-index "know" ?user_inp)
         then (return "idk")
         else
         (if (str-index "no" ?user_inp)
            then (return "no")
            else (return "invalid")))))
CLIPS> (ask_for_input)
yes
"yes"
CLIPS> (ask_for_input)
yah
"invalid"
CLIPS> (ask_for_input)
I don't know
"idk"
CLIPS> (ask_for_input)
not really
"no"
CLIPS> 

Upvotes: 1

Related Questions