Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Case Study 2: Eliza Program

(A software with chatting ability)

Dr. Umair Abdullah

1
Case Study 2: Eliza Program
• Qualities of Humans
– Communication Skill
• Natural Language Processing
– Pattern Matching
• Images
• Audio/sound patterns
• Symbol/text matching

2
Turing Test: Criteria of AI

S/W

Interrogator

3
Eliza: Introduction
• Eliza was one of the first programs to feature English
output as well as input. The program was named after
the heroine of a Pygmalion, who was taught to speak
proper English by a dedicated teacher.
• Eliza’s principal developer, MIT professor Joseph
Weizenbaum, published a paper on Eliza in the
January 1966, issue of the communications of the
association for computing machinery.
4
Eliza Logic
USER> Hello my name is Ali Ahmed Eliza Rules
ELIZA> HOW ARE YOU Ali Ahmed 1."?*x my name is ?*y":
USER> My name is Kashif Jamil [
"How are you ?y?",
ELIZA> HOW ARE YOU Kashif Jamil
"How can I help you ?y?"
USER>Hi my name is Anwer Ali ]
ELIZA> HOW CAN I HELP YOU ANWER ALI
2. "?*x I need ?*y":
Again user says any one of the following [
USER> I am thirsty, I need a glass of water "What would it mean if you got ?y",
ELIZA> SUPPOSE YOU GOT A GLASS OF WATER "Why do you need ?y",
"Suppose you got ?y"
USER> Actually I need help in AI ]
ELIZA> WHY DO YOU NEED HELP IN AI
5
Eliza Code
1. Eliza Code
2. Pattern Matching Module
(will also be used in next case study STUDENT)

6
Pattern Matching Module
Input:
‘?a my name is ?n’  where ‘?a’ and ‘?n’ are logical variables
‘Hello my name is Ali’  it can bound with exactly one value
 segment variable is like ‘?*a’ which can
bound with zero or more values

Output:
{'a': ['hello'], 'n': ['ali']}  Binding List = bindings

7
Four Functions
• is_variable
• match_variable
• contains_tokens
• match_pattern

8
is_variable
letters = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ"

def is_variable(pattern): >>> is_variable("?a")


True
return (type(pattern) is str >>> is_variable('?a')
and pattern[0] == '?‘ True
>>> is_variable('?abc')
and len(pattern) > 1 True
and pattern[1] != '*‘ >>> is_variable('?foo')
True
and pattern[1] in letters >>> is_variable('foo')
and ' ' not in pattern) False
9
match_variable
def match_variable(var, value, bindings): bindings = {}
binding = bindings.get(var) >>> match_variable("?a", 10, bindings)
if not binding: {'?a': 10}
bindings.update({var: value}) >>> match_variable("?b", 20, bindings)
return bindings {'?a': 10, '?b': 20}
if value== bindings[var]: >>> match_variable("?c", 30, bindings)
{'?a': 10, '?b': 20, '?c': 30}
return bindings
>>> match_variable("?c", 30, bindings)
return False {'?a': 10, '?b': 20, '?c': 30}
>>> match_variable("?b", 40, bindings)
False
>>> match_variable("?d", 40, bindings)
{'?a': 10, '?b': 20, '?c': 30, '?d': 40}
10
contains_tokens
def contains_tokens(pattern):
return type(pattern) is list >>> contains_tokens([1,2,3])
and len(pattern) > 0 True
>>> contains_tokens([])
False
>>> contains_tokens('a')
False

11
match_pattern
def match_pattern(pattern, input, bindings=None):
if bindings is False:
return False >>> match_pattern(['a', '?d', '?e'], ['a', 'b', 'c'])
if pattern == input: P= ['a', '?d', '?e'] I= ['a', 'b', 'c'] B = None
return bindings
P= a I= a B = {}
bindings = bindings or {}
if is_variable(pattern): P= ['?d', '?e'] I= ['b', 'c'] B = {}
var = pattern[1:] P= ?d I= b B = {}
return match_variable(var, [input], bindings) P= ['?e'] I= ['c'] B = {'d': ['b']}
elif contains_tokens(pattern) and P= ?e I= c B = {'d': ['b']}
contains_tokens(input): P= [] I= [] B = {'d': ['b'], 'e': ['c']}
return match_pattern(pattern[1:],
input[1:], {'d': ['b'], 'e': ['c']}
match_pattern(pattern[0], input[0],
bindings))
else:
return False
12
match_pattern
def match_pattern(pattern, input, bindings=None):
if bindings is False:
return False >>> match_pattern(['a', 'b', 'c'], ['a', 'b', 'd'])
if pattern == input: P= ['a', 'b', 'c'] I= ['a', 'b', 'd'] B = None
return bindings
P= a I= a B = {}
bindings = bindings or {}
if is_variable(pattern): P= ['b', 'c'] I= ['b', 'd'] B = {}
var = pattern[1:] P= b I= b B = {}
return match_variable(var, [input], bindings) P= ['c'] I= ['d'] B = {}
elif contains_tokens(pattern) and P= c I= d B = {}
contains_tokens(input): P= [] I= [] B = False
return match_pattern(pattern[1:],
input[1:], False
match_pattern(pattern[0], input[0],
bindings))
else:
return False
13
5) def match(pattern, input):
return match_pattern(pattern.split(),input.split())
print(match( '?a my name is ?n', 'hello my name is ali'))
P= ['?a', 'my', 'name', 'is', '?n'] I= ['hello', 'my', 'name', 'is', 'ali'] B = None
P= ?a I= hello B = {}
P= ['my', 'name', 'is', '?n'] I= ['my', 'name', 'is', 'ali'] B = {'a': ['hello']}
P= my I= my B = {'a': ['hello']}
P= ['name', 'is', '?n'] I= ['name', 'is', 'ali'] B = {'a': ['hello']}
P= name I= name B = {'a': ['hello']}
P= ['is', '?n'] I= ['is', 'ali'] B = {'a': ['hello']}
P= is I= is B = {'a': ['hello']}
P= ['?n'] I= ['ali'] B = {'a': ['hello']}
P= ?n I= ali B = {'a': ['hello']}
P= [] I= [] B = {'a': ['hello'], 'n': ['ali']}
{'a': ['hello'], 'n': ['ali']} 14
Assignment 8
QUESTION: Trace the following match_pattern function call
i.e. show the input and output of all inner recursive calls of
match_pattern function.

>>> match_pattern(['a', '?x', 'c', '?y', 'e'], ['a', 'b', 'c', 'd' , 'e'])

15

You might also like