-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoperators.py
More file actions
183 lines (156 loc) · 5.54 KB
/
operators.py
File metadata and controls
183 lines (156 loc) · 5.54 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import evaluate
import txt2num
#TODO:
#nested sum speech!!! #decimal notation
def keywords():
return ['plus','sum','add','minus','subtract','product','multiply','times','divide','divided','quotient','to','power','mod','modulus'];
def sum_evaluate(string):
words = txt2num.num_fix(string)
words = words.split()
prefix_flag = 0
operator_keywords = keywords()
operator_keywords.remove('plus')
operator_keywords.remove('sum')
operator_keywords.remove('add') #specify for sum evaluate
if words[0] == "sum":
prefix_flag = 1
if words[1] == "of":
words = words[2:]
elif words[1] == "up":
words = words[2:]
else:
words = words[1:]
elif words[0] == "add":
prefix_flag = 1
if words[1] == "up":
words = words[2:]
else:
words = words[1:]
output = words[0] #initialize the first summand
words = words[1:]
num_words = len(words)
if prefix_flag: #we need to add in the plus symbols
for w in range(num_words):
if words[w] == "and":
output += "+" + words[w+1]
break
else:
output += "+"+words[w]
else: #we don't need to add the plus symbols
for w in range(num_words):
if words[w] == "plus":
output += "+"
elif words[w] in operator_keywords:
output += evaluate.evaluate(words[w:]) #pass the rest of words to evaluate
print output
return output
else:
output += words[w]
print output
return output
sum_evaluate('four plus zero')
sum_evaluate('four plus one plus four')
sum_evaluate('four plus two plus twenty five plus one hundred four plus four')
sum_evaluate('four plus one hundred three minus four')
sum_evaluate('sum of four four and four')
sum_evaluate('add five four and four')
def subtract_evaluate(string):
words = txt2num.num_fix(string)
words = words.split()
prefix_flag = 0
operator_keywords = keywords()
operator_keywords.remove('subtract')
operator_keywords.remove('minus') #specify for subtract evaluate
if words[0] == "subtract": #add take away?
prefix_flag = 1
words = words[1:]
output = words[0] #initialize the first difference
words = words[1:]
num_words = len(words)
if prefix_flag: #we need to add in the minus symbols
if words[1] == "from":
output += "-"+words[2]
else:
output += '-'+words[1]
else: #we don't need to add the minus symbols
for w in range(num_words):
if words[w] == "minus":
output += "-"
elif words[w] in operator_keywords:
output += evaluate.evaluate(words[w:]) #pass the rest of words to evaluate
print output
return output
else:
output += words[w]
print output
return output
subtract_evaluate('one minus twenty five')
subtract_evaluate('thirty two minus one hundred nine minus one hundred and four')
subtract_evaluate('four minus three minus seventy two minus forty nine minus zero')
subtract_evaluate('seven minus six plus five')
subtract_evaluate('subtract ten from ten')
def multiply_evaluate(string):
words = txt2num.num_fix(string)
words = words.split()
prefix_flag = 0
operator_keywords = keywords()
operator_keywords.remove('times')
operator_keywords.remove('product')
operator_keywords.remove('multiply') #specify for sum evaluate
if words[0] == "product":
prefix_flag = 1
if words[1] == "of":
words = words[2:]
else:
words = words[1:]
elif words[0] == "multiply":
prefix_flag = 1
words = words[1:]
output = words[0] #initialize the first summand
words = words[1:]
num_words = len(words)
if prefix_flag: #we need to add in the plus symbols
for w in range(num_words):
if words[w] == "and":
output += "*" + words[w+1]
break
if words[w] == "by":
output += "*" + words[w+1]
break
else:
output += "*"+words[w]
else: #we don't need to add the plus symbols
for w in range(num_words):
if words[w] == "times":
output += "*"
elif words[w] in operator_keywords:
output += evaluate.evaluate(words[w:]) #pass the rest of words to evaluate
print output
return output
else:
output += words[w]
print output
return output
multiply_evaluate('one times twenty five')
multiply_evaluate('thirty two times one hundred nine times one hundred and four')
multiply_evaluate('four times three times seventy two times forty nine times zero')
multiply_evaluate('seven times six plus five')
multiply_evaluate('product of one hundred and one hundred')
multiply_evaluate('multiply ten and ten')
multiply_evaluate('multiply eleven by eleven')
def mod_evaluate(string): #do txt2num
words = txt2num.num_fix(string)
words = words.split()
output = words[0] + "%" + words[2]
print output
return output
mod_evaluate("four mod seven")
mod_evaluate("four thousand mod seventy six")
def exp_evaluate(string): #do txt2num
words = txt2num.num_fix(string)
words = words.split()
output = words[0] + "**" + words[2]
print output
return output
exp_evaluate("four mod seven")
exp_evaluate("four thousand mod seventy six")