JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
Input:
<input id="input" type="text" value="" />
<input type="button" value="Submit" onclick="$('#output').append('> ').append($('#input').val()).append('<br/>').append(respond($('#input').val())).append('<br/>')" />
<input id="clear" type="button" value="Clear" onclick="$('#output').empty()" />
<div id="output"></div>
CSS
#output {
margin: 10px 0 0 0;
min-height: 100px;
border: 1px solid #ccc;
}
JavaScript
var brain = [
{ key: "hello",
value: {
neutral: "Hello, please say the name of a book to get a review.",
positive: "Hello, please say the name of a book to get a review.",
negative: "Perhaps, a good book would cheer you up. Please say the name of a book to get a review."
}
},
{ key: "alice in wonderland",
value: {
neutral: "The book Alice in Wonderland has a rating of 4 stars.",
positive: "That's great to hear that you enjoy the book. You might also like \"The Annotated Alice: The Definitive Edition\" by the same author, with a rating of 4.5. Would you like to hear about it?",
negative: "Although this book might be dissapointing, you might enjoy \"The House at Pooh Corner\", by A.A. Milne, with a rating of 4.3. Would you like to hear about it?"
}
},
{ key: "pemberley",
value: {
neutral: "The book Pemberley has a rating of 2 stars.",
positive: "That's great to hear that you enjoy the book. You might also like \"Wild Nights\" by the same author, with a rating of 3.7.",
negative: "Although this book has quite a low rating, you might enjoy \"Mrs Darcy's Dilemma\", by Diana Birchall, with a rating of 3.4. Would you like to hear about it?"
}
},
{ key: "bye",
value: {
neutral: "Goodbye.",
positive: "I'm glad to have helped! Please come back soon. Goodbye.",
negative: "I'm sorry I couldn't be of much help. Please try again, next time. Goodbye."
}
},
{ key: "help",
value: {
neutral: "Please say the name of a book to get a review.",
positive: "Awesome! Please say the name of a book to get a review.",
negative: "Retrieving book reviews can actually be quite easy! Just say the name of a book to get a review."
}
}
];
function getSentiment(input) {
var sentiment = 'neutral';
var positives = ["love", "happy", "great", "enjoy"];
var negatives = ["hate", "sad", "bad", "dislike"];
// Split sentence into words.
var words = input.split(' ');
...