Pattern matching and unification - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Pattern matching and unification

Description:

(cons (cons var val) bindings)) A (first) correct version of pat-match with binding management ... (let ((binding (get-binding var bindings))) (cond ((not binding) ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 24
Provided by: yae9
Category:

less

Transcript and Presenter's Notes

Title: Pattern matching and unification


1
Pattern matching and unification??????? -?????
  • ??? ???
  • ???? ????????
  • ????? ??????

2
  • Men Are all alike.
  • IN WHAT WAY.
  • They're always bugging us about something or
    another.
  • CAN YOU BE MORE SPECIFIC.
  • Well, my boyfriend made me come here.
  • YOUR BOYFRIEND MADE YOU COME HERE.
  • He says I'm depressed much of the time.
  • I AM SORRY TO HEAR YOU ARE DEPRESSED.
  • It's true. I'm unhappy.
  • DO YOU THINK COMING HERE WILL HELP YOU NOT TO
    BE UNHAPPY.
  • I need some help, that much is certain.
  • WHAT WOULD IT MEAN TO YOU IF YOU GOT SOME
    HELP.
  • Perhaps I could learn to get along with my
    mother.
  • TELL ME MORE ABOUT YOUR FAMILY.

3
?? ?????
  • ????? ?? ??? Joseph Weizenbaum 1966
  • ????? ?? ???? ?? ???????? ???'?????.
  • ????'?????? ?????? ????? ?-nondirective, ?????
    ?????-???? ??????? ?????? ?????? ?????? ??
    ????.
  • ????? ?? ????????? ???? ????? pattern-matching
    (????? ??????) ??? ?????? ??????? ???? ????.
  • http//www.cit.gu.edu.au/7Eterryd/subjects/intro.
    to.ai/lecture2slide3.html

4
?? ??? ?? ?????
  • Specification
  • 1. Read input.
  • 2. Find a pattern matching input.
  • 3. Transform input into a response.
  • 4. Print response.
  • Back to 1

5
Pattern matching
  • Pattern
  • (i need a X)
  • Response
  • (what would it mean to you if you got a X ?)
  • Match symbols with same symbols and variables
    with any symbol.
  • Generalization of equal
  • (defun pat-match (pattern input)
  • "Does pattern match input? Any variable can
    match anything."
  • (if (variable-p pattern)
  • t
  • (if (or (atom pattern) (atom input))
  • (eql pattern input)
  • (and (pat-match (first pattern) (first
    input))
  • (pat-match (rest pattern) (rest
    input))))))

6
Representing variables
  • use naming convention on symbols.
  • Any symbol with name starting with '?'.
  • (defun variable-p (x)
  • "Is x a variable (a symbol beginning with
    '?')?"
  • (and
  • (symbolp x)
  • (equal
  • (char (symbol-name x) 0) \?)))

7
Extracting information from the match
  • Want to get out the bindings of variables when
    successful.
  • How to represent bindings? difficult issue to
    optimize.
  • Take advantage of the SUBLIS primitive.
  • (sublis
  • '((?X . vacation))
  • '(what would it mean to you if you got a ?X
    ?))
  • (WHAT WOULD IT MEAN TO YOU IF YOU GOT A VACATION
    ?)

8
Pattern matching with variables
  • (defun pat-match (pattern input)
  • "Does pattern match input? BUGGY VERSION."
  • (if (variable-p pattern)
  • (list (cons pattern input))
  • (if (or (atom pattern) (atom input))
  • (eql pattern input)
  • (append
  • (pat-match (first pattern) (first input))
  • (pat-match (rest pattern) (rest input))))))
  • 4 bugs
  • a. (eql pattern input) can return T and append
    will die.
  • b. (eql pattern input) can return NIL meaning
    failure, and be
  • interpreted as an empty binding
    (semi-predicate problem).
  • c. We want the bindings of variables to agree
    (?X ?X) (1 2) fails.
  • d. Inefficient to check first and rest when we
    know that first fails.

9
Bindings Management
  • Need to be more careful about bindings
  • a. pass them as parameter to pat-match (pat-match
    pat input bindings)
  • b. distinguish between failure and no-bindings.
  • New abstraction
  • (defconstant fail nil)
  • (defconstant no-bindings '((t . t)))
  • (defun get-binding (var bindings)
  • "Find a (var . value) pair in a binding list."
  • (assoc var bindings))
  • (defun binding-val (binding)
  • "Get the value part of a binding."
  • (cdr binding))
  • (defun lookup (var bindings)
  • "Get the value part (for var) from a binding
    list."
  • (binding-val (get-binding var bindings)))
  • (defun extend-bindings (var val bindings)
  • "Add a (var . value) pair to a binding list."

10
A (first) correct version of pat-match with
binding management
  • (defun pat-match (pattern input optional
    (bindings no-bindings))
  • "Match pattern against input in the context of
    the bindings."
  • (cond ((eq bindings fail) fail)
  • ((variable-p pattern)
  • (match-variable pattern input bindings))
  • ((eql pattern input) bindings)
  • ((and (consp pattern) (consp input))
  • (pat-match (rest pattern) (rest input)
  • (pat-match (first pattern)
    (first input) bindings)))
  • (t fail)))

11
  • (defun match-variable (var input bindings)
  • "Does VAR match input? Uses (or updates) and
    returns bindings."
  • (let ((binding (get-binding var bindings)))
  • (cond ((not binding)
  • (extend-bindings var input bindings))
  • ((equal input (binding-val
    binding))
  • bindings)
  • (t fail))))
  • gt (pat-match '(i need a ?X) '(i need a vacation))
  • ((?X . vacation) (t . t))

12
Make extend-bindings smarter
  • (defun extend-bindings (var val bindings)
  • "Add a (var . val) pair to a binding list."
  • (cons (var val)
  • (if (eq bindings no-bindings) nil
    bindings)))
  • gt (pat-match '(1 2 3) '(1 2 3))
  • ((t . t))
  • gt (pat-match '(?X is ?X) '((2 2) is 4))
  • nil
  • gt (pat-match '(?X is ?X) '((2 2) is (2 2)))
  • ((?X 2 2))

13
Segment pattern matching
  • - Similar to kleene-star notation in regular
    expressions want to match
  • any number of elements in a list.
  • - Choose a syntax to write segment variables (?
    ?var)
  • (defun segment-pattern-p (pattern)
  • (and (consp pattern) (starts-with (first
    pattern) '?)))
  • gt (pat-match '((? ?x) is (? ?p) ?) '(1 2 3
    is 5 2 ?))
  • ((?x 1 2 3) (?p 5 2))
  • (defun pat-match (pattern input optional
    (bindings no-bindings))
  • "Match pattern against input in the context of
    the bindings."
  • (cond ((eq bindings fail) fail)
  • ((variable-p pattern)
  • (match-variable pattern input bindings))
  • ((eql pattern input) bindings)
  • ((segment-pattern-p pattern)
  • (segment-pattern-match pattern input
    bindings))
  • ((and (consp pattern) (consp input))

14
Segment Pattern Matching and backtracking
  • How far should segment pattern match?
  • Assume segment pattern is followed by a non
    variable C
  • (pattern starts with ((? ?p) C p2...))
  • Look for first occurrence of C in input index
    pos. (... C i2 ...)
  • If not found, fail.
  • Else match up to pos and check that (rest input)
    matches (rest pattern)
  • b2 (pat-match (p2...) (r2 ...))
  • If b2 succeeds -gt succeed and extend with binding
    ?p.
  • Else? Maybe need to bind ?p to a longer segment.
  • gt (pat-match '((? ?p) is a (? ?x)) '(what he is
    is a fool))
  • ((?p what he is) (?x fool))

15
  • (defun segment-match (pattern input bindings
    optional (start 0))
  • "Match segment pattern ((? var) . pat) against
    input."
  • (let ((var (second (first pattern)))
  • (pat (rest pattern)))
  • (if (null pat)
  • (match-variable var input bindings)
  • assume pat starts with a constant
  • (let ((pos (position (first pat) input
    start start test 'equal)))
  • (if (null pos)fail
  • (let ((b2 (pat-match pat (subseq input
    pos) bindings)))
  • If this one failed, try a longer
    segment
  • Else check that the variables
    match
  • (if (eq b2 fail)
  • (segment-match pattern input
    bindings
  • ( pos 1))
  • (match-variable var (subseq input 0
    pos) b2))))))))

16
A bug in segment-match!
  • gt (pat-match
  • '((? ?x) a b (? ?x))
  • '(1 2 a b a b 1 2 a b)) --gt NIL!
  • (defun segment-match (pattern input bindings
    optional (start 0))
  • "Match segment pattern ((? var) . pat) against
    input."
  • (let ((var (second (first pattern)))
  • (pat (rest pattern)))
  • (if (null pat)
  • (match-variable var input bindings)
  • assume pat starts with a constant
  • (let ((pos (position (first pat) input
    start start test 'equal)))
  • (if (null pos)
  • fail
  • (let ((b2 (pat-match pat (subseq input
    pos)
  • (match-variable
    var (subseq input 0 pos)

  • bindings))))
  • If this one failed, try a longer
    segment
  • (if (eq b2 fail)

17
ELIZA A Rule-based translator
  • (defun rule-pattern (rule) (first rule))
  • (defun rule-responses (rule) (rest rule))
  • Rule example
  • ((? ?x) I want (? ?y))
  • (What would it mean if you got ?y)
  • (Why do you want ?y)
  • (Suppose you got ?y soon))
  • If pattern matches, select one of the answers
    randomly.

18
How to handle a set of rules?
  • Several rules match, what to do?
  • - Option 1 accept the first matching rule.
  • - Option 2 choose one rule randomly among the
    matching rules.
  • - Option 3 combine the responses from several
    matching rules.
  • Use option 1.

19
  • (defparameter eliza-rules
  • '((((? ?x) hello (? ?y))
  • (How do you do. Please state you problem.))
  • (((? ?x) I want (? ?y))
  • (What would it mean if you got ?y)
  • (Why do you want ?y)
  • (Suppose you got ?y soon))
  • (((? ?x) if (? ?y))
  • (Do you really think it is likely that ?y)
  • (Do you wish that ?y)
  • (What do you think about ?y)
  • (Really -- if ?y))))

20
A rule-based interpreter
  • (defun eliza ()
  • "Respond to user input using pattern matching
    rules."
  • (loop
  • (print 'elizagt)
  • (write (flatten (use-eliza-rules (read)))
    pretty t)))
  • (defun use-eliza-rules (input)
  • "Find some rul with which to transform the
    input."
  • (some '(lambda (rule)
  • (let ((result (pat-match
    (rule-pattern rule) input)))
  • (if (not (eq result fail))
  • (sublis (switch-viewpoint result)
  • (random-elt
    (rule-responses rule))))))
  • eliza-rules))

21
  • (defun switch-viewpoint (words)
  • "Change I to you and vice-versa, and so on."
  • (sublis '((I . you) (you . I) (me . you) (am .
    are)) words))
  • aux functions
  • (defun flatten (the-list)
  • "Append together elements or lists in the-list"
  • (mappend 'mklist the-list))
  • (defun mklist (x)
  • "Return x if it is a list, otherwise (x)"
  • (if (listp x) x (list x)))
  • (defun random-elt (choices)
  • (elt choices (random (length choices))))

22
(No Transcript)
23
  • What Eliza Can't Do
  • In a word anything. 
  • ELIZA defines the major areas of AI in terms of
    what she can't do and doesn't know.
  • She doesn't know anything Knowledge
    Representation (KR) She can't reason  Reasoning
    She can't plan Planning She can't learn
    Machine Learning She doesn't understand natural
    language Natural Language Processing (NLP)
Write a Comment
User Comments (0)
About PowerShow.com