I need to write a method of a function that does the following:
Divides the text into words;
Prints words that are different from the first word;
And before that converts each word according to the following rule:
If the word is odd, then removes its middle letter.
The result is displayed on the screen and in a text file.
Here's a function that will give you the list of different words:
CLIPS>
(deffunction munge (?text)
(bind ?w1 (explode$ ?text))
(bind ?w2 (create$))
(progn$ (?w ?w1)
(bind ?len (str-length ?w))
(if (oddp ?len)
then
(bind ?nw (str-cat (sub-string 1 (div ?len 2) ?w)
(sub-string (+ (div ?len 2) 2) ?len ?w)))
(bind ?w2 (create$ ?w2 ?nw))
else
(bind ?w2 (create$ ?w2 (str-cat ?w)))))
(bind ?first (nth$ 1 ?w2))
(bind ?rest (rest$ ?w2))
(bind ?w3 (create$))
(progn$ (?w ?w2)
(if (neq ?w ?first)
then
(bind ?w3 (create$ ?w3 ?w))))
?w3)
CLIPS> (munge "red green blue purple brown green white red black blue")
("gren" "blue" "purple" "brwn" "gren" "whte" "blck" "blue")
CLIPS>
I suggest you to start with some basic documentation.
An example:
http://www2.cs.siu.edu/~rahimi/cs537/slides/big-2.pdf
You should look at multi-field built-in functions.
Related
I´m using the bind function but the text to be bound is very big.
I want to split the text in more lines so when I use the print out command, it will fit in the screen properly.
Any suggestions how to do it ?
Define a deffunction:
CLIPS>
(deffunction print-to-width (?log-name ?width ?str)
(if (<= ?width 0)
then
(printout ?log-name ?str crlf)
(return))
(bind ?w ?width)
(while (neq ?str "")
(bind ?pos (str-index " " ?str))
(if (or (not ?pos)
(> ?pos (+ ?w 1)))
then
(if (and (not ?pos) (<= (str-length ?str) ?w))
then
(printout ?log-name ?str)
(bind ?str "")
else
(if (!= ?w ?width)
then
(printout ?log-name crlf)
(bind ?w ?width)
else
(printout ?log-name (sub-string 1 ?w ?str))
(bind ?str (sub-string (+ ?w 1) (str-length ?str) ?str))
(if (neq ?str "") then (printout ?log-name crlf))
(bind ?w ?width)))
else
(printout ?log-name (sub-string 1 ?pos ?str))
(bind ?str (sub-string (+ ?pos 1) (str-length ?str) ?str))
(bind ?w (- ?w ?pos)))
(if (eq ?str "") then (printout ?log-name crlf)))
(return))
CLIPS> (print-to-width t 0 "the quick brown fox jumped over the lazy dogs")
the quick brown fox jumped over the lazy dogs
CLIPS> (print-to-width t 80 "the quick brown fox jumped over the lazy dogs")
the quick brown fox jumped over the lazy dogs
CLIPS> (print-to-width t 40 "the quick brown fox jumped over the lazy dogs")
the quick brown fox jumped over the lazy
dogs
CLIPS> (print-to-width t 20 "the quick brown fox jumped over the lazy dogs")
the quick brown fox
jumped over the lazy
dogs
CLIPS> (print-to-width t 10 "the quick brown fox jumped over the lazy dogs")
the quick
brown fox
jumped
over the
lazy dogs
CLIPS>
Or a message-handler
CLIPS>
(defmessage-handler STRING print-to-width (?log-name ?width)
(bind ?str ?self)
(if (<= ?width 0)
then
(printout ?log-name ?str crlf)
(return))
(bind ?w ?width)
(while (neq ?str "")
(bind ?pos (str-index " " ?str))
(if (or (not ?pos)
(> ?pos (+ ?w 1)))
then
(if (and (not ?pos) (<= (str-length ?str) ?w))
then
(printout ?log-name ?str)
(bind ?str "")
else
(if (!= ?w ?width)
then
(printout ?log-name crlf)
(bind ?w ?width)
else
(printout ?log-name (sub-string 1 ?w ?str))
(bind ?str (sub-string (+ ?w 1) (str-length ?str) ?str))
(if (neq ?str "") then (printout ?log-name crlf))
(bind ?w ?width)))
else
(printout ?log-name (sub-string 1 ?pos ?str))
(bind ?str (sub-string (+ ?pos 1) (str-length ?str) ?str))
(bind ?w (- ?w ?pos)))
(if (eq ?str "") then (printout ?log-name crlf)))
(return))
CLIPS>
(send "the quick brown fox jumped over the lazy dogs" print-to-width t 0)
the quick brown fox jumped over the lazy dogs
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 80)
the quick brown fox jumped over the lazy dogs
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 40)
the quick brown fox jumped over the lazy
dogs
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 20)
the quick brown fox
jumped over the lazy
dogs
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 10)
the quick
brown fox
jumped
over the
lazy dogs
CLIPS>
In my expert system user must check Developer and Price after he'll see notebook which is suitable for this parameters.
For example with this parameters (on screenshot) I must have result: Model: Noteebok1
But I don't see anything. Where is a problem or bug?
CLP File Code:
(defglobal ?*s* = 0)
(deftemplate Notebook
(slot pModel)
(slot pDeveloper)
(slot pPrice))
;*******************************************************************
(deffunction QuestionOf(?TextQuestion $?variations)
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer)))
(while (not (member ?Answer ?variations)) do
(printout t ?TextQuestion)
(bind ?Answer (read))
(if (lexemep ?Answer)
then (bind ?Answer (lowcase ?Answer))))
?Answer)
;********************************************************************'
(defrule banner
(declare (salience 10))
=>(load-facts D:\fact.txt)
(printout t crlf crlf)
(printout t "Expert system. Nout search")
(printout t crlf crlf))
;******************************************************************'
(defrule QuestionDeveloper
(not (Developer ?))
=>(bind ?asssert(QuestionOf "Check Developer (a-HP,b-Samsung,c-Apple,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Developer HP)))
(if (eq ?asssert b)then (assert (Developer Samsung)))
(if (eq ?asssert c)then (assert (Developer Apple)))
(if (eq ?asssert d)then (assert (Developer IDontKnow))))
;******************************************************************'
(defrule QuestionPrice
(not (Price ?))
=>(bind ?asssert(QuestionOf "Price?(a-300,b-400,c-500,d-IDontKnow)" a b c d))
(if (eq ?asssert a)then (assert (Price 300)))
(if (eq ?asssert b)then (assert (Price 400)))
(if (eq ?asssert c)then (assert (Price 500)))
(if (eq ?asssert d)then (assert (Price IDontKnow))))
;******************************************************************'
(defrule Vyvod
(or (Developer ?xDeveloper)(Developer IDontKnow))
(or (Price ?xPrice)(Price IDontKnow))
(Notebook(pModel ?Model)(pDeveloper ?xDeveloper)(pPrice ?xPrice))
=>(bind ?*s*(+ ?*s* 1))
(printout t crlf " " ?*s* ". Model : " ?Model crlf))
;******************************************************************'
Fact.txt:
(Notebook(pModel Notebook1)(pDeveloper HP)(pPrice 500))
(Notebook(pModel Notebook2)(pDeveloper Samsung)(pPrice 400))
(Notebook(pModel Notebook3)(pDeveloper Apple)(pPrice 500))
The facts from the file fact.txt are not being loaded. Check that the file is at the specified path and that you have read access.
I'm trying to increment a defglobal variable (symcount) by 1 if the user defines that they have pain by using the (read) function
(defrule QPain
(initial-fact)
=>
(printout t "Are You In Pain? " crlf)
(bind (ans Answer) (read))
)
(defrule AnsInc
(Answ Answer = "y")
=>
(bind ?*symcount* (+ ?*symcount* 1)))
the increment must only happen of the user presses "y"
otherwise the increment must not happen.
CLIPS> (defglobal ?*symcount* = 0)
CLIPS>
(defrule QPain
=>
(printout t "Are You In Pain? ")
(bind ?answer (read))
(if (eq ?answer y)
then
(bind ?*symcount* (+ ?*symcount* 1))))
CLIPS> (reset)
CLIPS> (run)
Are You In Pain? y
CLIPS> ?*symcount*
1
CLIPS> (reset)
CLIPS> (run)
Are You In Pain? n
CLIPS> ?*symcount*
0
CLIPS>
Using the Clips programming language, what is the correct "not equals" syntax?
This is the not symbol ~
Clips Documentation
The ~ constraint is part of the pattern matching language. The neq function is for use within expressions. Both can be used with values of any type. The != and <> functions can only be used with numeric arguments.
CLIPS> (clear)
CLIPS>
(defrule rule-1
(color ?color&~red&~blue)
=>
(printout t "rule-1: " ?color crlf))
CLIPS>
(defrule rule-2
(color ?color)
(test (and (neq ?color red) (neq ?color blue)))
=>
(printout t "rule-2: " ?color crlf))
CLIPS> (assert (color green) (color blue) (color yellow) (color red))
<Fact-4>
CLIPS> (run)
rule-1: yellow
rule-2: yellow
rule-1: green
rule-2: green
CLIPS> (neq 2 3)
TRUE
CLIPS> (neq a b)
TRUE
CLIPS> (!= 2 3)
TRUE
CLIPS> (!= a b)
[ARGACCES5] Function != expected argument #1 to be of type integer or float
CLIPS>
Morning, Excuse the silly question but I am busy building a expert system much like the "21 Questions" game that uses questions asked to the user in order to determine the right dog for them. The expert system is coded in CLIPS / .CPS language and one of the requirements I am looking to include is that when the user is asked a yes/no question they are required to input "y" or "n".
In all the resources we have been taught we have only been tough number validation and not a specific character validation and I cannot find any resources that do this either.
This is an example of the number validation I did in order to ensure they input a valid number on one of my questions
(defrule test-integer
(number-in ?number&:(integerp ?number))
=>
(printout t ?number "is valid"
(defrule test-non-int
?number-address <- (number-in ?number&:(not (integerp ?number)))
=>
(printout t ?number " not valid int" crlf)
(retract ?number-address))
This is how you'd do it using rules:
CLIPS>
(defrule test-response
(response-in ?response&y|n)
=>
(printout t ?response " is valid" crlf))
CLIPS>
(defrule test-non-response
?response-address <- (response-in ?response&~y&~n)
=>
(printout t ?response " not valid response" crlf)
(retract ?response-address))
CLIPS> (assert (response-in xyz))
<Fact-1>
CLIPS> (run)
xyz not valid response
CLIPS> (assert (response-in n))
<Fact-2>
CLIPS> (run)
n is valid
CLIPS>
I'd suggest using a function that only accepts correct responses:
CLIPS>
(deffunction ask-question (?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer)))
(while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer))))
?answer)
CLIPS> (ask-question "Continue? " y n yes no)
Continue? k
Continue? l
Continue? ye
Continue? YeS
yes
CLIPS>
What i figured out was to link the answer from the one defrule to that of another defrule first to check if the answer was valid and then again if that answer was valid to link it to the correct defrule then that will proceed with the next question.
Code is from my own Expert System:
(defrule Small-CoatType-Full
(Small-Coat f)
(person (name ?name))
=>
(open "result.txt" result "a")
(printout result ?name " Likes Smaller, Fury Dogs" crlf)
(close result)
(printout t "Would you like a low energetic(l) or high energetic(h) breed?" crlf)
(assert (Small-Energy-Level(lowcase(read)))))
(defrule Small-Energy-Level-Wrong
(Small-Energy-Level ?var &~l&~h)
=>
(printout t crlf "Plesae Only Choose (l) or (h)")
(assert (Small-Energy-Level (lowcase(read)))))`