Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

all questions please. Python In [21]: text = You look wonderful today. The sky is blue and the sun is shining. I look at you.

image text in transcribedimage text in transcribedimage text in transcribed

all questions please. Python

In [21]: text = "You look wonderful today. The sky is blue and the sun is shining. I look at you. Look at these trees. There print(text) You look wonderful today. The sky is blue and the sun is shining. I look at you. Look at these trees. There are bir ds and butterflies here. In [29]: ("5hining.").5plit(",") Out [29]: ['shining', ''] Step 2. Create a list of words and punctuations (separately?) of text. Expected output: ['you', 'look', 'wonderful', 'today', '.', 'the', 'sky', 'is', 'blue', 'and', 'the', 'sun', 'is', 'shining', ',', 'i', 'look', 'at', 'you', '.', 'look', 'at', 'these', 'trees', '.', 'there', 'are', 'birds', 'and', 'butterflies', 'here', ','] In [ ] : In [2]: text = "You look wonderful today. The sky is blue and the sun is shining. I look at you. Look at these trees. There words = [word. strip ("," ") for word in text. split()] punctuations = [word for word in text. split() if word.strip(). endswith((".,", ","))] output = words + punctuations print(output) ['You', 'look', 'wonderful', 'today', 'The', '5ky', 'is', 'blue', 'and', 'the', 'sun', 'is', 'shining', 'I', 'look' ' 'at', 'you', 'Look', 'at', 'these', 'trees', 'There', 'are', 'birds', 'and', 'butterflies', 'here', 'today,', 'sh ining.', 'you.", "trees.", 'here.'] In [4]: words = [] for word in text. split (]: words. append (word. strip (".,"). Lower (J) if word. strip(). endswith ( (",",",")) : words. append (word. strip {[[1]) Step 3. Based on the previous output, create a list of bigrams of that text. Expected output: [['you', 'look'], ['look', 'wonderful'], ['wonderful', 'today'], ['today', ','], ['.', 'the'], ['the', 'sky'], ['sky', 'is'], ['is', 'blue'], ['blue', 'and'], ['and', 'the'], ['the', 'sun'], ['sun', 'is'], ['is', 'shining'], ['shining', ','], ['.', 'i'], ['i', 'look'], ['look', 'at'], ['at', 'you'], ['you', ','], ['.', 'look'], ['at', 'these'], ['these', 'trees'], ['trees', '.' ], ['.', 'there'], ['there', 'are'], ['are', 'birds'], ['birds', 'and'], ['and', 'butterflies'], ['butterflies', 'here'], ['here', ',']] In [26]: Out [26] : [ ] Step 4. A function choice from the package random takes a non-empty list as input and returns a random item from this list as the output In [20]: from random import choice choice (["A", "B", "C"]) Out [20]: 'C' Let us then pick a random bigram as the beginning of the sentence, and then keep adding words to that sentence depending on its last word. The sentence is finished if we encountered .. In [58] : ['blue', 'and'] ['and', 'butterflies'] ['is', "blue'] ['the', 'sun'] ['and', 'the'] ['butterflies', 'here'] ['at', 'you'] ['Look', 'wonderful'] ['you', 'look'] [ is", 'blue'] ['and', 'the'] ['is', "blue'] ['trees", '] Out [58]: 'blue andand butterfliesis bluethe sunand thebutterflies hereat youlook wonderfulyou lookis blueand theis bluetrees In [53] : sky is blue and butterflies here. Question: will there be any difference in the "quality" of the generated sentences if we use the same code, but keep duplicate bigrams? Extension: madify the code above above so that it's not a bigram based sentence generator, but rather an n-gram based. In [ ] : Type Markdown and LaTeX: 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions