Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m pretty confused on this whole question, and where it even begins Implementa

ID: 3748709 • Letter: I

Question

I'm pretty confused on this whole question, and where it even begins

Implementation Details Before you start working on the aforementioned functions, it's important to consider how we'll be parsing passages for tokens. Here's the body of Romeo's balcony soliloquy In [1]: #-GRADED- ROMEO SOLILOQUY = """ But, soft! what light through yonder window breaks? It is the east, and Juliet is the sun Arise, fair sun, and kill the envious moon, who is already sick and pale with grief, Thatthou her maid art far more fair than she: be not her maid, since she is envious; her vestal livery is but sick and green and none but fools do wear it; cast it off. It is my lady, 0, it is my love! 0, that she knew she were! She speaks yet she says nothing: what of that? Her eye discourses; I will answer it. I am too bold, 'tis not to me she speaks: two of the fairest stars in all the heaven, having some business, do entreat her eyes to twinkle in their spheres till they return What if her eyes were there, they in her head? The brightness of her cheek would shame those stars, as daylight doth a lamp; her eyes in heaven would through the airy region stream so bright that birds would sing and think it were not night See, how she leans her cheek upon her hand! 0, that I were a glove upon that hand, that I might touch that cheek! " # Using the string's built-in split methodpreviously mentioned in class - along with lower, we can derive from the passage a list of tokens toks = [t, lower() for t in ROMEOSOLILOQUY, split()] toks[:8] We could do more interesting things (such as separating out punctuation), but we'll keep our parser simple. For the sake of consistency, we'll rely on this In [ ] : - fairly straighttforward approach to parsing. Onwards!

Explanation / Answer

import random

def compute_ngrams(toks,n=2):
"""
This problem pertains to Natural language processing
Returns an n-gram dictionary based on the provided list of tokens.
Basic Idea is to iterate over each element and manually combine it with the one directly following it.
This can be acheived by using loop to iterate or use zip (more pythonic)
Here is the implementation
"""
return zip(*[toks[i:] for i in range(n)]))

"""
Function is designed as per specification provied in the question
random module is used
"""
def gen_passage(dict, token=10):
list = []
random_key = random.choice(dict.keys())
for i in range(token):
random_tuple = random.choice(dict[random_key].pop())
list.append(random_tuple);
if random_tuple[-1] not in dict.keys():
random_key = random_tuple[-1]