Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE (c) S. Tanimoto, 2002 AI Techniques

Similar presentations


Presentation on theme: "CSE (c) S. Tanimoto, 2002 AI Techniques"— Presentation transcript:

1 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques
Pattern Matching Many AI programs are “data driven”. The program exams the data and then decides how to process it. Rule-based programming: the program consists of a collection of rules and a means for applying them. CSE (c) S. Tanimoto, AI Techniques

2 Production System Form
A rule of the form if [condition] then [action] is sometimes called a production rule. Lisp’s COND form provides a means to implement rule testing and application. ; Example structure: (COND (condition1 action1) (condition2 action2) ... (conditionN actionN) ) CSE (c) S. Tanimoto, AI Techniques

3 A Lisp Production System for Translation into Roman Numerals
(defun to-roman () (let ((n nil))(loop (cond ((null n) (setq n (read))) ((equal n 0) (terpri) (setq n nil)) ((and (numberp n)(> n 0)(< n 4)) (princ #\I)(decf n)) ((and (numberp n)(= n 4)) (format t "IV")(decf n 4)) ((and (numberp n)(< n 9)) (princ #\V)(decf n 5)) ((and (numberp n)(> n 9)(< n 40)) (princ #\X)(decf n 10)) (t (format t "cannot handle ~S~%" n)(setq n nil)) ) ) ) ) CSE (c) S. Tanimoto, AI Techniques

4 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques
Focus on the Condition ((equal n 0)) (terpri) (setq n nil)) A test to see whether the data possesses some particular property. We generally use predicates -- functions that return boolean values. Many conditions can be expressed as a comparison between the input data and a “pattern”. A trivial case is: (equal n 0); n = data, 0 = pattern. CSE (c) S. Tanimoto, AI Techniques

5 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques
The MATCH Function It’s not built into Common Lisp but defined in the text. It supports... 1. exact matching of 1-level lists (match '(hello there) my-input) 2. wild-card construct, e.g., (match '(my name is (? X)) '(my name is john) ) 3. wild-card with restrictive predicate, e.g., (match '(age (numberp age) '(age 21))) 4. wild-sequence construct, e.g., (* LST) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran)) CSE (c) S. Tanimoto, AI Techniques

6 What MATCH Returns MATCH returns an association list.
(match '(hello there) '(hello)) NIL (match '(hello there) '(hello there)) ((YES . YES)) (match '(my name is (? X)) '(my name is john)) ((X . JOHN)(YES . YES)) CSE (c) S. Tanimoto, AI Techniques

7 What MATCH Returns (Cont.)
(match '(age (numberp age) '(age 21) )) ((AGE. 21)(YES . YES)) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran) ) ((X JIM AND JAN) (Y MANUEL AND TRAN) (YES . YES)) CSE (c) S. Tanimoto, AI Techniques

8 Accessing Associations Returned by MATCH
(match '(hello there) '(hello there)) ((YES . YES)) ;;; VAL is defined in MATCH.CL (val 'x '((x . 5)(y . 10)) 5 (val 'x (match '(hi (? x))'(hi there))) THERE CSE (c) S. Tanimoto, AI Techniques

9 Accessing Associations (Cont.)
(match '((* STUFF) is (? NAME)) '(my name is john)) ((STUFF MY NAME)(NAME . JOHN)(YES . YES)) (setq result *) (val 'stuff result) (MY NAME) (val ’name result) JOHN (val 'y result) NIL CSE (c) S. Tanimoto, AI Techniques

10 The MATCH Function (continued)
MATCH is defined in the file MATCH.CL You must load this file explicitly to use it. (load "MATCH.CL") or, if you have renamed the file to match.lsp, use (load "match.lsp") Make sure there is a copy of the file in your current directory. CSE (c) S. Tanimoto, AI Techniques

11 CSE 415 -- (c) S. Tanimoto, 2002 AI Techniques
Getting User Input (READ) returns the next form from standard input. (READ-LINE) returns a string containing the next line of text from standard input. To get a line of text as a list, we can use: (defun get-input-as-list (prompt-string) (format t "~A~%" prompt-string) (read-line) ; Swallow & ignore current line. (read-from-string ;Get a form from a string. (concatenate ;Combine some sequences, 'string ;result to be a string. "(" ;begin a list (read-line) ;words in the list ")" ;end of the list ) ) ) ) CSE (c) S. Tanimoto, AI Techniques

12 Using GET-INPUT-AS-LIST
(get-input-as-list "How are you?") How are you? I am fine (I AM FINE) 11 READ-FROM-STRING returns 2 values -- the form read and the number of characters used from the string. Therefore GET-INPUT-AS-LIST also returns 2 values. CSE (c) S. Tanimoto, AI Techniques

13 Using MATCH to Parse Input
(load "MATCH.CL") (defun get-favorite-color () (let* ((ans (get-input-as-list "What is your favorite color?") ) (result (or (match '((? c)) ans) (match '(its (? c)) ans) (match '((? c) is my (* x)) ans) (match '(my (* x) is (? c)) ans) ) ) ) (if result (val 'c result) 'unknown) ) ) CSE (c) S. Tanimoto, AI Techniques

14 Parsing Input with MATCH (Cont.)
>(get-favorite-color) What is your favorite color? Its red RED Green is my favorite color GREEN black BLACK My very favorite is chartreuse CHARTREUSE CSE (c) S. Tanimoto, AI Techniques

15 Reading Math Formulas as English
(load "MATCH.CL") (defun get-sum () (let* ((exp (get-input-as-list "Describe a particular sum") ) (result (or (match '((? a) plus (? b)) exp) (match '(the sum of (? a) and (? b)) exp) ) ) ) (if result (list '+ (val 'a result)(val 'b result)) ) ) ) CSE (c) S. Tanimoto, AI Techniques

16 Reading Math Formulas (Cont.)
>(get-sum) Describe a particular sum 5 plus 10 (+ 5 10) the sum of area and perimeter (+ AREA PERIMETER) CSE (c) S. Tanimoto, AI Techniques


Download ppt "CSE (c) S. Tanimoto, 2002 AI Techniques"

Similar presentations


Ads by Google