Finding the count of repetitions of words (Clips) - clips

I have a task, write a program to count the number of repetitions of a word in the list.
I just started learning the clips so I do not know many things.
I wrote the code, but unfortunately it does not work, what could be the error?
(clear)
(deftemplate list_1
(slot numeral)
)
(deftemplate list_2
(slot numeral)
)
(deftemplate list_3
(slot numeral)
)
(deffacts start
(list_1 (numeral zero))
(list_1 (numeral one))
(list_1 (numeral two))
(list_2 (numeral zero))
(list_2 (numeral two))
(list_2 (numeral three))
(list_3 (numeral zero))
(list_3 (numeral one))
(list_3 (numeral three))
)
(defglobal
?*countword* = 0
)
(defrule inputword
(initial-fact)
=>
(printout t crlf “Enter a word to search for: “)
(bind ?i (read))
(assert (wordforsearch ?i))
)
(defrule searchword
(wordforsearch ?i)
(list_1 (numeral ?i))
(list_2 (numeral ?i))
(list_3 (numeral ?i))
=>
(bind ?*countword* (+ ?*countword* 1))
)
(defrule outputword
(wordforsearch ?i)
=>
(printout t "Number of repetitions for a word: " ?i " = " ?*countword* crlf)
(reset)
(halt)
)
(run)
I really hope the same way that you explain in detail what the error is and maybe tell me another version of the implementation of the code.
P. S. I implemented another version of the program - the search for the number of repetitions of words in the sentence. This code also does not work.
(clear)
(defglobal
?*countword* = 0
)
(defrule inputword
(initial-fact)
=>
(printout t crlf “Enter a sentence: “)
(bind ?s (read))
)
(defrule inputword
(?s)
=>
(printout t crlf “Enter a word to search for: “)
(bind ?i (read))
(assert (wordforsearch ?i))
)
(defrule searchword
(wordforsearch ?i)
(?s ?i)
=>
(bind ?*countword* (+ ?*countword* 1))
)
(defrule outputword
(wordforsearch ?i)
=>
(printout t "Number of repetitions for a word: " ?i " = " ?*countword* crlf)
(reset)
(halt)
)
(run)
I really hope for your help in understanding the clips.
P. P. S. Sorry for my english

I can offer here this option. This has many shortcomings, but as an example this will suitable.
(defglobal
?*i* = 0
?*count* = 0
?*string* = (create$)
?*wordsearch* = ""
?*wordsearch1* = (create$)
)
(defrule Searching
?fact <- (searching)
=>
(retract ?fact)
(bind ?*i* (+ ?*i* 1))
(if (<= ?*i* (length ?*string*))
then
(if (eq ?*wordsearch1* (subseq$ ?*string* ?*i* ?*i*))
then
(bind ?*count* (+ ?*count* 1))
(assert (searching))
else
(assert (searching))
)
else
(printout t "Word " ?*wordsearch* " repeats " ?*count* " times" crlf)
)
)
(defrule Start
=>
(printout t crlf "Enter the string: ")
(bind ?*string* (create$ (explode$ (lowcase (readline)))))
(printout t crlf "Enter a word to search for: ")
(bind ?*wordsearch* (lowcase (readline)))
(bind ?*wordsearch1* (create$ (explode$ ?*wordsearch*)))
(assert (searching))
)

Related

hey i have a problem in this clips expert system code its not printing the result

this is a Diet and Nutrition Expert System i have a problem that i cant print the result to the user after entering the input so can any one help me to make it run correctly?
(defrule read-gender
(initial-fact)
=>
(printout t crlf crlf "Welcome! Diet and Nutrition Expert System" crlf)
(printout t "******************************************" crlf)
(printout t "This output of this program is:" crlf)
(printout t "1. Your Body Mass Index (BMI) and body-status." crlf)
(printout t "2. Recommended daily calories needed based on your body-status." crlf)
(printout t "3. Daily protein needed based on your weight (kgs)." crlf)
(printout t "4. Daily celcium needed based on your age." crlf)
(printout t "5. Daily fiber needed based on your calories needed." crlf)
(printout t "6. Daily carbohydrate needed based on your weight (kgs)." crlf)
(printout t "******************************************" crlf crlf)
(printout t "What is your gender (Female/Male) *case-sensitive*:")
(assert (gender (read))))
(defrule read-age
(gender ?)
=>
(printout t "Please enter your age:")
(assert (age (read))))
(defrule read-height
(gender ?)
=>
(printout t "Please enter your height:")
(assert (height (read))))
(defrule read-weight
(gender ?)
=>
(printout t "Please enter your weight in(KGs):")
(assert (weight (read))))
(defrule read-activity-days
(gender ?)
=>
(printout t "How many day do you exercise for a week:")
(assert (activity-days (read))))
(defrule set-activity-rate-sedentary
(activity-days ?days)
(test (< ?days 2))
=>
(assert (activity-rate "Sedentary"))
(assert (activity-factor 1.2)))
(defrule set-activity-rate-moderate
(activity-days ?days)
(test (and (>= ?days 2) (< ?days 5)))
=>
(assert (activity-rate "Moderate"))
(assert (activity-factor 1.55)))
(defrule set-activity-rate-hard
(activity-days ?days)
(test (>= ?days 5))
=>
(assert (activity-rate "Hard"))
(assert (activity-factor 1.75)))
(defrule calculate-bmi
(weight ?weight)
(height ?height)
=>
(bind ?bmi (* ?weight (/ ?height ?height)))
(assert (bmi ?bmi)))
(defrule set-body-status-underweight
(bmi ?bmi)
(test (< ?bmi 18.5))
=>
(assert (body-status "Underweight")))
(defrule set-body-status-normal-weight
(bmi ?bmi)
(test (and (>= ?bmi 18.5) (< ?bmi 24.9)))
=>
(assert (body-status "Normal-weight")))
(defrule set-body-status-overweight
(bmi ?bmi)
(test (and (>= ?bmi 24.9) (< ?bmi 29.9)))
=>
(assert (body-status "Overweight")))
(defrule calculate-daily-calories-female
(gender "Female")
(activity-factor ?activity-factor)
(weight ?weight)
(age ?age)
(height ?height)
=>
(bind ?bmr (* 655.1 (+ (* 9.563 ?weight) (* 1.85 ?height) (* 4.676 ?age))))
(bind ?daily-calories (* ?bmr ?activity-factor))
(assert (daily-calories ?daily-calories)))
(defrule calculate-daily-calories-male
(gender "Male")
(activity-factor ?activity-factor)
(weight ?weight)
(age ?age)
(height ?height)
=>
(bind ?bmr (* 66.5 (+ (* 13.75 ?weight) (* 5.003 ?height) (* 6.755 ?age))))
(bind ?daily-calories (* ?bmr ?activity-factor))
(assert (daily-calories ?daily-calories)))
(defrule set-daily-calcium-baby
(age ?age)
(test (< ?age 4))
=>
(assert (daily-calcium "210-270mg")))
(defrule set-daily-calcium-child
(age ?age)
(test (and (>= ?age 4) (< ?age 9)))
=>
(assert (daily-calcium "350-450mg")))
(defrule set-daily-calcium-teen
(age ?age)
(test (and (>= ?age 9) (< ?age 19)))
=>
(assert (daily-calcium "800mg")))
(defrule set-daily-calcium-adult
(age ?age)
(test (and (>= ?age 19) (< ?age 51)))
=>
(assert (daily-calcium "1000mg")))
(defrule set-daily-calcium-old
(age ?age)
(test (>= ?age 51))
=>
(assert (daily-calcium "1200mg")))
(defrule calculate-daily-protein-sedentary
(weight ?weight)
(activity-rate "Sedentary")
=>
(bind ?daily-protein (* ?weight 0.8))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-protein-moderate
(weight ?weight)
(activity-rate "Moderate")
=>
(bind ?daily-protein (* ?weight 1))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-protein-hard
(weight ?weight)
(activity-rate "Hard")
=>
(bind ?daily-protein (* ?weight 1.2))
(assert (daily-protein ?daily-protein)))
(defrule calculate-daily-carbohydrates
(weight ?weight)
=>
(bind ?daily-carbohydrates (* ?weight 2.5))
(assert (daily-carbohydrates ?daily-carbohydrates)))
(defrule protein-advice
(protein-needed ?p)
=>
(printout t crlf crlf" ######## Result ######## " crlf)
(printout t " 1. You need " ?p "g of protein per day." crlf))
(defrule carbohydrate-advice
(carbohydrate-needed ?c)
=>
(printout t " 3. You need " ?c "g of carbohydrate per day." crlf))
(defrule fiber-advice
(fiber-needed ?f)
=>
(printout t " 2. You need " ?f "g of fiber per day." crlf))
(defrule calcium-advice
(calcium-needed ?c)
=>
(printout t " 4. You need " ?c "g of calcium per day." crlf))
(defrule calories-advice-underweight
(body-status underweight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t "
5. Your Body Mass Index (BMI) is " ?bm " (" ?b "), "crlf "
6. You
need " ?c " calories per day. "crlf "
7. For advice from the experts, You may need extra
daily 300 calories (" (+ 300 ?c)") to gain 0.25kg/week." crlf crlf))
(defrule calories-advice-normalweight
(body-status normal-weight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b ")," crlf " 6. You
need " ?c " calories per day to maintain your healthy weight." crlf crlf))
(defrule calories-advice-overweight
(body-status overweight)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b ")," crlf " 6. You need " ?c " calories per day. " crlf " 7. For advice from the experts, You may need to reduce your daily calories needed by 300 to " (- ?c 300)))
(defrule calories-advice-obesity
(body-status obesity)
(calories-needed ?c)
(bmi ?bm)
(body-status ?b)
(carbohydrate-needed ?ca)
(calcium-needed ?ce)
=>
(printout t " 5. Your Body Mass Index (BMI) is " ?bm " (" ?b "), "crlf " 6. You
need " ?c " calories per day. "crlf " 7. For advice from the experts, You may need to reduce
your daily calories needed by 500 to (" (- ?c 300)") to loss 0.5kg/week." crlf crlf))
I tried to rewrite the code in different ways but its still working correctly but without any result so can any one please help me to rewrite it correctly?
Don't use an initial-fact pattern in your rules. The initial-fact is
no longer supported in CLIPS 6.4. Just leave the conditions of the
rule empty and it will work in both version 6.3 and 6.4.
The protein-advice defrule expects a protein-needed fact but a
daily-protein fact is asserted by your other rules.
The carbohydrate-advice defrule expects a carbohydrate-needed fact but a daily-carbohydrates fact is asserted by your other rules.
The fiber-advice defrule expects a fiber-needed fact but no fiber
related facts are asserted by any of your rules.
The calcium-advice defrule expects a calcium-needed fact but a daily-calcium fact is asserted by your other rules.
Your other rules that print results have similar issues.

CLIPS does not recognize deftemplate name

I am trying to retract a deftemplate fact but when I do this CLIPS keeps saying I have to first declare the deffunction yet it is the appropriate deftemplate.What seems to be the problem?
I have attached the related code:
I get this error:
[EXPRNPSR3] Missing function declaration for Agriculture.
What seems to be the problem?
(deftemplate Agriculture
(slot weed
(type SYMBOL)
(allowed-symbols B G))
(slot crop
(type SYMBOL)
(allowed-symbols C S))
(slot organic-matter
(type INTEGER)
(allowed-values 1 2 3)))
(defrule Sencor-1
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t "Do not use Sencor!!"crlf))
(defrule Sencor-2
(and (Agriculture(weed B))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2|3)))
=>
(printout t " " crlf "Use 3/4 pt/ac of Sencor" crlf ))
(defrule Lasso-1
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 1)))
=>
(printout t crlf"Use 2 pt/ac of Lasso" crlf))
(defrule Lasso-2
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 2)))
=>
(printout t crlf "Use 1 pt/ac of Lasso" crlf))
(defrule Lasso-3
(and (Agriculture(weed B|G))
(Agriculture(crop C|S))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 0.5 pt/ac of Lasso" crlf))
(defrule Bicep-1
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 1)))
=>
(printout t crlf "Use 1.5 pt/ac of Bicep" crlf))
(defrule Bicep-2
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 2)))
=>
(printout t crlf"Use 2.5 pt/ac of Bicep" crlf))
(defrule Bicep-3
(and (Agriculture(weed B|G))
(Agriculture(crop C))
(Agriculture(organic-matter 3)))
=>
(printout t crlf "Use 3 pt/ac of Bicep" crlf))
(defrule input
(initial-fact)
=>
(printout t crlf "What is the crop? (C:corn,S:soybean)")
(bind ?a (read))
(assert(Agriculture(crop ?a))) ;gets input from user
(printout t crlf "What is the weed problem? (B:broadleaf, G:grass)")
(bind ?b (read))
(assert(Agriculture(weed ?b)))
(printout t crlf "What is the % of organic matter content? (1:<2%,2:2-4%,3:>4%)")
(bind ?c (read))
(assert(Agriculture(organic-matter ?c)))
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
(printout t ""crlf crlf "RECOMMENDATIONS:"crlf)
(retract ?d))
In the RHS of the input rule you state:
?d <- (Agriculture(crop ?a) (weed ?b) (organic-matter ?c))
This is interpreted as "Run function Agriculture and bind its results into ?d".
What you probably are trying to do is:
(bind ?d (assert (Agriculture (crop ?a) (weed ?b) (organic-matter ?c))))

Incrementing a global variable within a user answered defrule

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>

Some problems with using "deftemplate"

I'm trying to change the code of the expert system (The Engine Diagnosis Expert System) Add disordered patterns - . Clip does not produce errors, but the questions are not loaded. What am I doing wrong?
(deftemplate your_car "This is template for describing condition car"
(slot working-state (default undefined))
(slot rotation-state (default undefined))
(slot spark-state (default undefined))
(slot charge-state (default undefined))
(slot symptom (default undefined))
(slot repair(default undefined))
)
(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
)
;-----------------------------------------------------------------------------------
(deffunction yes-or-no-p (?question)
(bind ?response (ask-question ?question yes no у n))
(if (or (eq ?response yes) (eq ?response y))
then
TRUE
else
FALSE)
)
;-----------------------------------------------------------------------------------
(defrule determine-engine-state ""
;(your_car (working-state undefined))
;(your_car (repair undefined))
?f1 <- (your_car (working-state undefined)(repair undefined))
=>
(if (yes-or-no-p "Does the engine start (yes/no)? ")
then
(if (yes-or-no-p "Does the engine run normally (yes/no)? ")
then
(modify ?f1 (working-state "engine normal"))
else
(modify ?f1 (working-state "engine unsatisfactory")))
else
(modify ?f1 (working-state "engine does-not-start"))))
;...
;-----------------------------------------------------------------------------------
(defrule no-repairs ""
(declare (salience -10))
;(your_car (repair undefined))
?f1 <- (your_car (repair undefined))
=>
(modify ?f1 (repair "Take your car to a mechanic."))
)
(defrule print-repair ""
(declare (salience 10))
;(your_car (repair ?item))
?f1 <- (your_car (repair ?item))
=>
(printout t crlf crlf)
(printout t "Suggested Repair:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?item)
)
;-----------------------------------------------------------------------------------
(defrule system-banner ""
(declare (salience 10))
=>
(printout t crlf crlf)
(printout t "****************************************" crlf)
(printout t "* The Engine Diagnosis Expert System *" crlf)
(printout t "****************************************" crlf)
(printout t crlf crlf)
)
A deftemplate defines the structure of a fact, but it does not create them. Add a deffacts to your program after the deftemplate definition.
(deffacts start
(your_car))
When a (reset) command is issued, this will assert the facts contained in any deffacts constructs.
CLIPS> (clear)
CLIPS>
(deftemplate your_car "This is template for describing condition car"
(slot working-state (default undefined))
(slot rotation-state (default undefined))
(slot spark-state (default undefined))
(slot charge-state (default undefined))
(slot symptom (default undefined))
(slot repair(default undefined))
)
CLIPS>
(deffacts start
(your_car))
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>
(deffunction yes-or-no-p (?question)
(bind ?response (ask-question ?question yes no у n))
(if (or (eq ?response yes) (eq ?response y))
then
TRUE
else
FALSE)
)
CLIPS>
(defrule determine-engine-state ""
?f1 <- (your_car (working-state undefined)(repair undefined))
=>
(if (yes-or-no-p "Does the engine start (yes/no)? ")
then
(if (yes-or-no-p "Does the engine run normally (yes/no)? ")
then
(modify ?f1 (working-state "engine normal"))
else
(modify ?f1 (working-state "engine unsatisfactory")))
else
(modify ?f1 (working-state "engine does-not-start"))))
CLIPS>
(defrule no-repairs ""
(declare (salience -10))
?f1 <- (your_car (repair undefined))
=>
(modify ?f1 (repair "Take your car to a mechanic."))
)
CLIPS>
(defrule print-repair ""
(declare (salience 10))
?f1 <- (your_car (repair ?item))
=>
(printout t crlf crlf)
(printout t "Suggested Repair:")
(printout t crlf crlf)
(format t " %s%n%n%n" ?item)
)
CLIPS>
(defrule system-banner ""
(declare (salience 10))
=>
(printout t crlf crlf)
(printout t "****************************************" crlf)
(printout t "* The Engine Diagnosis Expert System *" crlf)
(printout t "****************************************" crlf)
(printout t crlf crlf)
)
CLIPS> (reset)
CLIPS> (run)
Suggested Repair:
undefined
****************************************
* The Engine Diagnosis Expert System *
****************************************
Does the engine start (yes/no)? yes
Does the engine run normally (yes/no)? yes
Suggested Repair:
undefined
Suggested Repair:
Take your car to a mechanic.
CLIPS>

Using less than on CLIPS program

I am trying to return a message if the user types in a value within certain range. Is this possible on CLIPS? In addition the system should only accept values in increments of 10.
If the user types in a number less or equal to 10 it should say "A"
If the user types in a number greater than 10 and less than 40 it should say "B"
- so it should only accept values 10,20,30,40
This is the code I have so far:
(defrule b-a1
(b-a "a")
=>
(bind ?reply (get-text-from-user "How many points did you achieve?"))
(assert (b-a1 ?reply )))
(defrule b-a2
(b-a1 <= 10)
=>
(assert (conclusion "A")))
(defrule b-a2
(10 < b-a1 < 40)
=>
(assert (conclusion "B")))
Any ideas on how I can get this working?
CLIPS>
(defrule b-a1
(b-a "a")
=>
(printout t "How many points did you achieve? ")
(bind ?reply (read))
(assert (b-a1 ?reply )))
CLIPS>
(defrule b-a2
(b-a1 ?v&:(<= ?v 10))
=>
(assert (conclusion "A")))
CLIPS>
(defrule b-a2
(b-a1 ?v&:(< 10 ?v)&:(< ?v 40))
=>
(assert (conclusion "B")))
CLIPS> (assert (b-a "a"))
<Fact-1>
CLIPS> (run)
How many points did you achieve? 24
CLIPS> (facts)
f-0 (initial-fact)
f-1 (b-a "a")
f-2 (b-a1 24)
f-3 (conclusion "B")
For a total of 4 facts.
CLIPS>

Resources