首页
编程语言
数据库
网络开发
Algorithm算法
移动开发
系统相关
金融统计
人工智能
其他
首页
>
> 详细
python辅导、Random Sentence Generator辅导 解析R语言编程|讲解R
Question 1 - Random Sentence Generator - 7 marks
Write a program that reads in files of words and produces random but structurally correct English
sentences, according to the syntax (rules of grammar) specified on the next page.
Here is the output of a demo program. These sentences are correctly structured English but (usually) make
absolutely no sense (but are 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 has files for each class of word,
e.g. Nouns.txt, Adjectives.txt, …
? Important: put the files from Wordlists.zip into 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 you 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 two 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
in the 'Use this grammar to make your sentences' section 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 have 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 be commented
The explanation on the next page is a little wordy but the idea underlying is simple – you randomly select
words from appropriate word lists and combine them so they're correct English sentences.
159.171 – Assignment 3 1 ? Giovanni Moretti 2017Defining the Structure of Sentences
We all know what sounds right as a sentence, but to create correct sentences using a program, we have to
be more precise.
Below, I've defined a grammar that specifies the syntax (i.e. the structure) of valid tiny sentences.
What 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 will be replaced later.
EXAMPLE: to illustrate how a grammar can be defined and used, say we have four named lists of
words, (e.g.
is the name of the first group)
our, my, the
red, huge, slimy
cat, fire, sky
eats, climbs, digs up
and the following rule defines valid Noun-Phrases:
::= [
] [
]
Square brackets around an item means it's optional, so both the Noun-marker and Adjective can be
omitted. Therefore the following five noun phrases all comply with the above Noun-Phrase definition:
1) cat 2) our fire 3) our 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 is 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.
::= [
]
[
]
.
Read this as: “a sentence is defined as a Lead-in (which, because is surrounded by square brackets, is
optional) followed by a Noun-phrase, an optional Adverb, then a Verb-Phrase and a full stop”.
::=
[
]
::=
|
is read as: "A verb phrase can be an intransitive verb OR a transitive-verb1
followed by a noun phrase"
Where
means any line from the Nouns.txt file, and likewise for the others (e.g.
...)
1
transitive-verbs take an object (there must be a noun after the verb) e.g. the cat bounced the ball
159.171 – Assignment 3 2 ? Giovanni Moretti 2017Suggestions
? 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 are many ways to build the sentence:
? you can print out each word as you go. This method is simple but has the limitation that
you never have 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 will have a cleaner structure and be easier to follow if you create functions for
and
, and possibly
as well.
? You may find s.capitalize() useful.
Possible extensions
This is fun program and there are lots of way 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 2017Question 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 you like)
? name
? address
? phone-no
Each dictionary entry contains a dictionary of the details of that person, so
names['Tom'] # will return the dictionary that contains all of Tom's details,
and
contacts = names['Tom"]
address = contacts['address']
or simply
address = contacts['Tom']['address']
will 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 name, address, phone-no and nickname. Then save all of these
fields into a data structure. A dictionary is recommended but a list will also work.
find prompt for a nickname, then search for the name, address and phone number of the person
with this 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 are shown below.
Across
Nick Name Address Phone No
1 sue Sue Williams 104 Broadway, Pnth 021-333-5555
2 bob Robert Levine 25 Fitzherbert Ave 06-355-6666
Down
1 sue
Sue Williams
25 Broadway, PNth
021-333-5555
2 bob
Robert Levine
104 Fitzherbert Ave
06-355-6666
159.171 – Assignment 3 4 ? Giovanni Moretti 2017Does the name exist?
When adding entries, as soon as the nickname is 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, 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 an appropriate message if the user tries to find or delete an entry and the nickname isn't in use.
IMPORTANT: make sure that you use functions, usually one for each of the commands. You can of
course, create any additional functions that you think will 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 each command – they'll simply print out a message.
2. using an assignment statement, manually create a tiny addressbook containing a two 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 have the search string in any of the fields (not just the nickname).
What and how to submit
How: ALL submissions must be via Stream (not email) using the Assignment 3 submission link.
What: Submit your Python programs, each named with .py extensions. You should have 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 have a .py extension
159.171 – Assignment 3 5 ? Giovanni Moretti 2017
联系我们
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-21:00
微信:codinghelp
热点文章
更多
辅导 comm2000 creating socia...
2026-01-08
讲解 isen1000 – introductio...
2026-01-08
讲解 cme213 radix sort讲解 c...
2026-01-08
辅导 csc370 database讲解 迭代
2026-01-08
讲解 ca2401 a list of colleg...
2026-01-08
讲解 nfe2140 midi scale play...
2026-01-08
讲解 ca2401 the universal li...
2026-01-08
辅导 engg7302 advanced compu...
2026-01-08
辅导 comp331/557 – class te...
2026-01-08
讲解 soft2412 comp9412 exam辅...
2026-01-08
讲解 scenario # 1 honesty讲解...
2026-01-08
讲解 002499 accounting infor...
2026-01-08
讲解 comp9313 2021t3 project...
2026-01-08
讲解 stat1201 analysis of sc...
2026-01-08
辅导 stat5611: statistical m...
2026-01-08
辅导 mth2010-mth2015 - multi...
2026-01-08
辅导 eeet2387 switched mode ...
2026-01-08
讲解 an online payment servi...
2026-01-08
讲解 textfilter辅导 r语言
2026-01-08
讲解 rutgers ece 434 linux o...
2026-01-08
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!