Advertisement
Guest User

hhh

a guest
Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package chatbot;
  7.  
  8. import java.time.LocalDate;
  9. import java.time.Month;
  10. import java.time.Period;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13.  
  14. /**
  15. *
  16. * @author joelewis
  17. */
  18. public class BotWeek7_orig {
  19.  
  20. final String name = "Clarissa";
  21. final String country = "England";
  22. final String feeling = "fine";
  23. final LocalDate dateOfBirth = LocalDate.of(1994, Month.OCTOBER, 9);
  24.  
  25. private ArrayList<Like> myLikes;
  26.  
  27. String[] likes = {"wine", "music", "books", "sailing"};
  28. int[] likesHowMuch = {2, 3, 3, 4};
  29. String[] myDislikes = {"rap music", "Donald Trump", "tofu", "football"};
  30. int[] dislikesHowMuch = {3, 4, 3, 2};
  31. private final String[] likeWords = {"I quite like ", "I like ", "I really like ", "I love "};
  32. private final String[] dislikeWords= {"I'm not too keen on ", "I don't like ", "I can't stand ", "I loathe and abhor "};
  33. //create a no-args constructor
  34. public BotWeek7_orig() {
  35. //do you understand what this is doing?
  36. initLikes();
  37. }
  38.  
  39. //Encapsulate the instance variables
  40. //create getter methods only (Why?) for name, country, age (you will have to calculate-see lecture).
  41. public String getName() {
  42. return name;
  43. }
  44.  
  45. public String getCountry() {
  46. return country;
  47. }
  48.  
  49. public String getFeeling() {
  50. return feeling;
  51. }
  52.  
  53. public int getAge() {
  54. LocalDate today = LocalDate.now();
  55. Period age = Period.between(dateOfBirth, today);
  56. int year = age.getYears();
  57. return year;
  58. }
  59.  
  60. public void initLikes() {
  61.  
  62. myLikes = new ArrayList<>();
  63. myLikes.add(new Like("food", "chicken", 5));
  64. myLikes.add(new Like("food", "beef", 4));
  65. myLikes.add(new Like("food", "stinky tofu", -5));
  66. myLikes.add(new Like("food", "salmon", 3));
  67. myLikes.add(new Like("food", "ramen", 6));
  68.  
  69. myLikes.add(new Like("Artist", "Justin Bieber", 6));
  70. myLikes.add(new Like("Artist", "Maroon 5", 5));
  71. myLikes.add(new Like("Artist", "Taylor Swift", 4));
  72. myLikes.add(new Like("Artist", "Lauv", 3));
  73. myLikes.add(new Like("Artist", "Lady Gaga", -3));
  74. }
  75.  
  76. //create method to process questions
  77.  
  78. public String processQuestion(String question) {
  79. String[] questionArray = question.split(" ");
  80. question = question.toLowerCase();
  81. String response = "I'm not sure";
  82.  
  83. if (question.contains("what") && (question.contains("name")) && (question.contains("your"))) {
  84. response = this.getName();
  85. } else if (question.contains("where") && (question.contains("from")) && (question.contains("you"))) {
  86. response = this.getCountry();
  87. } else if (question.contains("how") && (question.contains("feeling")) && (question.contains("you"))) {
  88.  
  89. response = this.getFeeling();
  90. } else if (question.contains("what") && (question.contains("do")) && (question.contains("you")) && (question.contains("like"))){
  91. int i;
  92. for (i=0; !questionArray[i].equalsIgnoreCase("like"); i++) {
  93. }
  94. response = this.whatdoyoulike(questionArray[i+1]);
  95. } else if(question.contains("do you like")){
  96. String[] words = question.split(" ");
  97. //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'
  98. //this makes it a little complicated, but follow the code through
  99. int indexFrom = 0;
  100. for(int i = 0; i<words.length; i++){
  101. if(words[i].equals("like")){ //find the first occurence of 'like'
  102. indexFrom = i;
  103. break; //we almost never use break, but here...
  104. }
  105. }
  106. StringBuilder sb = new StringBuilder();
  107. for(int i = indexFrom+1; i<words.length; i++){
  108. sb.append(words[i]);
  109. sb.append(" ");
  110. }
  111. response = checkLikes(sb.toString());
  112. }
  113.  
  114.  
  115. return response;
  116. }
  117.  
  118. private String checkLikes(String toCheck){
  119. String response = "I have no preference";
  120. toCheck = toCheck.trim(); //we added a space on the end!!
  121. System.out.println("Checking likes for: ["+toCheck+"]");
  122. for(int i=0; i<likes.length; i++){
  123. if(likes[i].equalsIgnoreCase(toCheck)){ //how to deal with plurals etc (but do you like dog/dogs not same!)
  124. response = likeWords[likesHowMuch[i]-1]+toCheck;
  125. //return response;
  126. }
  127. }
  128. for(int i=0; i<myDislikes.length; i++){
  129. if(myDislikes[i].equalsIgnoreCase(toCheck)){ //how to deal with plurals etc (but do you like dog/dogs not same!)
  130. response = dislikeWords[dislikesHowMuch[i]-1]+toCheck;
  131. //return response;
  132. }
  133. }
  134. return response;
  135. }
  136.  
  137.  
  138. public String whatdoyoulike(String keyword){
  139. ArrayList<Like> temp = new ArrayList<>();
  140.  
  141. for (int i= 0;i<myLikes.size();i++){
  142. if(myLikes.get(i).getKeyWord().equals(keyword)){
  143. temp.add(myLikes.get(i));
  144. }
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151. Collections.sort(temp);
  152. System.out.println(temp.size());
  153. Like outLike = temp.get(temp.size()-1);
  154. return outLike.getOnWhat();
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement