SHARE
TWEET

Untitled

a guest Oct 13th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     smoothing_parameter = 0.369
  2.     no_words_pos_rev = 0
  3.     word_in_pos_rev ={}
  4.  
  5.     no_words_neg_rev = 0
  6.     word_in_neg_rev ={}
  7.  
  8.     unique_words = set()
  9.  
  10.     label = 0
  11.     for review in train_set:
  12.         # review is positive
  13.         if train_labels[label] == 1:
  14.             for word in review:
  15.                 unique_words.add(word)
  16.  
  17.                 no_words_pos_rev += 1
  18.                 if word in word_in_pos_rev:
  19.                     word_in_pos_rev[word] += 1
  20.                 else:
  21.                     word_in_pos_rev[word] = 1
  22.         # review is negative    
  23.         else:
  24.             for word in review:
  25.                 unique_words.add(word)
  26.  
  27.                 no_words_neg_rev += 1
  28.                 if word in word_in_neg_rev:
  29.                     word_in_neg_rev[word] += 1
  30.                 else:
  31.                     word_in_neg_rev[word] = 1
  32.  
  33.  
  34.     sentiment_of_reviews = []
  35.     for review in dev_set:
  36.         positive_sentiment = np.log(pos_prior)
  37.         negative_sentiment = np.log(1-pos_prior)
  38.         for word in review:
  39.             denomenator_pos = no_words_pos_rev + smoothing_parameter*(len(unique_words)+1)
  40.             denomenator_neg = no_words_neg_rev + smoothing_parameter*(len(unique_words)+1)
  41.  
  42.             if word in word_in_pos_rev:
  43.                 # P(word_i|Positive) = (number of times word occured in a positive review) / (number of words in positive reviews)
  44.                 positive_sentiment += np.log( (word_in_pos_rev[word] + np.log(smoothing_parameter) ) / denomenator_pos )
  45.             else:
  46.                 positive_sentiment += np.log(smoothing_parameter/denomenator_pos)
  47.  
  48.             if word in word_in_neg_rev:
  49.                 negative_sentiment += np.log( (word_in_neg_rev[word] + np.log(smoothing_parameter) ) / denomenator_neg )
  50.             else:
  51.                 negative_sentiment += np.log(smoothing_parameter/denomenator_neg)
  52.         if positive_sentiment > negative_sentiment:
  53.             sentiment_of_reviews.append(1)
  54.         else:
  55.             sentiment_of_reviews.append(0)
  56.     print(sentiment_of_reviews)
  57.     return sentiment_of_reviews
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top