Advertisement
CR7CR7

solution_coding_exercise_4

May 19th, 2022
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4.  
  5. public class SentimentAnalyzer {
  6.  
  7.     // Tip: labeled continue can be used when iterating featureSet + convert review to lower-case
  8.  
  9. public static int[] detectProsAndCons(String review, String[][] featureSet, String[] posOpinionWords,
  10.  
  11. String[] negOpinionWords) {
  12.  
  13. int[] featureOpinions = new int[featureSet.length]; // output
  14.  
  15.                 review = review.toLowerCase();
  16.  
  17.                 // your code ~ you will be invoking getOpinionOnFeature
  18.  
  19.                 for(int i=0; i<=featureSet.length-1 ; i++){
  20.  
  21.                     for(int j=0; j<=featureSet[i].length -1 ; j++){
  22.  
  23.                        
  24.  
  25.                       featureOpinions[i] = getOpinionOnFeature(review,featureSet[i][j],posOpinionWords,negOpinionWords);
  26.  
  27.                     }
  28.  
  29.                 }
  30.  
  31. return featureOpinions;
  32.  
  33. }
  34.  
  35.  
  36.  
  37. // First invoke checkForWasPhrasePattern and
  38.  
  39. // if it cannot find an opinion only then invoke checkForOpinionFirstPattern
  40.  
  41. private static int getOpinionOnFeature(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  42.  
  43. // your code
  44.  
  45.         int x=checkForWasPhrasePattern(review,feature,posOpinionWords,negOpinionWords);
  46.  
  47.         if( x==0){
  48.  
  49.            x=checkForOpinionFirstPattern(review,feature,posOpinionWords,negOpinionWords);
  50.  
  51.         }
  52.  
  53. return x;
  54.  
  55. }
  56.  
  57.  
  58.  
  59. // Tip: Look at String API doc. Methods like indexOf, length, substring(beginIndex), startsWith can come into play
  60.  
  61. // Return 1 if positive opinion found, -1 for negative opinion, 0 for no opinion
  62.  
  63. // You can first look for positive opinion. If not found, only then you can look for negative opinion
  64.  
  65. private static int checkForWasPhrasePattern(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  66.  
  67. int opinion = 0;
  68.  
  69. String pattern = feature + " was ";
  70.  
  71.  
  72.  
  73. // your code
  74.  
  75. for (int i=0;i<posOpinionWords.length-1;i++){
  76.  
  77.      if (review.contains(pattern + posOpinionWords[i])) opinion = 1;
  78.  
  79. }
  80.  
  81. if (opinion ==0){
  82.  
  83.   for (int i=0;i<negOpinionWords.length-1;i++){
  84.  
  85.      if (review.contains(pattern + negOpinionWords[i])) opinion = -1;
  86.  
  87.   }
  88.  
  89. }
  90.  
  91. return opinion;
  92.  
  93. }
  94.  
  95. // You can first look for positive opinion. If not found, only then you can look for negative opinion
  96.  
  97. private static int checkForOpinionFirstPattern(String review, String feature, String[] posOpinionWords,
  98.  
  99. String[] negOpinionWords) {
  100.  
  101. // Extract sentences as feature might appear multiple times.
  102.  
  103. // split() takes a regular expression and "." is a special character
  104.  
  105. // for regular expression. So, escape it to make it work!!
  106.  
  107. String[] sentences = review.split("\\.");
  108.  
  109. int opinion = 0;
  110.  
  111. // String str="";
  112.  
  113. // your code for processing each sentence. You can return if opinion is found in a sentence (no need to process subsequent ones)
  114.  
  115. for (int i=0;i<posOpinionWords.length-1;i++){
  116.  
  117.     //str=posOpinionWords[i] + feature;
  118.  
  119.      if (sentences[i].startsWith(posOpinionWords[i] + feature)) opinion = 1;
  120.  
  121. }
  122.  
  123. if (opinion ==0){
  124.  
  125.   for (int i=0;i<negOpinionWords.length-1;i++){
  126.  
  127.      if (sentences[i].startsWith(negOpinionWords + feature)) opinion = -1;
  128.  
  129.   }
  130.  
  131. }
  132.  
  133.  
  134.  
  135. return opinion;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. public static void main(String[] args) {
  142.  
  143. String review = "Haven't been here in years! Fantastic service and the food was delicious! Definetly will be a frequent flyer! Francisco was very attentive";
  144.  
  145. //String review = "Sorry OG, but you just lost some loyal customers. Horrible service, no smile or greeting just attitude. The breadsticks were stale and burnt, appetizer was cold and the food came out before the salad.";
  146.  
  147. String[][] featureSet = {
  148.  
  149.         { "ambiance", "ambience", "atmosphere", "decor" },
  150.  
  151. { "dessert", "ice cream", "desert" },
  152.  
  153. { "food" },
  154.  
  155. { "soup" },
  156.  
  157. { "service", "management", "waiter", "waitress", "bartender", "staff", "server" } };
  158.  
  159. String[] posOpinionWords = { "good", "fantastic", "friendly", "great", "excellent", "amazing", "awesome","delicious" };
  160.  
  161. String[] negOpinionWords = { "slow", "bad", "horrible", "awful", "unprofessional", "poor" };
  162.  
  163.  
  164.  
  165. int[] featureOpinions = detectProsAndCons(review, featureSet, posOpinionWords, negOpinionWords);
  166.  
  167. System.out.println("Opinions on Features: " + Arrays.toString(featureOpinions));
  168.  
  169. }
  170.  
  171. }
  172.  
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement