Question

bigrams = text_file.flatMap(lambda line: line.split(".")) \ .map(lambda line: line.strip().split(" ")) \ .flatMap(lambda xs: (tuple(x) for x...

bigrams = text_file.flatMap(lambda line: line.split(".")) \
                   .map(lambda line: line.strip().split(" ")) \
                   .flatMap(lambda xs: (tuple(x) for x in zip(xs, xs[1:])))

bigrams.map(lambda x: (x, 1)).reduceByKey(lambda x, y: x + y)

This code gives me a list of bigrams from my file. Can this be edited so it gives, unigrams/trigrams/quadgrams etc?

0 0
Add a comment Improve this question Transcribed image text
Answer #1



def getNGrams(n):
    grams = text_file
        .flatMap(lambda line: line.split(".")) \
        .map(lambda line: line.strip().split(" ")) \
        .flatMap(lambda xs: (tuple(x[i:i+n]) for i in range(len(xs) - n + 1)))
        
return grams.map(lambda x: (x, 1)).reduceByKey(lambda x, y: x + y)

**************************************************

I have created a generic function, which you can call with values n=1,2....n for Ngrams. Like for bigrams, use 2.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
bigrams = text_file.flatMap(lambda line: line.split(".")) \ .map(lambda line: line.strip().split(" ")) \ .flatMap(lambda xs: (tuple(x) for x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me t...

    For Python, I am a little confused on the use of reduce,map, and lambda. My assignment wants me to not use .join() and write a SINGLE LINE function to join each letter in a list with another string.(See problem below). Using reduce, map, and lambda, write the single-expression function myJoin(L, sep) that takes a non-empty list L and a string sep, and without calling sep.join(L), works roughly the same as that returning a single string with the values in L...

  • ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple...

    ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple In this lab, you will write a scrambled word game.  The game starts by loading a file containing scrambled word-answer pair separated.  Sample of the file content is shown below.  Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it.  The number of guesses is unlimited.  When the user guesses the correct answer, it asks the user if he/she wants another scrambled...

  • I am having a hard time with my program to assignment 4.12. Here are the instructions:...

    I am having a hard time with my program to assignment 4.12. Here are the instructions: The Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is the following: <last name> <hours worked> <hourly wage> Write a program that inputs a filename from the user and prints to the terminal a report of the wages paid to the employees for the given period. The report...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • sc GEOMETRY IA A highway on a map can be approximated by the line y =...

    sc GEOMETRY IA A highway on a map can be approximated by the line y = x + 1, where x and y are in tens of miles. Which of these is the distance from a house at point P(4, -3) to the highway? 1D 412 5.7 -10 41526.9 8

  • (1 point) In your answers below, for the variable i type the word lambda, for y...

    (1 point) In your answers below, for the variable i type the word lambda, for y type the word gamma; otherwise treat these as you would any other variable. We will solve the heat equation u, = 4uxx: 0<x<2, 120 with boundary/initial conditions: u(0,1) = 0, and u(x,0) = So, 0<x< 1 u(2, 1) = 0, 13, 1<x<2. This models temperature in a thin rod of length L = 2 with thermal diffusivity a = 4 where the temperature at...

  • python 3.6 please ! and comment your code .def remove(val, xs, limit-None): Remove multiple copies of...

    python 3.6 please ! and comment your code .def remove(val, xs, limit-None): Remove multiple copies of val from xs (directly modify the list value that xs refers to). You may only remove up to the first limit occurrences of val. If limit -3, and xs had ten copies of val in it, then you'd only remove the first three and leave the last seven in place. When limitNone, there's truly no limit (and we remove all occurrences of val). Return...

  • Help needed related python task ! Thanx again How you're doing it • Write a function...

    Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...

  • Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • Using Python, if you could help me with the code # Create a modified version of...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT