Skip to content
Permalink
Browse files
Added Prototype, commented and refactored word checker
  • Loading branch information
marshj10 committed Nov 26, 2021
1 parent a5800d5 commit 2d2e42c9a844e326f7dc6bc6cd985a98d353b144
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 15 deletions.
@@ -70,7 +70,6 @@ def SelectCell(filename, chosenCol, sortVal):
cursor = connection.cursor()
filedata = cursor.execute("SELECT " + chosenCol + " FROM Ammo WHERE Name = ('" + sortVal + "')")
data = filedata.fetchall()[0][0]


#closes the connection to the file
connection.commit()
@@ -192,8 +191,8 @@ for i in range(len(ammoTypes) - 1):
#AddRow(filename, rowName, rowData)

#debug - displays all the data in the database
#DisplayDatabase(filename)
DisplayDatabase(filename)
#debug - displays the damage for a given ammo name
print(SelectCell(filename, "Damage", "12/70 RIP"))
#debug - displays all the data in the Name column and orders it by the corresponding Damage
#SelectColumn(filename, "Damage", "Damage")
SelectColumn(filename, "Damage", "Damage")
@@ -0,0 +1,61 @@
import sqlite3, os

def CreateDatabase (filename):
#checks if file already exists
if not os.path.exists(filename):
#creates connection to the file
connection = sqlite3.connect(filename)

#creates cursor and executes SQL command to create the data table inside the file
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS Items (Name TEXT,
Price INTEGER,
Info TEXT,
Stats TEXT)""")

#closes the connection to the file
connection.commit()
connection.close()

def AddRow(filename, itemName, itemPrice, itemInfo, itemStats):
#connects to the given file
connection = sqlite3.connect(filename)

#creates a cursor and executes SQL command to insert the given data into the table as a new row
cursor = connection.cursor()
cursor.execute("INSERT INTO Items VALUES(?, ?, ?, ?)", (itemName, itemPrice, itemInfo, itemStats))

#closes the connection to the file
connection.commit()
connection.close()

def DisplayDatabase (filename):
#connects to the given file
connection = sqlite3.connect(filename)

#creates a cursor and executes SQL command and select all of the data inside the table
cursor = connection.cursor()
filedata = cursor.execute("SELECT * FROM Items ORDER BY Name")
content = filedata.fetchall()

#loops through and prints each row
for i in range(len(content)):
print(content[i])

#closes the connection to the file
connection.commit()
connection.close()

filename = "items.db"

CreateDatabase(filename)
AddRow(filename, "AK101", 50000, "ak info", "ak stats")
AddRow(filename, ".338 Lapua", 500000, "lapua info", "lapua stats")
AddRow(filename, "M4A1", 45000, "m4 info", "m4 stats")
AddRow(filename, "MP7", 60000, "mp7 info", "mp7 stats")
AddRow(filename, "SVDS", 120000, "svd info", "svd stats")
AddRow(filename, "P90", 145000, "p90 info", "p90 stats")
AddRow(filename, "MK47 Mutant", 85000, "mk47 info", "mk47 stats")
AddRow(filename, "AR-15", 100000, "ar-15 info", "ar-15 stats")
AddRow(filename, "SR-25", 70000, "sr-25 info", "sr-25 stats")
DisplayDatabase(filename)
@@ -0,0 +1,137 @@
import sqlite3

#greets the user
def Greeting(botname):
print(botname + "Hey there, what would you like to be known as?")
username = input("User: ")
print(botname + "Welcome " + username + " nice to meet you, what can i help with?")

return username

#checks for keywords in given message
def WordChecker(userMessage):
#different lists of keywords
commands = ["price", "info", "stats"]
polite = ["thank you", "thanks", "cheers"]
farewell = ["goodbye", "bye", "see you later", "adios", "ciao"]

#checks if the message is empty
if userMessage.split() == []:
botResponse = "empty"
else:
#checks for keywords in the message from the command list
for commandWord in commands:
if commandWord in userMessage:
return commandWord
#checks for keywords in the message from the polite list
for politeWord in polite:
if politeWord in userMessage:
return "polite"
#checks for keywords in the message from the farewell list
for farewellWord in farewell:
if farewellWord in userMessage:
return "goodbye"
botResponse = "no word"
return botResponse

#pulls list of details from SQL table
def GetDetailList(filename, keyword):
detailList = []

#connects to the given file
connection = sqlite3.connect(filename)

#creates a cursor and executes SQL command and select all of the data inside the table
cursor = connection.cursor()
filedata = cursor.execute("SELECT " + keyword + " FROM Items ORDER BY " + keyword)
content = filedata.fetchall()

#loops through and prints each row
for i in range(len(content)):
detailList.append(content[i][0])

#closes the connection to the file
connection.commit()
connection.close()

return detailList

#gets specific detail from SQL table
def GetDetail(filename, keyword, targetValue, searchValue):
#connects to the given file
connection = sqlite3.connect(filename)

#creates a cursor and executes SQL command and select all of the data inside the table
cursor = connection.cursor()
filedata = cursor.execute("SELECT " + keyword + " FROM Items WHERE " + targetValue + " = '" + searchValue + "'")
detail = filedata.fetchall()[0][0]

#closes the connection to the file
connection.commit()
connection.close()

return detail

#handles price response
def price(userMessage):
itemList = GetDetailList(filename, "Name")

#checks for a match between the words in the message and the available items in the database
for item in itemList:
if item.lower() in userMessage.lower():
itemPrice = str(GetDetail(filename, "Price", "Name", item))
return ("the price of the " + item + " according to my scav sources is " + itemPrice)
return ("im sorry my friend but i couldnt find the item you were looking for")

#handles info response
def info(userMessage):
itemList = GetDetailList(filename, "Name")

#checks for a match between the words in the message and the available items in the database
for item in itemList:
if item.lower() in userMessage.lower():
itemInfo = GetDetail(filename, "Info", "Name", item)
return ("the information for the " + item + " according to my scav sources is " + itemInfo)
return ("im sorry my friend but i couldnt find the information about what you were looking for")

#handles stats response
def stats(userMessage):
itemList = GetDetailList(filename, "Name")

#checks for a match between the words in the message and the available items in the database
for item in itemList:
if item.lower() in userMessage.lower():
itemStats = GetDetail(filename, "Stats", "Name", item)
return ("the stats for the " + item + " according to my scav sources is " + itemStats)
return ("im sorry my friend but i couldnt find the statistics you were looking for")


#predefines some variables used throughout the program
activeCheck = True
filename = "items.db"
botname = "TarkyBot"
username = Greeting(botname + ": ")

#loop keeps bot active
while activeCheck:
#formats the start of the messages
userMessage = str(input(username + ": ")).lower()
botResponse = botname + ": "

#checks for something to do using the given message
procedure = WordChecker(userMessage)

#decides what to do based on the returned keyword
if procedure == "empty":
botResponse += "speak up i cant hear you"
elif procedure == "polite":
botResponse += "you are very welcome :)"
elif procedure == "goodbye":
botResponse += "Thank you for the chat, hope i was some help to you. See you later " + username
activeCheck = False
elif procedure == "no word":
botResponse += "do nothing"
else:
botResponse += eval(procedure + "(userMessage)")

print(botResponse)
BIN +8 KB Prototype/items.db
Binary file not shown.
@@ -1,26 +1,50 @@
def wordcheck(message):
#old function
def oldwordcheck(message):
#keyword list
procedures = ["price", "info", "stats"]


#checks for a match between each word in the message and the keyword list
for word in procedures:
if word in message:
return word
return "no procedure"

#shiny new checker
def NewWordCheck(message):
#keyword dictionary
keywords = {
"price" : ["price", "value", "cost", "rubles"],
"info" : ["info", "information", "details", "facts"],
"stats" : ["stats", "statistics", "data"]
}

#loops through each word in the message
for searchWord in message.split():
#loops through each key in the dictionary
for key, value in keywords.items() :
#checks for a match between the message word and either the key or the values associated to it
if value.__contains__(searchWord):
return key

return "no procedure"

#temp function for testing purpose
def stats():
print("this is the statistics function")

#temp function for testing purpose
def price():
print("hellooooooo")
print("this is the price function")

#temp function for testing purpose
def info():
print("cheese")

print("this is the information function")

message = "hello friend"
procedure = wordcheck(message)
#temp user message for testing purpose
message = "how many roubles does the ak101 cost"
procedure = NewWordCheck(message)

if procedure == "no procedure":
print("nothing to do")
print("error, no function found/nothing to do")
else:
eval(procedure + "()")


#testing pull request?
eval(procedure + "()")

0 comments on commit 2d2e42c

Please sign in to comment.