Advertisement
CR7CR7

Sentiment analysies

May 19th, 2022
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public 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]; // output
  8.  
  9.         // your code ~ you will be invoking getOpinionOnFeature    
  10.  
  11.         return featureOpinions;
  12.     }
  13.  
  14.     // First invoke checkForWasPhrasePattern and
  15.     // if it cannot find an opinion only then invoke checkForOpinionFirstPattern
  16.     private static int getOpinionOnFeature(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  17.        
  18.         // your code
  19.        
  20.     }  
  21.  
  22.     // Tip: Look at String API doc. Methods like indexOf, length, substring(beginIndex), startsWith can come into play
  23.     // Return 1 if positive opinion found, -1 for negative opinion, 0 for no opinion
  24.     // You can first look for positive opinion. If not found, only then you can look for negative opinion
  25.     private static int checkForWasPhrasePattern(String review, String feature, String[] posOpinionWords, String[] negOpinionWords) {
  26.         int opinion = 0;
  27.         String pattern = feature + " was ";
  28.  
  29.         // your code
  30.  
  31.         return opinion;    
  32.     }
  33.    
  34.     // You can first look for positive opinion. If not found, only then you can look for negative opinion
  35.     private static int checkForOpinionFirstPattern(String review, String feature, String[] posOpinionWords,
  36.             String[] negOpinionWords) {
  37.         // Extract sentences as feature might appear multiple times.
  38.         // split() takes a regular expression and "." is a special character
  39.         // for regular expression. So, escape it to make it work!!
  40.         String[] sentences = review.split("\\.");
  41.         int opinion = 0;
  42.        
  43.         // your code for processing each sentence. You can return if opinion is found in a sentence (no need to process subsequent ones)       
  44.  
  45.         return opinion;
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.         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";
  50.        
  51.         //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.";
  52.        
  53.         String[][] featureSet = {
  54.                 { "ambiance", "ambience", "atmosphere", "decor" },
  55.                 { "dessert", "ice cream", "desert" },
  56.                 { "food" },
  57.                 { "soup" },
  58.                 { "service", "management", "waiter", "waitress", "bartender", "staff", "server" } };
  59.         String[] posOpinionWords = { "good", "fantastic", "friendly", "great", "excellent", "amazing", "awesome",
  60.                 "delicious" };
  61.         String[] negOpinionWords = { "slow", "bad", "horrible", "awful", "unprofessional", "poor" };
  62.  
  63.         int[] featureOpinions = detectProsAndCons(review, featureSet, posOpinionWords, negOpinionWords);
  64.         System.out.println("Opinions on Features: " + Arrays.toString(featureOpinions));
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement