-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathBot.py
142 lines (113 loc) · 4.91 KB
/
mathBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import math
class isMath():
"""A class to instatiate an object of the message
input by the user if is it a math question"""
num = ""
currentEval = 0
eval = False
#Flags to decide if the question is a math one
numCheck = False
opCheck = False
rootCheck = False
mathQ = False
def __init__(self, stringInp):
"""Manipulate user input as string to check if it follows the pattern of a math question"""
#Change english to operators and overwrite the input
stringInp = self.__checkDict(stringInp)
#Loop through string, check if both and operator and number are included or there is a square root using mathBot
for i in range(len(stringInp)):
if (self.__isNum(stringInp[i])):
self.numCheck = True
elif (self.__checkOperator(stringInp[i])):
self.opCheck = True
elif (self.__checkRoot(stringInp)):
self.rootCheck = True
#If both operator and number are included or there is a square root then run calculate from mathBot
if (self.numCheck == True and self.opCheck == True) or self.rootCheck:
self.mathQ = True
self.__getAns(stringInp)
def __getAns(self, stringInp):
"""Goes through the string input and checking for open and closed brackets,
when the bracket is open the characters are appened to a variable until a closed
bracket or the end of the sequence. Anything appended is evaluated."""
for i in range(len(stringInp)):
current = stringInp[i]
#Checks if the current character is a number or operator
if current == "(":
self.eval = False
self.num = str(self.num) + str(current)
continue
elif current in "0123456789.+-/*%":
self.num = str(self.num) + str(current)
if current == "/" and stringInp[i + 1] == "0":
self.currentEval = "unable to be calculated!"
break;
elif i != len(stringInp)-1 and stringInp[i+1] == ")":
self.num = str(self.num)+ ")"
self.eval = True
elif i == len(stringInp) - 1:
self.eval = True
if self.eval == True:
if "square root" in stringInp or "sqrt" in stringInp:
if int(self.num) < 0:
self.currentEval = "unable to be calculated!"
break;
else:
self.currentEval = math.sqrt(float(self.num))
else:
self.currentEval = eval(self.num)
self.num = self.currentEval
self.eval = False
self.currentEval = 'The answer is {}'.format(self.currentEval)
def __isNum(self, item):
"""Check if the argument passed is a number,
input can be of any type and output will be a boolean"""
try:
int(item)
return True
except ValueError:
return False
def __checkOperator(self, operator):
"""Check if the argument is an operator by comparing
to a list of valid operators, input is a string and output is a boolean"""
validOperators = ["+","-","*","/","%","**","//"]
if operator in validOperators:
return True
else:
return False
def __checkRoot(self, userInp):
"""Check if any English variations of asking for
the square root have been entered, takes thse user input as a string
and returns a boolean"""
validArgs = ["square root","squareroot","sqrt"]
for arg in validArgs:
if arg in userInp:
return True
else:
return False
def __checkDict(self, userInp):
"""Replace any English words for operators with the python
symbol for that given operator using the dictionary mathDict
user input is a string and the output is a string of the new userInp variable"""
mathDict = {
"to the power of":"**",
"divided by":"/",
"over":"/",
"x":"*",
"times by":"*",
"times":"*",
"multiplied by":"*",
"plus":"+",
"add":"+",
"minus":"-",
"take away":"-",
"modulus":"%",
"mod":"%",
"modulo":"%"
}
#Checks every key in the dictionary to see if it is in the user input
#and replaces any found with python equivalent
for key in mathDict:
if key in userInp:
userInp = userInp.replace(key,mathDict[key])
return userInp