Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
carreira committed Nov 28, 2018
1 parent c8c9799 commit c352065a0d4f0c3c4be75111e4110cb5d158bf89
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 36 deletions.
@@ -0,0 +1,124 @@
from word2number.w2n import word_to_num
import re
import inflect

""" 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 indexex 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 indexex 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 indexex 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 indexex -> example: if the number in this indexex 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 indexex 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 indexex
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 indexex 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



def working(tot, acum,index,type):
if "sum" in type:
acum[index - 1] = tot # replaces the indexex before with the result of the operation
acum.remove(acum[index + 1]) # removes the other indexexes involved in the operation
acum.remove(acum[index]) # "" ""
sums(acum, index) # calls the function again, with the same indexex since we removed two indexexes from the list
else:
acum[index - 1] = tot # replaces the indexex before with the result of the operation
acum.remove(acum[index + 1]) # removes the other indexexes involved in the operation
acum.remove(acum[index]) # "" ""
mults(acum, index) # calls the function again, with the same indexex since we removed two indexexes from the list

@@ -0,0 +1,19 @@
import pymysql #allows connection to databases with sql


"""Connect to the database"""
def connection():
connect = pymysql.connect(host="localhost",
user="root",
passwd="",
database="users")
return connect









57 info.py
@@ -0,0 +1,57 @@
import random #this package allows to choose randomly an index of a list
from confirmDB import connection
from Setup import load_setting


""" prints a random answer to question about favourite things """
def informations(sentence):
answer = ["I don't want to share this with you", "If i tell you I would have to kill you", "I just don't know yet",
"Good question!"]
final = random.choice(answer)
print(final)
ownInfo(sentence)


""" gets information about favourites of the user"""
def ownInfo(sentence):
sep = 'favourite '
info = sentence.split(sep, 1)[0] #gets the words before the word "favourite" in a sentence to isolate "favourite" and the key word
info = info + sep #adds the word "favourite" to the already filtered sentence
info = sentence.replace(info, "") #gets the key word by removing all the words previous to "favourite" and "favourite" included
info = info.replace(" ", "")
nick = load_setting(0) #get the nick of current user
nick = nick.lower()
nick = nick[:-1]
"""--------------------------------------------"""
try:
connect = connection() # gets the details to the database
cursor = connect.cursor() # establish a cursor to fetch the results wanted from the databse
sql = 'SELECT ' + info + ' FROM favourite WHERE NICKNAME = "' + nick + '"' #sql gets the order to select all info from the table favourite in the current user row
cursor.execute(sql) # order to select executed
myresult = cursor.fetchall() # gets all the rows of nicknames where the nickname == nick
l = str(myresult[0]) # l = ('favourite food',) / (None,)
if "None" in l:
final = (l[1:])
final = final[:-2]
else:
final = (l[2:])
final = final[:-3]
if final == "None":
getInfo(nick, info)
else:
print("Your favourite ", info.lower() , " is ", final)
except:
getInfo(nick, info)


""" "add" new values to table favourite """
def getInfo(nick, info):
print("I don't know yours, what is it? ")
ans = input("")
connect = connection() # gets the details to the database
cursor = connect.cursor() # establish a cursor to fetch the results wanted from the databse
sql = 'UPDATE favourite SET '+info+'="'+ans+'" WHERE NICKNAME="'+ nick + '"' #sql gets the order to update all info from the table favourite in the current user row
cursor.execute(sql) # order to select executed
connect.commit() # tell the database to save all the changes
print("Ok, I will try not to forget!")

72 play.py
@@ -0,0 +1,72 @@
import webbrowser #
import glob #allows to have a list
import os #provides a portable way of using operating system functionalities
import random #allows to choose randomly an index of a list


def play(sentence):
mlist = (glob.glob("music/*.mp3")) #gets all the names of the musics inside the folder music, in the same directory as the python files
if "show" in sentence or "list" in sentence:
if "my" not in sentence:
if "all" in sentence:
filt = sentence
if "show" in sentence:
if "all" in sentence:
filt = filt.replace("show all music of ", "") #filters sentence to get the name to identify the music
else:
if "all" in sentence:
filt = filt.replace("list all music of ", "") #filters sentence to get the name to identify the music
filt = filt.lower()
print("All music of :", filt.upper() ," \n")
attp = 0
for i in mlist:
top = i.lower()
i = i[6:] #filter the value in order to get only the name of the music/artist
if filt in top:
print(i) #prints the songs with the name user wanted
attp = 1
if attp < 1:
print("You have no songs with that name")
else:
print("Your music: \n")
for i in mlist:
i = i[6:] #filter the value in order to get only the name of the music/artist
print(i) #prints the song


elif "random" in sentence or "shuffle" in sentence:
shuffle = random.choice(mlist) #chooses one random song
os.startfile(shuffle) #execute the song with that name
shuffle = shuffle[6:] #filters the name of the song to print only the name of the song and artist
print("Playing ", shuffle)
else:
if "play " in sentence:
counter = 0
newS = sentence.replace("play ", "") #gets the name of the song
for i in mlist:
top = i.lower()
newS = newS.lower()
if newS in top: #if finds the name of the song wanted
print("Playing ", i[6:])
os.startfile(i) #plays the music qanted
break
else:
if counter < (len(mlist)-1): #allows to continue searching while there are still musics to search
counter += 1
continue
else: #if doesn't find the song wanted in the list:
uOpen(sentence)
break


def uOpen(sentence):
sentence = sentence[5:] #sentence is sentence after play
print("Playing " + sentence)
url = "https://www.youtube.com/results?search_query=" + sentence
webbrowser.open(url) #opens google with that url

def uGoogle(sentence):
sentence = sentence[5:] #sentence is sentence after open
print("Opening " + sentence)
url = "https://www." + sentence
webbrowser.open(url) # opens google with that url
@@ -0,0 +1,31 @@
import smtplib # implements the SMTP protocol to be able to send emails

""" sends an email to new users with the details of the account
https://www.geeksforgeeks.org/send-mail-gmail-account-using-python/
"""
def email(nick, password, mail):
try:
server = smtplib.SMTP('smtp.gmail.com', 587) #declaration of gmail SMTP server settings
server.starttls() #For security, this start the SMTP connection in the Transport Layer Security mode that encrypts all the SMTP commands
server.login("theforeignersgroup@gmail.com", "wearethebest101") #information of the account which will send the email
msg = "Thank you for joining our CHATBOT!\nDont lose these email.\nYour account details to access our " \
"chatbot:\nLogin Username: " + nick + "\nPassword: " + password
server.sendmail("theforeignersgroup@gmail.com", mail, msg)
server.quit() #closes the SMTP session
except:
print("Something went wrong. Probably your wiki connection it is not the best for this service")


""" Allows the user to send emails to everyone he wants, using our email"""
def create_email():
try:
server = smtplib.SMTP('smtp.gmail.com', 587) #declaration of gmail SMTP server settings
server.starttls() #For security, this start the SMTP connection in the Transport Layer Security mode that encrypts all the SMTP commands
mail = input("Email: ")
server.login("theforeignersgroup@gmail.com", "wearethebest101") #information of the account which will send the email
msg = input("Message: ")
server.sendmail("theforeignersgroup@gmail.com", mail, msg)
server.quit() #closes the SMTP session
print("Done! What can I help you now?")
except:
print("Something went wrong. Probably your wiki connection it is not the best for this service")
@@ -0,0 +1,8 @@
import random #this package allows to choose randomly an index of a list


""" prints random answers to compliments """
def thanks():
answer = ["Thank you! :)", "I know, I know..", "I have never heard that before, thank you!",
"Probably you are right :)", "I don't care about what you think."]
print(random.choice(answer))

0 comments on commit c352065

Please sign in to comment.