;;;; Rules to build Word N-Grams ;;; (deftemplate Category ; A category of text (e.g. scientific, legal, spam, etc) (slot NAME (type SYMBOL)) ; Name for Category of Text (slot M (type INTEGER)) ; Size of Training Set for Category ) (deftemplate WordPair ; structure for ccounting Word Pairs (2-Grams of words) (slot CATEGORY (type SYMBOL)) (slot WORD1 (type SYMBOL)) (slot WORD2 (type SYMBOL)) (slot COUNT (type INTEGER)) ; Number of instances of word pair ) ;;;; And here are some rules to open an close text files. ;;;; ;;;; Rule to open a file of text (defrule init (initial-fact) => (printout t "Name of file to read? ") (bind ?filename (read)) (printout t "Catagory of text? ") (bind ?category (read)) (bind ?flag (open ?filename data "r")) (printout t "(file " ?category ?flag ")" crlf) (assert (file ?category ?flag)) ) ;;; If file does not exist (defrule no-file ?f <- (file ?c FALSE) => (retract ?f) (printout t "File not found" crlf) ) ;;; Read a paragraph of text ;; (defrule ReadLineOfText ?f<-(file ?class TRUE) (not (line ?class EOF)) => (bind ?line (readline data)) (printout t ?line crlf) (assert (line ?class ?line)) (retract ?f) (assert (file ?class TRUE)) ) (defrule eof (declare (salience 10)) ?f <- (file ?class TRUE) ?eof <- (line ?class EOF) => (retract ?f ?eof) (close data) ) (defrule ConverLineToParagraph ?l <- (line ?class ?line) => (assert (Paragraph ?class (explode$ ?line))) (retract ?l) )