CSE 341 -- S. Tanimoto Pattern Matching 1 Pattern Matching Many Lisp programs are “data driven”. The program exams the data and then decides how to process.

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

CSCI3170 Introduction to Database Systems
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
1 Programming Languages and Paradigms Lisp Programming.
Lesson 4: Formatting Input Data for Arithmetic
WELCOME HI GOOD MORNING GOOD MORNING GOOD MORNING GOOD MORNING.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
CSE (c) S. Tanimoto, 2004 Natural Language Understanding 1 Natural Language Understanding Outline: Motivation The SHRINK program, revisited Communicating.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CSE (c) S. Tanimoto, 2007 Production Sys. & Pattern Matching 1 CSE 415 Production Systems Many AI programs are “data driven.” The program examines.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
JavaScript, Third Edition
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
SE2 Math FIT Project Sign in and take a name tag.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
CSE (c) S. Tanimoto, 2008 Production Systems 1 Production Systems Many AI programs are “data driven.” The program examines the data, and then it.
Common lisp A functional programming language. Useful URL:
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
OVERVIEW OF CLIENT-SIDE SCRIPTING
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
WHAT IS A CHATTERBOT? A chatterbot is a computer program that simulates a conversation between two people. That is, one person writes something and the.
EZGİ GENÇ History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
John Tran Business Program Manager, The Suddath Companies
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Using the Console.
Defining Macros in Lisp
Data Structures in Lisp
CSE (c) S. Tanimoto, 2002 AI Techniques
Natural Language Understanding
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Env. Model Implementation
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
The Metacircular Evaluator
Modern Programming Languages Lecture 21 Fakhar Lodhi
Natural Language Understanding
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
CSE (c) S. Tanimoto, 2004 AI Techniques
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
Data Structures in Lisp
Data Structures in Lisp
Bindings, Scope, and Extent
In a phone call the words between the “Hello” and the
Programming Languages
Lisp.
Hello Hi Hello Hi Hello Hi Hello Hi Hello Hi Hello Hi.
Presentation transcript:

CSE S. Tanimoto Pattern Matching 1 Pattern Matching Many Lisp 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 4 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 S. Tanimoto Pattern Matching 5 The MATCH Function It’s not built into Common Lisp but defined in the text. It supports 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 9 Accessing Associations (Cont.) (match '((* STUFF) is (? NAME)) '(my name is john)) ((STUFF MY NAME)(NAME. JOHN)(YES. YES)) (setq result *) ((STUFF MY NAME)(NAME. JOHN)(YES. YES)) (val 'stuff result) (MY NAME) (val ’name result) JOHN (val 'y result) NIL

CSE S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 11 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 14 Parsing Input with MATCH (Cont.) >(get-favorite-color) What is your favorite color? Its red RED >(get-favorite-color) What is your favorite color? Green is my favorite color GREEN >(get-favorite-color) What is your favorite color? black BLACK >(get-favorite-color) What is your favorite color? My very favorite is chartreuse CHARTREUSE

CSE S. Tanimoto Pattern Matching 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 S. Tanimoto Pattern Matching 16 Reading Math Formulas (Cont.) >(get-sum) Describe a particular sum 5 plus 10 (+ 5 10) >(get-sum) Describe a particular sum the sum of area and perimeter (+ AREA PERIMETER)

CSE S. Tanimoto Pattern Matching 17 The SHRINK: A Lisp Program for Pattern-directed Conversation Holds a conversation with the user. Illustrates how shallow a program’s knowledge can be and still present an image of understanding. Demonstrates key ideas from J. Weizenbaum’s ELIZA program, published in 1966.

CSE S. Tanimoto Pattern Matching 18 Mechanisms in SHRINK Processing is oriented towards maintaining an illusion of understanding. Pattern matching used to “parse” user input. The YOU-ME-MAP function transposes first and second person references. Keyword-based rules respond to certain topics such as dreams, love, being positive or negative. Questions to the SHRINK are transformed into questions to the user. When all other rules fail, PUNT responses are used.

CSE S. Tanimoto Pattern Matching 19 Uses Pattern Matching Sample rules ((match '(you feel (* x)) s) (format t "I SOMETIMES FEEL THE SAME WAY.~%") ) ((match '(because (* x)) s) (format t "IS THAT REALLY THE REASON.~%") ) ((setq b (match '(you have (* x)) s)) (print-q (append '(how long have you had) (val 'x b) )) )

CSE S. Tanimoto Pattern Matching 20 A Sample Dialog with SHRINK (shrink) WELCOME TO MY SOFA! PLEASE ENCLOSE YOUR INPUT IN PARENTHESES. (hi there) TELL ME MORE (i have a little problem) HOW LONG HAVE YOU HAD A LITTLE PROBLEM? (ever since i told my girlfriend that i love computers more than her) ALL IS FAIR IN LOVE AND WAR (thanks maybe i should have told her that) BE MORE DECISIVE (ok) I SEE (bye) GOODBYE