Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
mockk committed Nov 27, 2018
1 parent cc985c8 commit f108f94c4fc80d70953b2bf42ec81e18a3b80ef5
Show file tree
Hide file tree
Showing 18 changed files with 1,265 additions and 782 deletions.
@@ -0,0 +1,60 @@
''' All codes in this file are extracted and editted from https://stackoverflow.com/questions/25798674/python-duplicate-words '''
def isRepeat(sentence):
words = sentence.split(" ")
words.sort() # ascending sorting

# keep user original input
words_copied = sentence.split(" ")
words_copied.sort()

wordL=[]
'''Copied codes to loop through the sentence given.'''
for word in words[0:len(words)-1]:
count = 0
while True:
try:
index = words_copied.index(word)
count += 1
del words_copied[index]
except ValueError:
if count > 1:
wordL.append([word,count])
break
'''Copied code ends'''
if wordL != []:
repeat=True
else:
repeat=False
return repeat

def CountRepeat(sentence):
words = sentence.split(" ")
words.sort() # ascending sorting

# keep user original input
words_copied = sentence.split(" ")
words_copied.sort()

wordL={}

for word in words[0:len(words)-1]:
count = 0
while True:
try:
index = words_copied.index(word)
count += 1
del words_copied[index]
except ValueError:
if count > 1:
wordL.update({word:count})
break
if wordL != []:
repeat=True
else:
repeat=False
return wordL

#sentence=input('Enter your sentence : ')
#print(isRepeat(input()))
#print(CountRepeat(sentence))

@@ -0,0 +1,42 @@
# Sentences for responding the user
MyDictionary={
"GREETING_KEYWORDS":["hello", "hi", "greetings", "sup", "what's up"],
"GREETING_RESPONSES":["'sup bro", "hey", "*nods*", "hey you get my snap?"],
"REFUSE":["no","nah","not"],
"AGREE":["yes","sure","agreed","why not"],
"EMOTION_POSITIVE":["happy","thrilled","enjoyed","yo","high spirits"],
"EMOTION_NEGATIVE":["sad","blue","dissatisfied","heck","hell no"]
}

def ref(category):
catName=[]
for j,k in MyDictionary.items(): #listing a whole dictionary
catName.append(j)
for i in catName:
if (category.upper()).strip() == i:
return i
if (category.upper()).strip() not in catName:
return False



'''Function checks if a value is in a specified dictionary'''
def isRef(word):
try:
for j,k in MyDictionary.items(): #listing a whole dictionary
for i in k: #looping through the dictionary
for p in word.split():
if (p.lower()).strip()==i:
return True
else:
continue
continue
continue
except:
print("Invalid input!")
print("Type of i: ",type(i),";type of p: ",type(p))
else:
return False

#print(isRef(input("Test a word: ")))
#print(ref(input("Test a word: ")))
@@ -2,37 +2,8 @@

Multi-purpose discord chatbot for module 4006CEM - Oct/Nov 2018

Usage: `python3 connector.py`
Usage: python3 connector.py

Libraries being used:
- requests (http://docs.python-requests.org/en/master/) by Kenneth Reitz for web requests
- bs4 (https://www.crummy.com/software/BeautifulSoup/) by Leonard Richardson for parsing HTML pages
- discord.py (https://github.com/Rapptz/discord.py/) by Rapptz used to connect to Discord.
- selenium (https://docs.seleniumhq.org/) by (originally) Jason Huggins used to scrap pages that use javascript calls for data (translate module)
# Python 3.5 or larger must be installed due to asynchronous functions

How to get god-forsaken Selenium to work:
(https://selenium-python.readthedocs.io/installation.html)

Step1. sudo python3 -m pip install selenium

Step2. download Driver for your browser (I chose Chrome, download links in the link above)

Step3. Set this part in your code to match your locations of: 1. Driver you've just downloaded, 2. Browser's exe location

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.binary_location = r"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
chrome_driver_binary = "/mnt/c/Users/your_username/chromedriver.exe"

Step4. When creating a driver, do it like this:

driver = webdriver.Chrome(chrome_driver_binary, chrome_options = chrome_options)

Step5. Pray to lord that it is working now




# Python 3.5 or 3.6 must be installed due to asynchronous functions
# Please state which modules/components needed to be installed before executing your program, thanks!
@@ -0,0 +1,99 @@
from MyDictionary import *
def social(chat):
import random
try:
user=input("Can you share your life with me? ")
except:
print("Invalid input!")
else:
for i in user.split():
if i.lower() in MyDictionary["REFUSE"] or i.lower() in MyDictionary["Emotion_negative"]:
try:
user=input("I'm so sorry affecting your bad mood. So do you want to leave, or stay with my other services?(leave/service) ")
except:
print("Invalid input!")
else:
for j in user.split():
if (str(j.lower())).strip() in ["leave"]:
exit()
elif (str(j.lower())).strip() in ["service"]:
from main2 import main2
main2()
else:
print("I don't know what the hell are yo saying dude!!!")
try:
chat=input("Say again please: ")
except:
print("Invalid input!")
else:
social(chat)
else:
for j in user.split():
if str(j.lower()) in Emotion_positive:
em=True
break
else:
em=False
continue
print(em)
res="I'm quite "
from parrot import find_longest_word
if em==True:
print(res,"glad to hear that you are",find_longest_word(user.split()),".")
else:
try:
user=input("Sorry what do you mean by ",find_longest_word(user.split()),"?")
except:
print("Invalid input!")
finally:
print(res,"disappointed to hear that you said",find_longest_word(user.split()),".")


'''Greet each other'''
def greet():
import random
while True:
res=""
found=False
try:
user=input("Hello, what's up bro? ")
for i in user.split():
if i.lower() in GREETING_KEYWORDS:
print(GREETING_RESPONSES[random.randint(1,len(GREETING_RESPONSES))-1] ) # random generate a response based on user input
found=True
else:
if i.index(user.split())<len(user.split())-1:
continue
else:
found=False # return error
while found==False:
for j in user.split():
if j.lower() in (GREETING_KEYWORDS.lower()).strip(): # check if greeting words are in the user input
res=GREETING_RESPONSES[random.randint(0,len(GREETING_RESPONSES-1))] # returns a greeting statement if yes
found==True
else:
from Checkings import isRepeat,CountRepeat
if isRepeat(user)==True:
for k in range(0,len(CountRepeat(user))):
print("Word '",CountRepeat(user)[k][0],"' has been repeated:",CountRepeat(user)[k][1]," times.")
print("Response '",str(i),"' in '",str(user),"' not found sorry!")
else:
print("",end="")
if found==True:
break
else:
print("Error not found for: "+str(user))
user=input("Hello, what's up bro? ")
except: # error exception
print("Invalid input!")
else:
user=input("Are you feeling great with me today? ") # prompt to ask if user wants to start chatting
status=True
for i in user.split():
if i.lower() in REFUSE: # return to main menu
from main2 import main2
main2()
else: # start chatting
print("Great! \n")
social(user)
'''Greeting part ends'''
@@ -1,26 +1,29 @@
"""
Libraries:
- requests (http://docs.python-requests.org/en/master/) by Kenneth Reitz for web requests
"""
import requests
import xml.etree.ElementTree as ET

""" Returns a dictionary of currencies and rates. Coded with help from https://docs.python.org/2/library/xml.etree.elementtree.html """
async def getCurrencyDictionary():
response = requests.get("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml").text
root = ET.fromstring(response)
curr={}
for child in root[2][0]:
curr[child.attrib["currency"]] = float(child.attrib["rate"])
curr["EUR"] = 1
curr["XDR"] = 1/(0.423 + 12.1*(1/curr["JPY"]) + 0.111*(1/curr["GBP"]) + 0.66*(1/curr["USD"]))
return curr


async def convertCurrency(to, base, amount):

if amount <= 0:
return amount

currencies = await getCurrencyDictionary()
return amount*currencies[to]/currencies[base]
'''Code is written by Ben.'''


"""
Libraries:
- requests (http://docs.python-requests.org/en/master/) by Kenneth Reitz for web requests
"""
import requests
import xml.etree.ElementTree as ET

""" Returns a dictionary of currencies and rates. Coded with help from https://docs.python.org/2/library/xml.etree.elementtree.html """
async def getCurrencyDictionary():
response = requests.get("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml").text
root = ET.fromstring(response)
curr={}
for child in root[2][0]:
curr[child.attrib["currency"]] = float(child.attrib["rate"])
curr["EUR"] = 1
curr["XDR"] = 1/(0.423 + 12.1*(1/curr["JPY"]) + 0.111*(1/curr["GBP"]) + 0.66*(1/curr["USD"]))
return curr


async def convertCurrency(to, base, amount):

if amount <= 0:
return amount

currencies = await getCurrencyDictionary()
return amount*currencies[to]/currencies[base]

0 comments on commit f108f94

Please sign in to comment.