-
Notifications
You must be signed in to change notification settings - Fork 12
Chatbot4 #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inanfatih
wants to merge
5
commits into
hackyourfuturecanada:master
Choose a base branch
from
inanfatih:chatbot4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chatbot4 #26
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| let dogEndpoint = 'https://dog.ceo/api/breeds/image/random'; | ||
| let history = []; | ||
| let h_input = document.getElementById('input'); | ||
| let h_output = document.getElementById('output'); | ||
|
|
||
| //object containing inputs and outputs arrays | ||
| let chatInputsOutputs = [ | ||
| { | ||
| inputs: ['Hello', 'Hi', 'Greetings'], | ||
| outputs: ['Hello', 'Hey', 'Greetings'], | ||
| }, | ||
| { | ||
| inputs: [ | ||
| 'What is your favourite colour?', | ||
| 'Who is your favourite HYF instructor?', | ||
| 'Who is your role model?', | ||
| ], | ||
| outputs: [ | ||
| 'I am not sure.', | ||
| 'There are too many to choose from.', | ||
| 'I like everyone.', | ||
| ], | ||
| }, | ||
| { | ||
| inputs: [ | ||
| 'How are you?', | ||
| 'How is the weather today?', | ||
| 'How is Canada doing in the Olympics?', | ||
| ], | ||
| outputs: ['Fine', 'Great', 'Not so good'], | ||
| }, | ||
| ]; | ||
|
|
||
| const randomNumberGenerator = () => Math.floor(Math.random() * 3); | ||
|
|
||
| console.log(chatInputsOutputs); | ||
|
|
||
| //answer based on the option selected in the radio button | ||
| const answerRandom = item => { | ||
| return item[0].outputs[randomNumberGenerator()]; | ||
| }; | ||
|
|
||
| const answerShortest = item => { | ||
| return item[0].outputs.reduce((a, b) => (a.length < b.length ? a : b)); | ||
| }; | ||
|
|
||
| const answerLongest = item => { | ||
| return item[0].outputs.reduce((a, b) => (a.length > b.length ? a : b)); | ||
| }; | ||
|
|
||
| // reply function | ||
| const reply = selectedAnswer => { | ||
| let question = h_input.value; | ||
| console.log(question); | ||
|
|
||
| history.push('You: ' + question + '\n'); | ||
|
|
||
| console.log(history); | ||
|
|
||
| const filteredObject = chatInputsOutputs.filter(item => { | ||
| return item.inputs | ||
| .map(item => item.toLowerCase()) | ||
| .includes(question.toLowerCase()); | ||
| }); | ||
|
|
||
| if (question.toLowerCase().includes('dog')) { | ||
| console.log('if dog question'); | ||
| getPhotos(); | ||
| } else if (question.toLowerCase().includes('alarm')) { | ||
| setAlarm(); | ||
| } else if (filteredObject.length === 1) { | ||
| if (selectedAnswer === 1) { | ||
| history.push('Computer: ' + answerLongest(filteredObject) + '\n\n'); | ||
| } else if (selectedAnswer === 2) { | ||
| history.push('Computer: ' + answerShortest(filteredObject) + '\n\n'); | ||
| } else { | ||
| history.push('Computer: ' + answerRandom(filteredObject) + '\n\n'); | ||
| } | ||
| } else { | ||
| //when the input is not recognized | ||
| history.push( | ||
| "Computer: I don't understand that command. Please enter another. \n\n", | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| function setAlarm() { | ||
| setTimeout(function() { | ||
| history.push( | ||
| 'Computer: Did you forget about me? It’s your friend, the Alarm! \n\n', | ||
| ); | ||
| outputPrinter(history); | ||
| alert('Did you forget about me? It’s your friend, the Alarm!'); | ||
| }, 2000); | ||
| } | ||
|
|
||
| // Event listener of submit button | ||
| document.getElementById('submit').addEventListener('click', function() { | ||
| let option = 0; | ||
| if (document.getElementById('longest').checked) { | ||
| option = 1; | ||
| } else if (document.getElementById('shortest').checked) { | ||
| option = 2; | ||
| } | ||
| reply(option); | ||
| outputPrinter(history); | ||
| }); | ||
|
|
||
| function getPhotos() { | ||
| let xhr = new XMLHttpRequest(); | ||
| let ERROR_MESSAGE = 'Something bad happened!'; | ||
| let data = ''; | ||
| let message = ''; | ||
| console.log('/////////', 'getPhotos'); | ||
|
|
||
| xhr.onreadystatechange = function() { | ||
| console.log('aaaaaa', 'onreadystate'); | ||
|
|
||
| if (xhr.readyState == XMLHttpRequest.DONE) { | ||
| console.log('/////////', 'DONE'); | ||
|
|
||
| data = JSON.parse(xhr.response); | ||
| console.log(data); | ||
| message = data.message; | ||
| console.log('history: ', history); | ||
| console.log('message: ', message); | ||
| history.push(message); | ||
| outputPrinter(history); | ||
| } else { | ||
| message = ERROR_MESSAGE; | ||
| } | ||
| }; | ||
|
|
||
| xhr.open('GET', dogEndpoint, true); | ||
| xhr.send(); | ||
|
|
||
| return message; | ||
| } | ||
|
|
||
| const outputPrinter = history => { | ||
| let outputToPrint = ''; | ||
| let historyWithLink = history.map(item => { | ||
| console.log('item: --- ', item); | ||
| if (item.substring(0, 6) === 'https:') { | ||
| let linkA = `<img src= '${item}' width="500" height="600"> | ||
| <br></br>`; | ||
| document.getElementById('dog').innerHTML = linkA; | ||
| return `Computer: How is this dog? \n\n `; | ||
| } | ||
| return item; | ||
| }); | ||
| historyWithLink.forEach(item => (outputToPrint += item)); | ||
|
|
||
| h_output.innerHTML = outputToPrint; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| button { | ||
| font-family: Helvetica; | ||
| font-size: 10pt; | ||
| /*width: 92px;*/ | ||
| } | ||
| textarea { | ||
| font-family: arial; | ||
| font-size: 10pt; | ||
| } | ||
| body { | ||
| color: #333; | ||
| background-color: #efefef; | ||
| font: 13px helvetica, arial, freesans, clean, sans-serif; | ||
| } | ||
| #demo { | ||
| width: 80%; | ||
| max-width: 1000px; | ||
| margin-left: auto; | ||
| margin-right: auto; | ||
| padding: 20px; | ||
|
|
||
| background-color: #f8f8f8; | ||
| border: 1px solid #ccc; | ||
| box-shadow: 0 0 10px #999; | ||
| line-height: 1.4em; | ||
| font: 13px helvetica, arial, freesans, clean, sans-serif; | ||
| color: black; | ||
| } | ||
| #demo #input { | ||
| padding: 8px; | ||
| font-size: 14px; | ||
| border: 1px solid #ddd; | ||
| width: 400px; | ||
| } | ||
| #demo textarea { | ||
| padding: 8px; | ||
| font-size: 14px; | ||
| border: 1px solid #ddd; | ||
| width: 800px; | ||
| } | ||
|
|
||
| input:focus { | ||
| outline: none; | ||
| } | ||
| textarea:focus { | ||
| outline: none; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't leave comments in your code