Question

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

Implementation Details Before you start working on the aforementioned functions, its important to consider how well be parsing passages for tokens. Heres the body of Romeos 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! #</GRADED> Using the strings 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 well keep our parser simple. For the sake of consistency, well rely on this In [ ] : - fairly straighttforward approach to parsing. Onwards!

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

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]

Add a comment
Know the answer?
Add Answer to:
I'm pretty confused on this whole question, and where it even begins Implementation Details Before you...
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
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