AI-WK-13-Lec-25-26 Asg-10
AI-WK-13-Lec-25-26 Asg-10
AI-WK-13-Lec-25-26 Asg-10
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