I am making a chatbot and I want it to check if any of the words you type match words in a predefined list.
For example: I type I'm good thanks!
And I have a list with awesome
, perfect
, happy
and good
.
I want the program to check if any of the words I'm
, good
, or thanks!
is in the list.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import use.*;
public class welcome {
public static void say() {
}
public static void main(String[] args) {
wait.clean();
String input1 = "";
String name = "Antonio";
String surname = "Solana";
Scanner scanner1 = new Scanner(System.in);
String[] goodadj = {"good", "well", "awesome", "perfect", "adaptable", "adventurous", "affable", "affectionate", "agreeable", "ambitious", "amiable", "amicable", "amusing", "brave", "bright", "broad-minded", "calm", "careful", "charming", "communicative", "compassionate", "conscientious", "considerate", "convivial", "courageous", "courteous", "creative", "decisive", "determined", "diligent", "diplomatic", "discreet", "dynamic", "easygoing", "emotional", "energetic", "enthusiastic", "exuberant", "fair-minded", "faithful", "fearless", "forceful", "frank", "friendly", "funny", "generous", "gentle", "good", "gregarious", "hard-working", "happy", "helpful", "honest", "humorous", "imaginative", "impartial", "independent", "intellectual", "intelligent", "intuitive", "inventive", "kind", "loving", "loyal", "modest", "neat", "nice", "optimistic", "passionate", "patient", "persistent", "pioneering", "philosophical", "placid", "plucky", "polite", "powerful", "practical", "pro-active", "quick-witted", "quiet", "rational", "reliable", "reserved", "resourceful", "romantic", "self-confident", "self-disciplined", "sensible", "sensitive", "shy", "sincere", "sociable", "straightforward", "sympathetic", "thoughtful", "tidy", "tough", "unassuming", "understanding", "versatile", "warmhearted", "willing", "witty"};
String[] badadj = {"awful", "sad", "bad", "aggressive", "aloof", "arrogant", "belligerent", "big-headed", "bitchy", "boastful", "bone-idle", "boring", "bossy", "callous", "cantankerous", "careless", "changeable", "clinging", "compulsive", "conservative", "cowardly", "cruel", "cunning", "cynical", "deceitful", "detached", "dishonest", "dogmatic", "domineering", "finicky", "flirtatious", "foolish", "foolhardy", "fussy", "greedy", "grumpy", "gullible", "harsh", "impatient", "impolite", "impulsive", "inconsiderate", "inconsistent", "indecisive", "indiscreet", "inflexible", "interfering", "intolerant", "irresponsible", "jealous", "lazy", "Machiavellian", "materialistic", "mean", "miserly", "moody", "narrow-minded", "nasty", "naughty", "nervous", "obsessive", "obstinate", "overcritical", "overemotional", "parsimonious", "patronizing", "perverse", "pessimistic", "pompous", "possessive", "pusillanimous", "quarrelsome", "quick-tempered", "resentful", "rude", "ruthless", "sarcastic", "secretive", "selfish", "self-centred", "self-indulgent", "silly","sneaky", "stingy", "stubborn", "stupid", "superficial", "tactless", "timid", "touchy", "thoughtless", "truculent", "unkind", "unpredictable", "unreliable", "untidy", "untrustworthy", "vague", "vain", "vengeful", "vulgar", "weak-willed"};
String[] afirmation = {"ok", "okay", "yes", "well", "affirmative", "amen", "fine", "good", "okay", "true", "yeah", "all right", "aye", "beyond a doubt", "by all means", "certainly", "definitely", "even so", "exactly", "gladly", "good enough", "granted", "indubitably", "just so", "most assuredly", "naturally", "of course", "positively", "precisely", "sure thing", "surely", "undoubtedly", "unquestionably ", "very well", "willingly", "without fail", "yep star"};
String[] negation = {"no", "nope", "never", "nix", "absolutely not", "not at all", "no way", "by no means", "not by any means"};
Random rand = new Random();
int x = rand.nextInt((6 - 1) + 1) + 1;
String hi = "";
if (x == 1) {
hi = "Hey! Sup ";
}
if (x == 2) {
hi = "Nice to see you again ";
}
if (x == 3) {
hi = "You smell good today ";
}
if (x == 4) {
hi = "Kon'nichiwa ";
}
if (x == 5) {
hi = "I didn't heard you comming ";
}
if (x == 6) {
hi = "Wasssaaap ";
}
x = rand.nextInt((6 - 1) + 1) + 1;
String question1 = "";
if (x == 1) {
question1 = "How are you today boy?";
}
if (x == 2) {
question1 = "U good?";
}
if (x == 3) {
question1 = "You look good don't you? (I can't see you... sorry...)";
}
if (x == 4) {
question1 = "How are you feeling?";
}
if (x == 5) {
question1 = "How are you this beautiful day?";
}
if (x == 6) {
question1 = "Was your day good?";
}
x = rand.nextInt((6 - 1) + 1) + 1;
String joke = "";
if (x == 1) {
joke = "What's the difference between snowmen and snowladies? Snowballs.";
}
if (x == 2) {
joke = "I am a nobody, nobody is perfect, therefore I am perfect.";
}
if (x == 3) {
joke = "Siri. You should laugh, she is a joke.";
}
if (x == 4) {
joke = "Do you know the dentist who never agrees with the other nine? I think he is sleeping or something.";
}
if (x == 5) {
joke = "Why do farts smell? So deaf people can enjoy them too.";
}
if (x == 6) {
joke = "I asked God for a bike, but I know God doesn't work that way. So I stole a bike and asked for forgiveness.";
}
x = rand.nextInt((6 - 1) + 1) + 1;
String happy = "";
if (x == 1) {
happy = "What's the difference between snowmen and snowladies? Snowballs.";
}
if (x == 2) {
happy = "I am a nobody, nobody is perfect, therefore I am perfect.";
}
if (x == 3) {
happy = "Siri. You should laugh, she is a joke.";
}
if (x == 4) {
happy = "Do you know the dentist who never agrees with the other nine? I think he is sleeping or something.";
}
if (x == 5) {
happy = "Why do farts smell? So deaf people can enjoy them too.";
}
if (x == 6) {
happy = "I asked God for a bike, but I know God doesn't work that way. So I stole a bike and asked for forgiveness.";
}
x = rand.nextInt((6 - 1) + 1) + 1;
String sad = "";
if (x == 1) {
sad = "What's the difference between snowmen and snowladies? Snowballs.";
}
if (x == 2) {
sad = "I am a nobody, nobody is perfect, therefore I am perfect.";
}
if (x == 3) {
sad = "Siri. You should laugh, she is a joke.";
}
if (x == 4) {
sad = "Do you know the dentist who never agrees with the other nine? I think he is sleeping or something.";
}
if (x == 5) {
sad = "Why do farts smell? So deaf people can enjoy them too.";
}
if (x == 6) {
sad = "I asked God for a bike, but I know God doesn't work that way. So I stole a bike and asked for forgiveness.";
}
System.out.println(hi + name);
wait.one();
System.out.println(question1);
input1 = scanner1.next();
input1 = input1.toLowerCase();
String[] input2 = input1.split(" ");
if (Arrays.asList(goodadj).contains(input2) || Arrays.asList(afirmation).contains(input2)) {
System.out.println("Good for you!");
wait.one();
System.out.println("Do you want to tell me about it?");
input1 = scanner1.next();
input1 = input1.toLowerCase();
input2 = input1.split(" ");
if (Arrays.asList(goodadj).contains(input2) || Arrays.asList(afirmation).contains(input2)) {
input1 = scanner1.next();
System.out.println(happy);
}
if (Arrays.asList(goodadj).contains(input2) || Arrays.asList(afirmation).contains(input2)) {
input1 = scanner1.next();
System.out.println(sad);
}
}
if (Arrays.asList(badadj).contains(input2) || Arrays.asList(negation).contains(input2)) {
System.out.println("I'm sorry, I could help you if you want...");
input1 = scanner1.next();
input1 = input1.toLowerCase();
if (Arrays.asList(afirmation).contains(input2)) {
System.out.println(joke);
}
if (Arrays.asList(negation).contains(input2)) {
System.out.println("I understand");
}
}
else {
System.out.println("I don't really understand that...");
}
}
}
That is what I tried. goodadj
is the list where I want to search and good
is the input word within that list.
I'm allowed to use whatever I want, this project is just for fun. Thanks to everyone who is helping.
答案 0 :(得分:1)
You should split the input by delimiters like space, ',', '.', ... All the words in the arrays should be moved to a Set (best would be HashSet) and check with
if (wordSet.contains(wordToCheck)) { doSomething(); }
You may convert all input words and words in the sets to lower or upper case to match "good", "Good", "GOOD", ...
答案 1 :(得分:1)
Store input values into an array using .split(" ")
(item: apple, quantity: 5, Employee:me)
cashRegister.scan('apple',5);
cashRegister.applyStaffDiscount(me);
Which gives you something like this ["I,m", "good", "thanks!"]
For each value of inputArray, loop through those and compare it to each of your base values.
or
Take the input of the user that is entered, loop through the values in your array and see if the string they gave you .contains() any of them.
**Disclaimer, these are inefficient and using a Hashset would be the optimal way, these responses are specific to an array.
答案 2 :(得分:0)
/** Will return a set of words which are in the array and the sentence. */
public Set<String> checkWords( String sentence, String[] words )
{
Set<String> result = new HashSet<>()
String[] wordsInSentence = sentence.split(" ");
for( String word : wordsInSentence )
{
for( String word2 : words )
{
if ( word.equals(word2) )
{
result.add( word );
break;
}
}
}
}
It would be better if you used a Set instead of an array though. It better conveys the meaning and will be more efficient with a HashSet.
/** Will return a set of words which are in the set and the sentence. */
public Set<String> checkWords( String sentence, Set<String> words )
{
Set<String> result = new HashSet<>()
String[] wordsInSentence = sentence.split(" ");
for( String word : wordsInSentence )
{
if ( words.contains(word) )
result.add( word );
}
}