Python experimental writing, Random sentence generator generation

Source: Internet
Author: User

Question 1-random sentence Generator-7 marks

Write a program This reads in files of words and produces random but structurally correct 中文版

Sentences, according to the syntax (rules of grammar) specified on the next page.

Here are the output of a demo program. These sentences is correctly structured 中文版 but (usually) make

Absolutely no sense (but is often fun:-)

It is commonly believed that your ghastly mortician deliberately befriends my annoying bulb.

Lo! Our green lecturer often hibernates.

This foreign burglar usually cooks and ate the flesh of her inconceivable colour.

Indeed, the orbiting boy inexorably recognizes this half-hidden monstrosity.

My vaporized model knowingly reaches an understanding with his dainty terror.

Additional Data files:

? Randomsentencewordlists.zip on Stream have files for each class of Word,

e.g. Nouns.txt, Adjectives.txt, ...

? Important:put the files from Wordlists.zip to the same folder as your Python file.

Commands

Make the following commands available using a menu and prompt for a command. The commands can be

Upper or lower case (e.g. E or e) or either. It ' ll be easier if implement the commands in order (L first

...)

CMD Effect

L Load All the files of words from disk.

T Test–display The first word from each list to make sure they ' ve been loaded.

E Easy Sentence:display A, word sentence-a randomly selected noun followed

By a randomly selected intransitive verb and then a full stop.

S New Sentence-generate & display a sentence conforming to the grammar defined

The ' use ' of this grammar to make your sentences ' sections on the next page.

If the Wordlists haven ' t been loaded, display an error message.

Q Quit the program.

For full marks

? The sentences should be capitalised correctly and has a trailing full stop.

? The sentences generated by the ' S ' command should conform to the specified grammar

? Meaningful variable names should be used & any complex parts should being commented

The explanation on the next page was a little wordy but the idea underlying is simple–you randomly select

Words from appropriate word lists and combine them so they ' re correct 中文版 sentences.

159.171–assignment 3 1? Giovanni Moretti 2017

Defining the Structure of sentences

We all know and sounds right as a sentence, but to create correct sentences using a

Be more precise.

Below, I ' ve defined a grammar that specifies the syntax (i.e. the structure) of valid tiny sentences.

What is the symbols mean when defining a grammar:

? :: = is read as "is defined as".

? Square brackets [] around an item mean that item is optional (it can be omitted).

? The Vertical bar | means or (choose the item to the left OR the item right)

? Angle brackets (e.g. < >) around an item means it's a placeholder that'll be replaced later.

Example:to illustrate how a grammar can is defined and used, say we have four named lists of

words, (e.g <Noun> is the name of the first group)

<Noun-marker> our, my, the

<Adjective> Red, huge, slimy

<Noun> cat, Fire, Sky

<Verb> eats, climbs, digs up

And the following rule defines valid noun-phrases:

<Noun-Phrase>:: = [<Noun-marker>] [<Adjective>] <Noun>

Square brackets around an item means it's optional, so both the noun-marker and adjective can is

Omitted. Therefore the following five noun phrases all comply with the above Noun-phrase definition:

1) Cat 2) Our Fire 3) we slimy Sky (4) Huge Fire (5) My red cat

Just for this example, let the definition for sentence be:

Sentence:: = noun-phrase Verb noun-phrase

The red cat eats our slimy sky

Our sky climbs, the huge fire

My cat digs up the red sky

Capitalise the first word & Add a full stop, and each of these three lines are a silly but valid sentence.

Use this grammar to make your sentences

The following grammar defines the structure of a sentence for your answer to this assignment question.

<sentence>::= [<lead-in>] <Noun-Phrase> [<adverb>] <Verb-Phrase>.

Read this as: "A sentence is defined as a lead-in (which, because was surrounded by square brackets, is

Optional) followed by a noun-phrase, an optional adverb, then a verb-phrase and a full stop ".

<Noun-Phrase>:: = <Noun-marker> [<Adjective>] <Noun>

<Verb-Phrase>:: = <Intransitive-verb> | <Transitive-verb> <Noun-phrase>

is read as: "A verb phrase can be a intransitive verb OR A transitive-verb1

followed by a noun phrase "

Where <Noun> means any line from the Nouns.txt file, and likewise for the others (e.g. <Adjectives> ...)

1

Transitive-verbs take an object (there must is a noun after the verb) e.g. the cat bounced the ball

159.171–assignment 3 2? Giovanni Moretti 2017

Suggestions

? Optional items are included 50% of the time. To control this, you can use

The Random.random () function which returns a real number somewhere between 0 and 1, or

Random.choice ([' Heads ', ' tails '])-try it and see what it does

Don ' t forget to import the random module

? There is many ways to build the sentence:

? You can print out each word as you go. This method was simple and has the limitation.

You never has the complete sentence as a string.

? You can build up the sentence by making a string that gets longer as you add the next part

of the sentence:

e.g. S = a random selection from Leadin

s = s + random selection from Nounmarker # and keep adding

? You could add the pieces to a list and then join them together using ". Join (Your-list)

? Your program would have a cleaner structure and being easier to follow if you create functions for

<noun-phrase> <verb-phrase>, and possibly <sentence> as well.

? You may find s.capitalize () useful.

Possible Extensions

This is the fun program and there is lots of the-you could extend it. You could:

? Remember a noun from one sentence and reusing it in the next sentences

? There ' s a conjunction wordlist (e.g. and) which isn ' t currently used. You could make your program

Create paragraphs by creating multiple sentences joined with conjunctions, possibly repeating the

Nouns to provide some continuity

? Make it remember all the sentences created in a list.

? Add a favourites command which adds the most recently created sentence to a list of favourites.

159.171–assignment 3 3? Giovanni Moretti 2017

Question 2–an addressbook–7 Marks

Using a dictionary indexed by nickname (a short favourite name), write a simple address book that

Lets you save these contact details.

? Nickname (can be anything)

? Name

? Address

? Phone-no

Each dictionary entry contains a dictionary of the details of

names[' Tom ' # would return the dictionary that contains all of Tom's details,

and

contacts = names[' Tom ']

Address = contacts[' address ']

or simply

Address = contacts[' Tom ' [' Address ']

Would return Tom ' s address

The program first displays a menu (something like that shown below) and carries out the appropriate

Action depending on which letter the user types, and then redisplays the menu:

My Contacts * * *

F–find

A–add new Entry

D–delete

L–list All

Q–quit

Command:?

Add prompt separately for each of the name, address, Phone-no and nickname. Then save all of these

Fields into a data structure. A dictionary is recommended but a list would also work.

Find prompt for a nickname, then search for the name, address and phone number of the person

With the nickname in your addressbook and display their details. Make the search is caseinsensitive

i.e. only the letters matter, not whether they ' re upper or lower case

Delete Prompt for a nickname, then find and display the related entry. Ask the user if this is the

Correct one to be deleted. If they reply "yes" delete it.

List all as expected list all the entries, numbered sequentially. Display all the fields and format them

for easy reading. Some possibilities is shown below.

Across

Nick Name Address Phone No

1 Sue Sue Williams 104 Broadway, Pnth 021-333-5555

2 Bob Robert Levine Fitzherbert Ave 06-355-6666

Down

1 Sue

Sue Williams

Broadway, Pnth

021-333-5555

2 Bob

Robert Levine

104 Fitzherbert Ave

06-355-6666

159.171–assignment 3 4? Giovanni Moretti 2017

Does the name exist?

When adding entries, as soon as the nickname are given, check to see if that nickname in the AddressBook.

If so, ask if the new entry should replace the existing one for that nickname. If the answer is no and then

Prompt again for a new nickname. To abort the Add command (and Find/delete), simply enter an empty

String for the nickname.

Be sure to a appropriate message if the user tries to find or delete a entry and the nickname isn ' t in use.

Important:make sure that's functions, usually one for each of the commands. You can of

Course, create any additional functions this you think would help to simplify or clarify your code.

The addressbook-getting Started:

1. Get the menu going and then create a function for each of List-all, add, ... Initially, simply put a

Print inside each function, e.g. for the AddEntry function, put print ("You called ADD"). You can

Test Command–they ' ll simply print out a message.

2. Using an assignment statement, manually create a tiny addressbook containing a or three

Entries

3. Get the List-all command going. You can use this to view your manually created AddressBook and

Effects of the later commands

4. Then work on Add, find and delete

Optional (You can get full marks without doing this): you could try adding a s (search) command that

Searches for and displays all entries that has the search string in any of the fields (not just the nickname).

What and how to submit

How:all submissions must is via Stream (not email) using the Assignment 3 submission link.

What:submit your Python programs, each named with. py extensions. You should are three. py files to

Submit, one for each question.

Do not submit Word documents (. doc or. docx) or. zip files.

Check that:

? All programs should display your name and ID number when starting.

? That your files has a. py extension

159.171–assignment 3 5? Giovanni Moretti 2017http://www.6daixie.com/contents/3/1297.html

The core staff of the team mainly include Silicon Valley engineers, bat front-line engineers, domestic TOP5 master, PhD students, proficient in German English! Our main business scope is to do programming big homework, curriculum design and so on.

Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:

C/c++/c# Write

Java Write generation

It generation

Python writes

Tutoring Programming Jobs

The MATLAB Generation writes

Haskell writes

Processing Write

Linux Environment Setup

Rust Generation Write

Data Structure assginment Data structure generation

MIPS Generation Writing

Machine Learning Job Writing

Oracle/sql/postgresql/pig database Generation/Generation/Coaching

Web development, Web development, Web site jobs

Asp. NET Web site development

Finance insurace Statistics Statistics, regression, iteration

Prolog write

Computer Computational Method Generation

Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected]

: Codinghelp

Python experimental writing, Random sentence generator generation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.