AI-WK-13-Lec-25-26 Asg-10

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

CASE STUDY 3: STUDENT

(Classical AI program to solve mathematical problem input in the form of English


paragraph)

Dr. Umair Abdullah


Learning Objectives
• Solution of assignment # 9
• Introduction of Student Program
• Example Calls
• Student Modules (Code of Student)
• Removing noise words
• Some Student Rules
• Module 1. Translating English Paragraph to Mathematical Expressions
– Code of Translate to expression module
– Simple small example Tracing
– Example tracing of English question to list of equations
match_pattern
def match_pattern(pattern, input, bindings=None):
if bindings is False:
return False
if pattern == input:
return bindings
bindings = bindings or {}
if is_variable(pattern):
var = pattern[1:]
return match_variable(var, [input], bindings)
if is_segment(pattern):
token = pattern[0]
var = token[2:]
return match_segment(var, pattern[1:], input, bindings)
elif contains_tokens(pattern) and contains_tokens(input):
return match_pattern(pattern[1:], input[1:], match_pattern(pattern[0], input[0], bindings))
else:
return False
‘STUDENT’ Introduction
• Student is one of the early language understanding program, written by Daniel
Bobrow as his Ph.D. research project in 1964.
• It is designed to read and solve the kind of word problems found in high school
algebra books. For example;
• “if the number of customers Tom gets is twice the square of 20 % of the number of
advertisements he runs, and the number of advertisements is 45 , then what is the
number of customers Tom gets ?”
• Student could correctly find that the number of customers is 162. To do this
student must be far more sophisticated than Eliza; it must process and
“understand” a great deal of the input, rather than just concentrate on a few key
words. And it must compute the response rather than just fill in blanks.
• To transform the English paragraph it also employs the pattern-matching algorithm.
But the translating algorithm is itself recursive process. Further solving of equations
and isolating a variable is also a recursive.
Sample Problems
1. what is twice of square of z . if z is 3
2. The average score is 73 . the maximum score is 97 . what is the square of the
difference between the average and maximum scores ?
3. Tom age is twice of Mary age . James age is half of the difference between Mary
and Tom ages . Mary is 18 years old . how old is James ?
4. The price of a radio is 680 rupees . if this price is 10 % less than the marked price .
find the marked price .
5. x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . 8 divided by y is
1/4
6. If the number of customers Tom gets is twice of the square of 20 % of the number
of advertisements he runs . the number of advertisements is 45 . if the profit Tom
receives is 10 times the number of customers he gets . what is the profit ?
Sample Problems
7. Fran age divided by Robin height is one half of Kelly IQ . Kelly IQ minus 30 is Robin height. If Robin
is 4 feet tall . how old is Fran ?
8. The cost of MCS is equal to admission fee plus tuition fee of all semesters. Tuition fee is equal to
semester fee multiplied by total number of semesters of MCS program. Total number of semesters
in MCS program is 4 . Semester fee is 40000. Admission fee is 12000. Find the cost of MCS program.
9. 75 % attendance is required for every course. attendance can be calculated as sum of class
attendance and lab attendance. percentage can be calculated as attendance of a student multiplied
by 100 divided by total classes held. what is percentage of Asif. if his class attendance is 30 . lab
attendance is 20. While total classes held are 60.
10. Sum of theory and practical marks obtained by a student makes the total marks of the student for a
course. Similarly, Theory marks are calculated by adding Internal marks and marks obtained during
exams. Moreover, marks of exams can be further distributed into Mid exam and Final Exam.
Internal marks can be divided into Quizzes and Assignments. Find total marks obtained by Asif if he
has obtained . 8 Marks in Quizzes. 6 marks in Assignments. 10 marks in Mid . 20 marks in Final
exam. Finally, 12 in practical lab work.
STUDENT DEMO
• Open PyCharm
• Write student code (along with patmatch.py)
• Run the student.py
• Type and enter the statement
• Verify the results
Student Code Summary
Main student functions Solve_equation
1. from patmatch import * 1. in_exp(itm, lst)
2. Variables: student_rules, 2. reverse_op(op)
letters, operators, numbers 3. isolate(var, eq)
3. student 4. solve_equations(eqs)
4. remove_noise 5. print_equations(eqs)
Translate_to_expression 6. solve(eqs,solved=[])
5. toeqstr(elst) 7. solve_equation(var, eq)
6. replace_item(n,o,lst) 8. find_one_unknowneq(eqs)
7. translate_to_exp(eqwlst) 9. one_unknown2(equation)
Student Code
from patmatch import *
operators = "+-*/%"
numbers = "0123456789"
letters = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ"

def student():
userinput = input("Enter Statement:")
userinput = userinput.lower()
engEqs = userinput.split('.')
eqs = []
for engeq in engEqs:
engeq = engeq.split()
eqwlst = remove_noise(engeq)
eqs.append(translate_to_exp(eqwlst))
eqs = [eq for eq in eqs if eq != []]
solve_equations(eqs)
Remove noise words
def remove_noise(txt):
nwords = ['a','the','number','of','this','if','that','$',
'similarly,', 'finally,', 'moreover,', 'further',
', and', 'his', 'while']
for w in nwords:
if txt.count(w) > 0:
txt = [a for a in txt if a != w]
return txt
Student Rules
('find ?*a' , ['To-find' ,'=', '?a']),
('?*a is equal to ?*b', ['?a' ,'=', '?b']),
('?*a equal to ?*b' , ['?a' ,'=', '?b']),
('?*a is ?*b' , ['?a' ,'=', '?b']),
('twice ?*a' , ['2' ,'*', '?a']),
('sum ?*a and ?*b' , ['?a' ,'+', '?b']),
('half ?*a' , ['?a' ,'/', '2']),
('square ?*a' , ['?a' ,'*', '?a']),
('?*a plus ?*b' , ['?a', '+', '?b']),
('?*a minus ?*b' , ['?a', '-', '?b'])
Translate to Expression Modules
def translate_to_exp(eqwlst):
print('Input: ', eqwlst)
for pattern, response in student_rules:
pattern = pattern.split()
bindings = match_pattern(pattern, eqwlst)
if bindings:
print('Rule: ', pattern, ' ', response)
print('Binding:', bindings)
for var,val in bindings.items():
tval = translate_to_exp(val)
response = replace_item(tval,'?'+var,response)
print('Output:',response)
return response
if eqwlst == []:
return eqwlst
return eqwlst[0]
1. what is twice of square of z . if z is 3 1. Input: ['z', 'is', '3']
('find ?*a' , ['To-find','=','?a']),
2. Input: ['what', 'is', 'twice', 'square', 'z'] 2. Rule: ['?*a', 'is', '?*b']
['?a', '=', '?b']
('?*a is equal to ?*b',
3. Rule: ['?*a', 'is', '?*b'] ['?a', '=', '?b']
['?a' ,'=', '?
4. Binding: {'a': ['what'], 3. Binding: {'a': ['z'], 'b': ['3']}
b']),
'b': ['twice', 'square', 'z']} 4. Input: ['z']
('?*a equal to ?*b' ,
5. Input: ['what'] 5. Input: ['3']
['?a' ,'=', '?
6. Input: ['twice', 'square', 'z'] 6. Output: ['z', '=', '3'] b']),
7. Rule: ['twice', '?*a'] ['2', '*', '?a'] ('?*a is ?*b', ['?a' ,'=', '?b']),
8. Binding: {'a': ['square', 'z']} UNSOLVED EQUATIONS
9. Input: ['square', 'z'] 7. what = ( 2 * ( z * z ) ) ('twice ?*a', ['2' ,'*', '?a']),
10. Rule: ['square', '?*a'] ['?a', '*', '?a'] 8. z = 3 ('sum ?*a and ?*b' ,
11. Binding: {'a': ['z']} ['?a' ,'+',
12. Input: ['z'] SOLVED EQUATIONS '?b']),
13. Output: ['z', '*', 'z'] 9. z = 3 ('half ?*a' , ['?a' ,'/', '2']),
14. Output: ['2', '*', ['z', '*', 'z']] 10. what = 18 ('square ?*a' , ['?a' ,'*', '?a']),
('?*a plus ?*b', ['?a', '+', '?b']),
15. Output: ['what', '=', ['2', '*', ['z', '*', 'z']]]
('?*a minus ?*b', ['?a', '-', '?b'])
x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . and 8 divided by y is 1/4
1. Input:['x','plus','y', 'is', 'equal', 'to', 'z', 'minus', '5'] 1. Input: ['find', 'square', 'z']
2. Rule: ['?*a', 'is', 'equal', 'to', '?*b'] ['?a', '=', '?b']
3. Binding: {'a': ['x', 'plus', 'y'], 'b': ['z', 'minus', '5']} 2. Rule: ['find', '?*a'] ['To-find', '=', '?a']
4. Input: ['x', 'plus', 'y'] 3. Binding: {'a': ['square', 'z']}
5. Rule: ['?*a', 'plus', '?*b'] ['?a', '+', '?b']
6. Binding: {'a': ['x'], 'b': ['y']}
4. Input: ['square', 'z']
7. Input: ['x'] 5. Rule: ['square', '?*a'] ['?a', '*', '?a']
8. Input: ['y']
6. Binding: {'a': ['z']}
9. Output: ['x', '+', 'y']
10. Input: ['z', 'minus', '5'] 7. Input: ['z']
11. Rule: ['?*a', 'minus', '?*b'] ['?a', '-', '?b'] 8. Output: ['z', '*', 'z']
12. Binding: {'a': ['z'], 'b': ['5']}
13. Input: ['z']
9. Output: ['To-find', '=', ['z', '*', 'z']]
14. Input: ['5']
15. Output: ['z', '-', '5']
16. Output: [['x', '+', 'y'], '=', ['z', '-', '5']]
x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . and 8 divided by y is 1/4
1. Input: ['6', 'minus', 'x', 'is', '4'] 1. Input: ['8', 'divided', 'by', 'y', 'is', '1/4']
2. Rule: ['?*a', 'is', '?*b'] ['?a', '=', '?b'] 2. Rule: ['?*a', 'is', '?*b'] ['?a', '=', '?b']
3. Binding: {'a': ['6', 'minus', 'x'], 'b': ['4']} 3. Binding: {'a': ['8', 'divided', 'by', 'y'], 'b': ['1/4']}
4. Input: ['6', 'minus', 'x'] 4. Input: ['8', 'divided', 'by', 'y']
5. Rule: ['?*a', 'minus', '?*b'] ['?a', '-', '?b'] 5. Rule: ['?*a', 'divided', 'by', '?*b'] ['?a', '/', '?b']
6. Binding: {'a': ['6'], 'b': ['x']} 6. Binding: {'a': ['8'], 'b': ['y']}
7. Input: ['6'] 7. Input: ['8']
8. Input: ['x'] 8. Input: ['y']
9. Output: ['6', '-', 'x'] 9. Output: ['8', '/', 'y']
10. Input: ['4'] 10. Input: ['1/4']
11. Output: [['6', '-', 'x'], '=', '4'] 11. Output: [['8', '/', 'y'], '=', '1/4']
x plus y is equal to z minus 5 . find square of z . 6 minus x is 4 . and 8 divided by y is 1/4

1. Output: [['x', '+', 'y'], '=', ['z', '-', '5']] SOLVED EQUATIONS
2. Output: ['To-find', '=', ['z', '*', 'z']]
3. Output: [['6', '-', 'x'], '=', '4']
1. x=2
4. Output: [['8', '/', 'y'], '=', '1/4']
2. y = 2.0
UNSOLVED EQUATIONS 3. z = 9.0
4. To-find = 81.0
5. (x+y)=(z-5)
6. To-find = ( z * z )
7. (6-x)=4
8. ( 8 / y ) = 1/4
Assignment 10
QUESTION: Show the process how translate_to_expression module
of student program will convert following paragraph into equations

75 % attendance is required for every course. attendance can be


calculated as sum of class attendance and lab attendance. percentage
can be calculated as attendance of a student multiplied by 100
divided by total classes held. what is percentage of Asif. if his class
attendance is 30 . lab attendance is 20. While total classes held are
60.

You might also like