[python] Sentiment Analysis

Viewer

copydownloadembedprintName: Sentiment Analysis
  1. def main(sc,filename):
  2.     # CODE IT YOURSELF  
  3.     rdd = sc.textFile(filename).map(lambda text: remove_features(text)) #remove and replace
  4.     rdd_en = rdd.filter(lambda text: TextBlob(text).detect_language() == 'en').map(lambda text: abb_en(text)) #filter to english
  5.     rdd_ms = rdd.filter(lambda text: TextBlob(text).detect_language() == 'ms').map(lambda text: abb_bm(text)).map(lambda text: str(TextBlob(text).translate(to='en'))) #filter to bahasa and translate to en
  6.     
  7.     rdd = rdd_en.union(rdd_ms)
  8.  
  9.     positive_rdd = rdd.filter(lambda text: TextBlob(text).sentiment.polarity > 0)
  10.     negative_rdd = rdd.filter(lambda text: TextBlob(text).sentiment.polarity < 0)
  11.  
  12.     make_plot(int(positive_rdd.count()),int(negative_rdd.count())) #the cast is just to ensure the value is in integer data type
  13.  

Editor

You can edit this paste and save as new:


File Description
  • Sentiment Analysis
  • Paste Code
  • 06 May-2021
  • 817 Bytes
You can Share it: