<jupyter_start><jupyter_text>Word Count of Pride and Prejudice using PySpark<jupyter_code>import re, string

text_file = sc.textFile('Data/Pride_and_Prejudice.txt')
# first 5 elements of the RDD
text_file.take(5)
type(u'lama')
string.punctuation
punc = '!"#$%&\'()*+,./:;<=>?@[\\]^_`{|}~'
def uni_to_clean_str(x):
    converted = x.encode('utf-8')
    lowercased_str = converted.lower()
    # for more difficult cases use re.split(' A|B')
    lowercased_str = lowercased_str.replace('--',' ')
    clean_str = lowercased_str.translate(None, punc) #Change 1
    return clean_str
one_RDD = text_file.flatMap(lambda x: uni_to_clean_str(x).split())
one_RDD = one_RDD.map(lambda x: (x,1))
one_RDD = one_RDD.reduceByKey(lambda x,y: x + y)
one_RDD.take(15)
one_RDD = text_file.map(lambda x: uni_to_clean_str(x))
one_RDD.take(5)
one_RDD = text_file.map(lambda x: uni_to_clean_str(x).split())
one_RDD.take(5)
one_RDD = text_file.flatMap(lambda x: uni_to_clean_str(x).split())
one_RDD.take(5)
one_RDD = text_file.flatMap(lambda x: uni_to_clean_str(x).split())
one_RDD = one_RDD.map(lambda x: (x,1))
one_RDD.take(5)
one_RDD = text_file.flatMap(lambda x: uni_to_clean_str(x).split())
one_RDD = one_RDD.map(lambda x: (x,1))
one_RDD = one_RDD.reduceByKey(lambda x,y: x + y)
one_RDD.take(15)
one_RDD = text_file.flatMap(lambda x: uni_to_clean_str(x).split())
one_RDD = one_RDD.map(lambda x: (x,1)) 
one_RDD = one_RDD.reduceByKey(lambda x,y: x + y)
one_RDD.take(5)
one_RDD = one_RDD.map(lambda x:(x[1],x[0])) 
one_RDD.take(5)
one_RDD.sortByKey(False).take(15)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1673027989821883, 'fraction_numerical': 0.013994910941475827, 'mean_word_length': 6.1826484018264845, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '146843', 'license': 'MIT', 'n_tokens_mistral': 725, 'n_tokens_neox': 704, 'n_words': 115, 'path': 'PySpark_Basics/.ipynb_checkpoints/PySpark_Part1_Word_Count_Removing_Punctuation_Pride_Prejudice-checkpoint.ipynb', 'repo_name': 'mrpal39/Python_Tutorials', 'size': '1572'}