0
votes

I have to implement an expert system in CLIPS that uses a grammar that can generate:

  • I saw a tutorial.
  • I went in a library.
  • In library a tutorial I saw.
    I receive an input from a user - let's suppose it it the first sentence - and I have to print a message like: YES G1 G4 G3 G8, if I can parse the input with my system or NO, otherwise.

In order to achieve this, I found a model system, implemented like:
(deffacts facts
(rule G1 S i B)
(rule G2 S in E)
(rule G3 B a C)
(rule G4 B saw B)
(rule G5 B went S)
(rule G6 B saw #)
(rule G7 B #)
(rule G8 C tutorial #)
(rule G9 D i B)
(rule G10 E a library B)
(rules S) )


Now, I don't know how to implement the rules and I'm looking for some help.

Thank you!

1
It's not clear from the description of your problem how you would derive the output G3 G3 G1 G2 from the input "In a library I saw a tutorial" using your model system. - Gary Riley
I hope now it's clear. I tried to be more specific - Biax

1 Answers

2
votes
         CLIPS (6.31 6/12/19)
CLIPS> 
(deftemplate sentence
   (multislot words)
   (multislot queue)
   (multislot rules)
   (slot symbol (default S)))
CLIPS>       
(deffacts productions
   (rule G1 S i B) 
   (rule G2 S in E) 
   (rule G3 B a C) 
   (rule G4 B saw B) 
   (rule G5 B went S) 
   (rule G6 B saw #) 
   (rule G7 B #) 
   (rule G8 C tutorial #) 
   (rule G9 D i B) 
   (rule G10 E a library B))
CLIPS>    
(deffacts test
   (sentence (words i saw a tutorial))
   (sentence (words i went in a library))
   (sentence (words in library a tutorial i saw)))
CLIPS>              
(defrule load-queue
    ?f <- (sentence (words ?w1 $?w2)
                    (queue)
                    (symbol S))
    =>
    (modify ?f (queue ?w1 $?w2)))
CLIPS>     
(defrule apply
   (rule ?r ?s $?m ?ns)
   (sentence (words $?w)
             (symbol ?s)
             (queue $?m $?e)
             (rules $?rules))
   =>
   (assert (sentence (words ?w)
                     (symbol ?ns)
                     (queue ?e)
                     (rules ?rules ?r))))
CLIPS>    
(defrule success 
   (declare (salience -10))
   (sentence (words $?w)
             (symbol #)
             (queue)
             (rules $?r))
   =>
   (printout t "YES " (implode$ ?w) " : " (implode$ ?r) crlf))
CLIPS> 
(defrule failure 
   (declare (salience -10))
   (sentence (words $?w) (queue $?w))
   (not (sentence (words $?w) (queue) (symbol #)))
   =>
   (printout t "NO  " (implode$ ?w) crlf))
CLIPS> (reset)
CLIPS> (run)
YES i saw a tutorial : G1 G4 G3 G8
YES i went in a library : G1 G5 G2 G10 G7
NO  in library a tutorial i saw
CLIPS>