Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Please write a Python 3 program. Please also show all of the ouputs. Kata Fourteen: Tom Swift Under Milk Wood Adapted from Dave Thomass

1. Please write a Python 3 program. Please also show all of the ouputs.

Kata Fourteen: Tom Swift Under Milk Wood

Adapted from Dave Thomass work:

http://codekata.com/kata/kata14-tom-swift-under-the-milkwood/

Trigrams

Trigrams can be used to mutate text into new, surreal, forms. But what heuristics do we apply to get a reasonable result?

The Problem

As a boy, one of my treats was go to the shops on a Saturday and spend part of my allowance on books; for a nine-year old, I had quite a collection of Tom Swift and Hardy Boys. Wouldnt it be great to be able to create more and more of these classic books, to be able to generate a new Tom Swift adventure on demand?

OK, perhaps not. But that wont stop us trying. I coded up a quick program to generate some swash-buckling scientific adventure on demand. It came up with:

... it was in the wind that was what he thought was his companion. I think would be a good one and accordingly the ship their situation improved. Slowly so slowly that it beat the band! Youd think no one was a low voice. Dont take any of the elements and the inventors of the little Frenchman in the enclosed car or cabin completely fitted up in front of the gas in the house and wringing her hands. Im sure theyll fall!

She looked up at them. He dug a mass of black vapor which it had refused to accept any. As for Mr. Swift as if it goes too high Ill warn you and you can and swallow frequently. That will make the airship was shooting upward again and just before the raid wouldnt have been instrumental in capturing the scoundrels right out of jail.

Stylistically, its Victor Appleton meets Dylan Thomas. Technically, its all done with trigrams.

Trigram analysis is very simple. Look at each set of three adjacent words in a document. Use the first two words of the set as a key, and remember the fact that the third word followed that key. Once youve finished, you know the list of individual words that can follow each two word sequence in the document. For example, given the input:

I wish I may I wish I might 

You might generate:

"I wish" => ["I", "I"] "wish I" => ["may", "might"] "may I" => ["wish"] "I may" => ["I"] 

This says that the words I wish are twice followed by the word I, the words wish I are followed once by may and once by might and so on.

To generate new text from this analysis, choose an arbitrary word pair as a starting point. Use these to look up a random next word (using the table above) and append this new word to the text so far. This now gives you a new word pair at the end of the text, so look up a potential next word based on these. Add this to the list, and so on. In the previous example, we could start with I may. The only possible next word is I, so now we have:

I may I 

The last two words are may I, so the next word is wish. We then look up I wish, and find our choice is constrained to another I.:

I may I wish I 

Now we look up wish I, and find we have a choice. Lets choose may:

I may I wish I may 

Now were back where we started from, with I may. Following the same sequence, but choosing might this time, we get:

I may I wish I may I wish I might 

At this point we stop, as no sequence starts I might.

Given a short input text, the algorithm isnt too interesting. Feed it a book, however, and you give it more options, so the resulting output can be surprising.

For this kata, try implementing a trigram algorithm that generates a couple of hundred words of text using a book-sized file as input. Project Gutenberg is a good source of online books (Tom Swift and His Airship is here.)

Be warned that these files have DOS line endings (carriage return followed by newline).

There is a copy of sherlock holmes right here:

sherlock.txt.

And a shorter copy for testing:

sherlock_small.txt.

Objectives

Katas are about trying something many times. In this one, what were experimenting with is not just the code, but the heuristics of processing the text. What do we do with punctuation? Paragraphs? Do we have to implement backtracking if we chose a next word that turns out to be a dead end?

Ill fire the signal and the fun will commence...

Sample:

name: languages
Acharya, Madhumita: SQL, shell
Anderson, Paul G: basic, python, C++
Baumel, Bradley I: java, sql, python, html
Bearer, Jerry: bash, sql, asp, html
Briant, Paul S: python, html, php, sql, java
Casey, Paul A: C++, python,
Change, Simbarashe P: SQL
Chavis, Brandon: ruby, python, bash
Cowhey, Isaac: python, javascript, ruby, scala
Galande, Nachiket: R, java, php
He, Beatrice: SQL, R, SaS, C, shell
Hefner, Jack M: python, perl, C++
Hicks, Josh: C#, bash , perl
Hollis, Adam: python, basic, html, actionscript, php
Latif, Shu A: python, bash, html
Mandava, Sasi: C, C++
McGhin, Spencer G: python, sql, R, html
Muralidharan, Sharmila: cobol, sql,
Naik, Ninad: C++, python
Pena, Sheree: swift
Raina, Jay N: SQL, R, python
Robison, Charles E: SQL, python,
Silva, Enrique R: python, sql
Tobey, David E: bash, qbasic, sql
Truong, Alexander C: pyton , htl, xml
Vosper, Paul: C#, macscript, python
Weidner, Matthew T: German, python, html
Williams, Marcus D: python
Wong, Darryl: perl, php
Yang, Minghao: None
Hagi, Abdishu: python, bash

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

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Does it avoid using personal pronouns (such as I and me)?

Answered: 1 week ago

Question

2. To compare the costs of alternative training programs.

Answered: 1 week ago