forked from AtharvaEXE/Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strmath.py
107 lines (92 loc) · 2.41 KB
/
strmath.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
from num2words import num2words
from word2number import w2n
def add(query):
query = query.replace('what is', '')
if '+' in query:
list = query.split('+')
if 'plus' in query:
list = query.split('plus')
if 'add' in query:
query = query.replace('add', '')
if 'to' in query:
list = query.split('to')
else:
list = query.split('and')
try:
list[0] = num2words(list[0])
except:
pass
try:
list[1] = num2words(list[1])
except:
pass
a = w2n.word_to_num(list[0])
b = w2n.word_to_num(list[1])
return num2words(a + b)
def subtract(query):
query = query.replace('what is', '')
if '-' in query:
list = query.split('-')
if 'minus' in query:
list = query.split('minus')
if 'subtract' in query:
query = query.replace('subtract', '')
list = query.split('from')
try:
list[0] = num2words(list[0])
except:
pass
try:
list[1] = num2words(list[1])
except:
pass
a = w2n.word_to_num(list[0])
b = w2n.word_to_num(list[1])
return num2words(a - b)
def divide(query):
if 'divided by' in query:
query = query.replace('what is', '')
list = query.split('divided by')
elif 'divide' in query:
query = query.replace('divide', '')
if 'by' in query:
list = query.split('by')
else:
list = query.split('from')
try:
list[0] = num2words(list[0])
except:
pass
try:
list[1] = num2words(list[1])
except:
pass
a = w2n.word_to_num(list[0])
b = w2n.word_to_num(list[1])
return num2words(a / b)
def multiply(query):
if 'what is' in query:
query = query.replace('what is', '')
if 'times' in query:
list = query.split('times')
else:
list = query.split('multiplied by')
elif 'multiply' in query:
query = query.replace('multiply', '')
if 'by' in query:
list = query.split('by')
else:
list = query.split('times')
try:
list[0] = num2words(list[0])
except:
pass
try:
list[1] = num2words(list[1])
except:
pass
a = w2n.word_to_num(list[0])
b = w2n.word_to_num(list[1])
return num2words(a * b)
def lol():
print('lol')