Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package chatbot;
- import java.time.LocalDate;
- import java.time.Month;
- import java.time.Period;
- import java.util.ArrayList;
- import java.util.Collections;
- /**
- *
- * @author joelewis
- */
- public class BotWeek7_orig {
- final String name = "Clarissa";
- final String country = "England";
- final String feeling = "fine";
- final LocalDate dateOfBirth = LocalDate.of(1994, Month.OCTOBER, 9);
- private ArrayList<Like> myLikes;
- String[] likes = {"wine", "music", "books", "sailing"};
- int[] likesHowMuch = {2, 3, 3, 4};
- String[] myDislikes = {"rap music", "Donald Trump", "tofu", "football"};
- int[] dislikesHowMuch = {3, 4, 3, 2};
- private final String[] likeWords = {"I quite like ", "I like ", "I really like ", "I love "};
- private final String[] dislikeWords= {"I'm not too keen on ", "I don't like ", "I can't stand ", "I loathe and abhor "};
- //create a no-args constructor
- public BotWeek7_orig() {
- //do you understand what this is doing?
- initLikes();
- }
- //Encapsulate the instance variables
- //create getter methods only (Why?) for name, country, age (you will have to calculate-see lecture).
- public String getName() {
- return name;
- }
- public String getCountry() {
- return country;
- }
- public String getFeeling() {
- return feeling;
- }
- public int getAge() {
- LocalDate today = LocalDate.now();
- Period age = Period.between(dateOfBirth, today);
- int year = age.getYears();
- return year;
- }
- public void initLikes() {
- myLikes = new ArrayList<>();
- myLikes.add(new Like("food", "chicken", 5));
- myLikes.add(new Like("food", "beef", 4));
- myLikes.add(new Like("food", "stinky tofu", -5));
- myLikes.add(new Like("food", "salmon", 3));
- myLikes.add(new Like("food", "ramen", 6));
- myLikes.add(new Like("Artist", "Justin Bieber", 6));
- myLikes.add(new Like("Artist", "Maroon 5", 5));
- myLikes.add(new Like("Artist", "Taylor Swift", 4));
- myLikes.add(new Like("Artist", "Lauv", 3));
- myLikes.add(new Like("Artist", "Lady Gaga", -3));
- }
- //create method to process questions
- public String processQuestion(String question) {
- String[] questionArray = question.split(" ");
- question = question.toLowerCase();
- String response = "I'm not sure";
- if (question.contains("what") && (question.contains("name")) && (question.contains("your"))) {
- response = this.getName();
- } else if (question.contains("where") && (question.contains("from")) && (question.contains("you"))) {
- response = this.getCountry();
- } else if (question.contains("how") && (question.contains("feeling")) && (question.contains("you"))) {
- response = this.getFeeling();
- } else if (question.contains("what") && (question.contains("do")) && (question.contains("you")) && (question.contains("like"))){
- int i;
- for (i=0; !questionArray[i].equalsIgnoreCase("like"); i++) {
- }
- response = this.whatdoyoulike(questionArray[i+1]);
- } else if(question.contains("do you like")){
- String[] words = question.split(" ");
- //now, the thing liked might be any number of words long - we have to deal with 'rap music', 'people who take selfies all the time'
- //this makes it a little complicated, but follow the code through
- int indexFrom = 0;
- for(int i = 0; i<words.length; i++){
- if(words[i].equals("like")){ //find the first occurence of 'like'
- indexFrom = i;
- break; //we almost never use break, but here...
- }
- }
- StringBuilder sb = new StringBuilder();
- for(int i = indexFrom+1; i<words.length; i++){
- sb.append(words[i]);
- sb.append(" ");
- }
- response = checkLikes(sb.toString());
- }
- return response;
- }
- private String checkLikes(String toCheck){
- String response = "I have no preference";
- toCheck = toCheck.trim(); //we added a space on the end!!
- System.out.println("Checking likes for: ["+toCheck+"]");
- for(int i=0; i<likes.length; i++){
- if(likes[i].equalsIgnoreCase(toCheck)){ //how to deal with plurals etc (but do you like dog/dogs not same!)
- response = likeWords[likesHowMuch[i]-1]+toCheck;
- //return response;
- }
- }
- for(int i=0; i<myDislikes.length; i++){
- if(myDislikes[i].equalsIgnoreCase(toCheck)){ //how to deal with plurals etc (but do you like dog/dogs not same!)
- response = dislikeWords[dislikesHowMuch[i]-1]+toCheck;
- //return response;
- }
- }
- return response;
- }
- public String whatdoyoulike(String keyword){
- ArrayList<Like> temp = new ArrayList<>();
- for (int i= 0;i<myLikes.size();i++){
- if(myLikes.get(i).getKeyWord().equals(keyword)){
- temp.add(myLikes.get(i));
- }
- }
- Collections.sort(temp);
- System.out.println(temp.size());
- Like outLike = temp.get(temp.size()-1);
- return outLike.getOnWhat();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement