Question

In: Computer Science

Problem: Perform the word counter problem through MapReduce. Tools: Anaconda/Python 3/Jupyter Notebook Example word: “One a...

Problem:

Perform the word counter problem through MapReduce.

Tools: Anaconda/Python 3/Jupyter Notebook

Example word: “One a penny, two a penny, hot cross buns.”

Submitted files: (1) Source code (adding comments); (2) Supporting document for code comments

Solutions

Expert Solution

Code-

def Map(line):

    # varibale to store mapped values

    ret = []

    # remove leading and trailing whitespace

    line = line.strip()

    # split the line into words

    words = line.split()

    # increase counters

    for word in words:

        # add the words with count = 1 to ret list

        ret.append(( f'{word}\t1'))

    return ret

def reduce(lst:list):

    words={}

    for line in lst:

        # remove leading and trailing whitespace

        line = line.strip()

        # parse the input

        word, count = line.split('\t', 1)

        # reduce by adding word to the dictionary

        words[word] = words.get(word, 0) + int(count)

    # print the result to console

    for word, count in words.items():

        print(f'{word} = {count}')

#apply the algorithm by first mapping then reducing

reduce(Map("One a penny, two a penny, hot cross buns."))

OUTPUT-

I hope it helps. For any doubt, feel free to ask in comments, and give upvote if u get the answer.


Related Solutions

Create a new Python 3 Jupyter Notebook. At the top, be sure to name your notebook...
Create a new Python 3 Jupyter Notebook. At the top, be sure to name your notebook as "*assignment 2.08 - Your Name Here.ipynb*" (obviously, replace the "Your Name Here" part with your actual name). Create a single python cell to program the following specifications. Use what you've learned on this page to: 1. Find the index of "lmno" in the English alphabet using an appropriate instruction and store it in a variable. (hint: you'll need to define a string that...
Please give me an example of how we import stock data in jupyter notebook(Python) and analyze...
Please give me an example of how we import stock data in jupyter notebook(Python) and analyze each step.
Python HW Open a new Jupyter notebook Create a new function named fibonacci() that takes one...
Python HW Open a new Jupyter notebook Create a new function named fibonacci() that takes one required parameter: maxint, an integer that will serve as the upper bound of the loop Following the example on the Python tutorial: https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming Our implementation will have a few changes: While you will use a while loop to make a Fibonacci sequence, the upper bound of the sequence will be your maxint parameter. Store the results into a list and append each new generated...
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an...
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an integer number. The function, which must be a value-returning function, returns 1 if the number is even or 0 if the number is odd. In the “main” function (i.e. def main()), capture the return value and print an appropriate message on screen (i.e. number is even or odd). Rubric: Correctly defined a value-returning function: 5 pts Correctly capture and use return value from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT