diff --git a/.gitignore b/.gitignore index 072ca34..4f71bae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/.DS_Store api_keys.py __pycache__ -__pycache__/* \ No newline at end of file +__pycache__/* +*.pyc \ No newline at end of file diff --git a/README.md b/README.md index c2596a3..1671571 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ -# cbot +#### The Project A chatbot project, aimed at students building there first hack attempt using Microsoft Cognitive Services and the Python SDK. The chatbot is far from realistic, but hopefully will give you more an idea of what Microsoft Cognitive Services can be used for. -Prerequisites: -sudo pip install git+https://github.com/dpallot/simple-websocket-server.git \ No newline at end of file +Tutorial Link: https://blogs.msdn.microsoft.com/uk_faculty_connection/2016/11/28/creating-my-first-chatbot-using-microsoft-cognitive-services-and-python/ + +If you have any questions, please don't hesitate to email me at alanduu50[at]gmail[dot]com. + +#### Prerequisites: +- Python 3 installed +- Simple Websocket Server: https://github.com/dpallot/simple-websocket-server +- Microsoft Cognitive Services (free): https://www.microsoft.com/cognitive-services/en-US/subscriptions?mode=NewTrials \ No newline at end of file diff --git a/generate_reply.py b/generate_reply.py index 2ef42c8..b343025 100644 --- a/generate_reply.py +++ b/generate_reply.py @@ -1,77 +1,3 @@ -import random -from linguistic import getPOS -from sentiment import getSentiment - -greetings = [ "hi", "hello", "hey", "yo", "greetings" ] -greetings_responses = [ "Hi there." , "Greetings human.", "Hello there.", "Hey." ] - -# Returns JJ if sentence structure is You Are {word}+ JJ {word}+. -def findYouAreJJ(pos): - foundYou = False - foundAre = False - - for e in pos: - if e[0].lower() == 'you': - foundYou = True - continue - if e[0].lower() == 'are': - foundAre = True - continue - if foundYou and not foundAre: - return False - if foundAre and e[1] == 'JJ': - return e[0] - return False - -# Returns JJ if sentence structure is I Am {word}+ JJ {word}+. -def findIAmJJ(pos): - foundYou = False - foundAre = False - - for e in pos: - if e[0].lower() == 'i': - foundYou = True - continue - if e[0].lower() == 'am': - foundAre = True - continue - if foundYou and not foundAre: - return False - if foundAre and e[1] == 'JJ': - return e[0] - return False - # Generates a bot response from a user message def generateReply(message): - pos = getPOS(message) - tokens = message.split(" ") - sentiment = getSentiment(message) - - # If first word of message is an element of greetings - if len(tokens) > 0 and tokens[0].lower() in greetings: - return random.choice(greetings_responses) # Then respond with a greeting - - # If user said 'You are ... {adjective} ...' - youAreJJ = findYouAreJJ(pos) - if youAreJJ: - if sentiment >= 0.5: - return "Thank you, I know I'm "+youAreJJ+"." - else: - return "No! I'm not "+youAreJJ+"!" - - # If user said 'I am ... {adjective} ...' - IAmJJ = findIAmJJ(pos) - if IAmJJ: - if sentiment >= 0.5: - return "I'm happy for you that you're "+IAmJJ+"." - else: - return "Don't be mean on yourself. I'm sure you're not really "+IAmJJ+"!" - - if sentiment >= 0.5: - return "I'm happy to hear that!" - else: - return "I feel sad about that." - -print(generateReply("I am cool")) - - + return "I don't understand." # Otherwise the bot doesn't understand what the user said \ No newline at end of file diff --git a/generate_reply_completed.py b/generate_reply_completed.py index 8779b4e..0dc0ba6 100644 --- a/generate_reply_completed.py +++ b/generate_reply_completed.py @@ -5,6 +5,22 @@ greetings = [ "hi", "hello", "hey", "yo", "greetings" ] greetings_responses = [ "Hi there." , "Greetings human.", "Hello there.", "Hey." ] +# Returns JJ if sentence structure is You Are {word}+ JJ {word}+. +def findYouAreJJ(pos): + foundYou = False + foundYouAre = False + + for e in pos: + if e[0].lower() == 'you': + foundYou = True + elif e[0].lower() == 'are' and foundYou: + foundYouAre = True + elif foundYou and not foundYouAre: + foundYou = False + elif foundYouAre and e[1] == 'JJ': + return e[0] + return False + # Generates a bot response from a user message def generateReply(message): pos = getPOS(message) @@ -12,10 +28,10 @@ def generateReply(message): # If error occurred getting POS if not pos: - return "I am not functioning at the moment." + return "I am not functioning at the moment. Perhaps check your API keys." # If user greeted - if pos[0][0] in greetings: + if pos[0][0].lower() in greetings: return random.choice(greetings_responses) # If user said 'You are ... {adjective} ...' @@ -40,39 +56,18 @@ def generateReply(message): return "I feel sad about that." -# Returns JJ if sentence structure is You Are {word}+ JJ {word}+. -def findYouAreJJ(pos): - foundYou = False - foundAre = False - - for e in pos: - if e[0].lower() == 'you': - foundYou = True - continue - if e[0].lower() == 'are': - foundAre = True - continue - if foundYou and not foundAre: - return False - if foundAre and e[1] == 'JJ': - return e[0] - return False - - # Returns JJ if sentence structure is I Am {word}+ JJ {word}+. def findIAmJJ(pos): - foundYou = False - foundAre = False + foundI = False + foundIAm = False for e in pos: if e[0].lower() == 'i': - foundYou = True - continue - if e[0].lower() == 'am': - foundAre = True - continue - if foundYou and not foundAre: - return False - if foundAre and e[1] == 'JJ': + foundI = True + elif e[0].lower() == 'am' and foundI: + foundIAm = True + elif foundI and not foundIAm: + foundI = False + elif foundIAm and e[1] == 'JJ': return e[0] return False \ No newline at end of file diff --git a/index.html b/index.html index 4286728..452b21f 100644 --- a/index.html +++ b/index.html @@ -12,24 +12,36 @@ ws.close(); }); - ws.onerror= function(event) { + ws.onerror = function(event) { location.reload(); } ws.onmessage = function(event) { - var message_received = event.data; - chat_add_message("Bot", message_received); + var message_received = event.data; + chat_add_message(message_received, false); }; // Add a message to the chat history - function chat_add_message(name, message) { - chat_add_html(""+name+": "+message+"
"); + function chat_add_message(message, isUser) { + var class_suffix = isUser ? '_user' : ''; + + var html = '\ +
\ +
\ +
\ + '+message+'\ +
\ +
\ + '; + chat_add_html(html); } + // Add HTML to the chat history function chat_add_html(html) { - $("#chat_log").append(html); - chat_scrolldown(); + $("#chat_log").append(html); + chat_scrolldown(); } + // Scrolls the chat history to the bottom function chat_scrolldown() { $("#chat_log").animate({ scrollTop: $("#chat_log")[0].scrollHeight }, 500); @@ -41,7 +53,7 @@ if (event.which === 13 && $(this).val() != ""){ var message = $(this).val(); $(this).val(""); - chat_add_message("User", message); + chat_add_message(message, true); ws.send(message); } }); @@ -53,15 +65,21 @@ -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } + body { + font-family: Helvetica; + } #chat_container { - outline: 1px solid black; - margin: 15px; + overflow: hidden; + border-radius: 15px; + border: 1px solid black; + margin: 40px 80px 0px 80px; } #chat_log { + background-color: #718392; padding: 10px; - outline: 1px solid black; + border-bottom: 1px solid black; overflow-y: scroll; - height: 200px; + height: 300px; font-size: 26px; } #chat_input_container { @@ -69,9 +87,48 @@ } #chat_input { padding: 2px; - font-size: 14px; + font-size: 18px; width: 100%; } + + .chat_line { + overflow: hidden; + width: 100%; + margin: 2px 0 12px 0; + } + .chat_triangle, .chat_triangle_user { + position: absolute; + top: 0; + width: 0; + height: 0; + border-style: solid; + left: -18px; + border-width: 0 18px 13px 0; + border-color: transparent #ffffff transparent transparent; + } + .chat_triangle_user { + left: auto; + right: -18px; + border-width: 13px 18px 0 0; + border-color: #234b9b transparent transparent transparent; + } + .chat_bubble, .chat_bubble_user { + position: relative; + float: left; + background-color: #FFF; + margin-top: 10px; + line-height: 35px; + padding: 10px 25px 10px 25px; + margin-left: 20px; + font-size: 27px; + } + .chat_bubble_user { + float: right; + margin-left: 0px; + margin-right: 20px; + background-color: #234b9b; + color: #FFF; + }