fork download
  1. import java.util.Arrays;
  2.  
  3. class SentimentAnalyzer {
  4. // Tip: labeled continue can be used when iterating featureSet + convert review to lower-case
  5. public static int[] detectProsAndCons(String review, String[][] featureSet, String[] posOpinionWords,
  6. String[] negOpinionWords) {
  7. int[] featureOpinions = new int[featureSet.length]; // Initialized to 0 by default
  8.  
  9. // Pass each pro or con to getOpinionOnFeature
  10. int index = 0;
  11. for (String[] ss: featureSet) { // For each feature
  12. for (String s: ss) { // Check feature synonyms
  13. featureOpinions[index] = getOpinionOnFeature(review.toLowerCase(), s, posOpinionWords, negOpinionWords);
  14. // Store the result when we find something and stop looking for more.
  15. if (featureOpinions[index] != 0) {
  16. index++;
  17. break; // Go to next feature
  18. }
  19. }
  20. }
  21. return featureOpinions;
  22. }
  23.  
  24. // First invoke checkForWasPhrasePattern and
  25. // if it cannot find an opinion only then invoke checkForOpinionFirstPattern
  26. private static int getOpinionOnFeature(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  27.  
  28. // Call the pattern checks
  29. int result = checkForWasPhrasePattern(review, feature, posOpinionWords, negOpinionWords);
  30.  
  31. if (result == 0)
  32. {
  33. result = checkForOpinionFirstPattern(review, feature, posOpinionWords, negOpinionWords);
  34. }
  35.  
  36. return result;
  37. }
  38.  
  39. // Tip: Look at String API doc. Methods like indexOf, length, substring(beginIndex), startsWith can come into play
  40. // Return 1 if positive opinion found, -1 for negative opinion, 0 for no opinion
  41. // You can first look for positive opinion. If not found, only then you can look for negative opinion
  42. private static int checkForWasPhrasePattern(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  43. int opinion = 0;
  44. String pattern = feature + " was "; // Let's do this without tokenizing the string around spaces, since we have this pattern string provided.
  45.  
  46. // your code
  47. int index = review.indexOf(pattern);
  48.  
  49. // If pattern matches
  50. if (index != -1) {
  51. // Move index to the opinion word
  52. // Note: will go out of bounds if at end of review string, but we shouldn't be at the end of a string mid-sentence
  53. index += pattern.length();
  54.  
  55. // Error check
  56. if (index >= review.length())
  57. return 0; // We have found an incomplete sentence at the end of the string.
  58.  
  59. // Since we are not out of bounds, find opinion
  60. String sub = review.substring(index);
  61.  
  62. for (String s : posOpinionWords) {
  63. // If we have a positive opinion
  64. if (sub.startsWith(s)) {
  65. opinion = 1;
  66. return opinion;
  67. }
  68. }
  69.  
  70. for (String s : negOpinionWords) {
  71. // If we have a negative opinion
  72. if (sub.startsWith(s)) {
  73. opinion = -1;
  74. return opinion;
  75. }
  76. }
  77. }
  78.  
  79. // If pattern doesn't match, or no opinion was found after the pattern:
  80. return opinion; // still 0
  81. }
  82.  
  83. private static int checkForOpinionFirstPattern(String review, String feature, String[] posOpinionWords,
  84. String[] negOpinionWords) {
  85. // Extract sentences as feature might appear multiple times.
  86. // split() takes a regular expression and "." is a special character
  87. // for regular expression. So, escape it to make it work!!
  88. String[] sentences = review.split("\\.");
  89. int opinion = 0;
  90.  
  91. // Find positive adjective + feature combination
  92. int index;
  93. for (String s : sentences) {
  94. for (String p : posOpinionWords) {
  95. index = s.indexOf(feature + p);
  96. // If we've found the feature + opinion, return
  97. if (index != -1) {
  98. opinion = 1; // Used explicit syntax for readability.
  99. return opinion;
  100. }
  101. }
  102. }
  103.  
  104. // Find negative adjevtive + feature combination
  105. for (String s : sentences) {
  106. for (String n : negOpinionWords) {
  107. index = s.indexOf(feature + n);
  108. // If we've found the feature + opinion, return
  109. if (index != -1) {
  110. opinion = -1; // Used explicit syntax for readability.
  111. return opinion;
  112. }
  113. }
  114. }
  115.  
  116. return opinion;
  117. }
  118.  
  119. public static void main(String[] args) {
  120. 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";
  121.  
  122. //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.";
  123.  
  124. String[][] featureSet = {
  125. { "ambiance", "ambience", "atmosphere", "decor" },
  126. { "dessert", "ice cream", "desert" },
  127. { "food" },
  128. { "soup" },
  129. { "service", "management", "waiter", "waitress", "bartender", "staff", "server" } };
  130. String[] posOpinionWords = { "good", "fantastic", "friendly", "great", "excellent", "amazing", "awesome",
  131. "delicious" };
  132. String[] negOpinionWords = { "slow", "bad", "horrible", "awful", "unprofessional", "poor" };
  133.  
  134. int[] featureOpinions = detectProsAndCons(review, featureSet, posOpinionWords, negOpinionWords);
  135. System.out.println("Opinions on Features: " + Arrays.toString(featureOpinions));
  136. }
  137. }
Success #stdin #stdout 0.16s 50648KB
stdin
Standard input is empty
stdout
Opinions on Features: [1, 0, 0, 0, 0]