[text] handons 4

Viewer

copydownloadembedprintName: handons 4
  1. from textblob import TextBlob
  2. import matplotlib.pyplot as plt
  3. from pyspark import SparkConf, SparkContext
  4. import re
  5. import string
  6.  
  7.  
  8. ##OTHER FUNCTIONS/CLASSES
  9.  
  10. def resolve_emoticon(line):
  11.    emoticon = {
  12.             ':-)' : 'smile',
  13.         ':)'  : 'sad',
  14.             ':))' : 'very happy',
  15.             ':)'  : 'happy',
  16.             ':((' : 'very sad',
  17.             ':('  : 'sad',
  18.             ':-P' : 'tongue',
  19.             ':-o' : 'gasp',
  20.             '>:-)':'angry'
  21.    }   
  22.    for key in emoticon:
  23.         line = line.replace(key, emoticon[key])
  24.    return line
  25.  
  26. def abb_bm(line):
  27.    abbreviation_bm = {
  28.          'sy': 'saya',
  29.          'sk': 'suka',
  30.          'byk': 'banyak',
  31.          'sgt' : 'sangat',
  32.          'mcm' : 'macam',
  33.          'bodo':'bodoh',
  34.          'kat' : 'dekat'
  35.    }  
  36.    abbrev = ' '.join (abbreviation_bm.get(word, word) for word in line.split())  
  37.    return (resolve_emoticon(abbrev)) 
  38.  
  39.   
  40.  
  41. def abb_en(line):
  42.    abbreviation_en = {
  43.     'u': 'you',
  44.     'thr': 'there',
  45.     'asap': 'as soon as possible',
  46.     'lv' : 'love',    
  47.     'c' : 'see'
  48.    } 
  49.    abbrev = ' '.join (abbreviation_en.get(word, word) for word in line.split())
  50.    return (resolve_emoticon(abbrev))  
  51.  
  52. def make_plot(pos,neg):
  53.   
  54.    #This function plots the counts of positive and negative words     
  55.  
  56.    Polarity = [1,2]
  57.    LABELS = ["Positive", "Negative"]
  58.    Count_polarity = [int(pos), int(neg)]
  59.  
  60.    plt.xlabel('Polarity')
  61.    plt.ylabel('Count')
  62.    plt.title('Sentiment Analysis - Lexical Based')
  63.  
  64.    plt.grid(True)
  65.  
  66.    plt.bar(Polarity, Count_polarity, align='center')
  67.    plt.xticks(Polarity, LABELS)
  68.    plt.show()
  69.  
  70.  
  71. def remove_features(data_str): #by niewan
  72.  
  73.     url_re = re.compile(r'https?://(\S+)')
  74.     num_re = re.compile(r'(\d+)')
  75.     mention_re = re.compile(r'(@|#)(\w+)')
  76.     RT_re = re.compile(r'RT(\s+)')
  77.  
  78.     data_str = str(data_str)
  79.     data_str = RT_re.sub('', data_str) # remove RT
  80.     data_str = url_re.sub('', data_str) # remove hyperlinks
  81.     data_str = mention_re.sub('', data_str) # remove @mentions and hash
  82.     data_str = num_re.sub('', data_str) # remove numerical digit
  83.     data_str = resolve_emoticon(data_str) # replace emoji
  84.     return data_str.lower()
  85.  
  86.  
  87. def main(sc,filename): #by niewan
  88.     # CODE IT YOURSELF  
  89.     rdd = sc.textFile(filename).map(lambda text: remove_features(text)) #remove and replace
  90.     rdd_en = rdd.filter(lambda text: TextBlob(text).detect_language() == 'en').map(lambda text: abb_en(text)) #filter to english
  91.     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
  92.     
  93.     rdd = rdd_en.union(rdd_ms)
  94.  
  95.     positive_rdd = rdd.filter(lambda text: TextBlob(text).sentiment.polarity > 0)
  96.     negative_rdd = rdd.filter(lambda text: TextBlob(text).sentiment.polarity < 0)
  97.  
  98.     make_plot(int(positive_rdd.count()),int(negative_rdd.count())) #the cast is just to ensure the value is in integer data type
  99.    
  100.  
  101.  
  102. if __name__ == "__main__":
  103.  
  104.    # Configure your Spark environment
  105.    conf = SparkConf().setMaster("local[*]").setAppName("My Spark Application")
  106.    sc = SparkContext(conf=conf)
  107.    # CODE IT YOURSELF
  108.   
  109.    filename = "simple_sentences.txt"
  110.     
  111.    resolve_emoticon(line)
  112.    abb_bm(line)
  113.    abb_en(line)
  114.    make_plot(pos,neg)
  115.    remove_features(data_str)
  116.    main(sc, filename)
  117.  
  118.    sc.stop()
  119.  

Editor

You can edit this paste and save as new: