Hello there Stack overflow community, I'm turning to you for help. I'm trying to do something in CLIPS, sort of like a parser and I'm having a bit of trouble. To summarize, I'm trying to make a program that takes the user's input, which is a phrase such as "I read a book" and give an output based on some rules I have defined, let's say "I read" is identified by rule G1, and "a book" is identified by rule G2 ("A book" can be identified by rule G3 should we need to process input "A book I read").
The output for "I read a book" should be "YES G1 G2" because based on the user input and rules the program identified the rules used in order to identify what was written in the input provided by the user. I hope I explained that pretty well, it's basically my first question here. So far I have managed to create the rules and I accounted for situations where the string could be placed at the beginning of the input (see G3 rule mentioned above).
Maybe this can be solved using explode$ and wildcards like $? but I'm really not sure how to start.
This is my code so far, I know it's not very much:
(defrule G1
=>
(assert (regula G1 "I read"))
)
(defrule G2
=>
(assert (regula G2 "I read."))
)
(defrule G3
=>
(assert (regula G3 "a book."))
)
(defrule G4
=>
(assert (regula G4 "A book"))
)
I appreciate all help and all answers.
You problem statement doesn't exactly match the code you've generated so far ("a book" is not identified by rule G2). Also, you can use a deffacts statement in place of rules that unconditionally assert facts.
Here's one way to solve the problem:
CLIPS (6.31 6/12/19)
CLIPS>
(deffacts regulas
(regula G1 I read)
(regula G2 I read.)
(regula G3 a book.)
(regula G4 A book))
CLIPS>
(deftemplate translation
(multislot text)
(multislot tokens)
(multislot output)
(slot complete (default NO)))
CLIPS>
(defrule get-input
=>
(printout t "Input: ")
(bind ?text (readline))
(assert (translation (text ?text)
(tokens (explode$ ?text)))))
CLIPS>
(defrule parse
?t <- (translation (tokens $?tokens $?rest)
(output $?output))
(regula ?replacement $?tokens)
=>
(modify ?t (tokens $?rest)
(output $?output ?replacement)))
CLIPS>
(defrule success
?t <- (translation (tokens)
(complete NO))
=>
(modify ?t (complete YES)))
CLIPS>
(defrule print-results
(declare (salience -10))
?t <- (translation (complete ?complete)
(tokens $?tokens)
(output $?output))
=>
(printout t ?complete " " (implode$ (create$ ?output ?tokens)) crlf))
CLIPS> (reset)
CLIPS> (run)
Input: I read a book.
YES G1 G3
CLIPS> (reset)
CLIPS> (run)
Input: A book I read.
YES G4 G2
CLIPS> (reset)
CLIPS> (run)
Input: A book you read.
NO G4 you read.
CLIPS> (reset)
CLIPS> (run)
Input: You read a book.
NO You read a book.
CLIPS>