Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from word2number.w2n import word_to_num #converts full english words into numbers -> font: https://pypi.org/project/word2number/
import re #necessary to know if there are certain caracters into one string -> font: https://stackoverflow.com/questions/9072844/how-can-i-check-if-a-string-contains-any-letters-from-the-alphabet
import inflect #allows to convert number into words -> font: https://stackoverflow.com/questions/19504350/how-to-convert-numbers-to-words-in-python
""" recevies the full sentence and change it in order to be compatible with the following operations. Calls others functions depeding on the operations needed."""
def entre(sentence):
acum = []
num = 0
if "plus" in sentence:
sentence = sentence.replace("plus", "+")
if "minus" in sentence:
sentence = sentence.replace("minus", "-")
if "times" in sentence:
sentence = sentence.replace("times", "*")
if "divided by" in sentence:
sentence = sentence.replace("divided by", "/")
if "power" in sentence:
sentence = sentence.replace("power", "^")
if "powers" in sentence:
sentence = sentence.replace("powers", "^")
if "what is " in sentence:
sentence = sentence.replace("what is ", "")
if "what" in sentence:
sentence = sentence.replace("what", "")
if "is" in sentence:
sentence = sentence.replace("is", "")
word = ""
final = ""
for k in sentence: #loop to check if there are any letters left in the sentence
cnf = (bool(re.match('[a-zA-Z]', k))) #returns True or False if letters in sentence
if cnf:
break
try:
if cnf: #if returned True, may be numbers in full english, change them to numbers
for i in sentence: #checks every indexes of the string to make the necessary treatment
if i in "+" or i in "-" or i in "*" or i in "/" or i in "^": #if this indexes has the value of an operation, the number must be completed
word = word_to_num(word) #if number is in english, becames a number
final += str(word) #add the number to the final string
final += i #add the operation to the final string
word = ""
continue
if i in " ":
continue
else:
word += i #keeps adding the letter/number to have the full word or full number wanted
word = word_to_num(word) #adds the final number to the final string (initial string ended so it is needed to add again since it was not added before
final += str(word)
sentence = final #finally the initial string, having numbers in it in english, becames only numbers.
except:
print("Only full english or just numbers!")
if " " in sentence:
sentence = sentence.replace(" ", "")
try:
for i in sentence: #loop to insert full numbers into a list
if i in "+" or i in "-" or i in "*" or i in "/" or i in "^":
acum.append(num) #appends the number wanted to the list
acum.append(i) #appends the operation wanted to the list
num = 0
continue
else:
num = num * 10 #if the indexes is still a number, the previous numbers are multiplied by 10 -> example: if num was 3, now is 30
num = num + float(i) #now, add the number in the indexes -> example: if the number in this indexes is 2, now the full number is 32
acum.append(num) #adds the final number wanted to the list one last time
acum = mults(acum, 0)
acum = sums(acum, 0)
if cnf: #if there was letters in the initial sentence
p = inflect.engine()
acum = p.number_to_words(acum[0]) #the final result becames in full english
if "point zero" in acum:
acum = acum.replace("point zero", "")
print("Easy, the answer is ", acum)
else: #if not, prints the final result
print("Easy, the answer is ", acum[0])
except:
print("Try again")
""" checks if there is any multiplication/division/power to do because they have priority.
if so the operation is done - gets as parameters the list acum and the indexes number - returns the result of this operations"""
def mults(acum, index):
if index < len(acum):
if acum[index] == "*":
tot = float(acum[index - 1]) * float(acum[index + 1])
working(tot,acum, index, "mult")
elif acum[index] == "^":
tot = float(acum[index - 1]) ** float(acum[index + 1])
working(tot,acum, index, "mult")
elif acum[index] == "/":
tot = float(acum[index - 1]) / float(acum[index + 1])
working(tot,acum, index, "mult")
else:
mults(acum, index + 1) #if index is a number, using recursion we call the function again with to check the following indexes
return acum
""" checks if there is any sums/subtractions to do.
if so the operation is done - gets as parameters the list acum and the indexes number - returns the final result"""
def sums(acum, index):
if index < len(acum):
if acum[index] == "+":
tot = float(acum[index - 1]) + float(acum[index + 1])
working(tot,acum, index, "sum")
elif acum[index] == "-":
tot = float(acum[index - 1]) - float(acum[index + 1])
working(tot,acum, index, "sum")
else:
sums(acum, index + 1)
return acum
""" replaces the indexes after the operations are done. After that if calls again the function in order to keep the operations """
def working(tot, acum,index,type):
acum[index - 1] = tot # replaces the indexes before with the result of the operation
acum.remove(acum[index + 1]) # removes the other indexeses involved in the operation
acum.remove(acum[index]) # "" ""
if "sum" in type:
sums(acum, index) # calls the function again, with the same indexes since we removed two indexeses from the list
else:
mults(acum, index) # calls the function again, with the same indexes since we removed two indexes from the list