159171 Assignment 3 Submit via Stream 14% towards final grade. Due date: Sunday June 2, XXXXXXXXXX:55 Part 1: A Random Sentence generator (8 marks) Write a program that reads in files of words and...

Hey would really appreciate it if someone could help do this assignment for me. Having difficulty doing this and if also could add how They did it too would be appreciated. Thanks


159171 Assignment 3 Submit via Stream 14% towards final grade. Due date: Sunday June 2, 2019 23:55 Part 1: A Random Sentence generator (8 marks) Write a program that reads in files of words and produces random but structurally correct English sentences, according to the syntax (rules of gram- mar) specified on the next page. Here is the output of a demo program. These sentences weren’t selected— they were the first six produced: Goodness gracious me: my darkness ignores my burden. Our slimy old hag laughed each spotty germ. Her unfathomed lecturer knowingly exploded the molten crane. Her false abnormality feverishly views the hideous offspring of the pet snake. Our magnificent woobly bits rises from the depths each canyon. It’s a fact, my terrible monolith secrets away the awful knowledge of the building. The following files each class of word are on Stream under “Assignment 3 Resources”. Adjectives.txt Adverbs.txt Conjunctions.txt IntransitiveVerbs.txt Leadin.txt NounMarkers.txt Nouns.txt TransitiveVerbs.txt Each file has a list of sample words for its corresponding word class. Put these files in the same folder/directory as your Python program. Commands Make the following commands available using a menu and prompt for a command. The commands can be presented in either upper or lower case but should work for either input (e.g. typing e or E should work). It’ll be easier if you implement the commands in order (L first etc . . . ) • 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 and display a sentence conforming to the grammar defined in the Grammatical Definition of the Sentences sec- tion on the next page. If the wordlists haven’t been loaded, display an error message. • Q: quit the program Defining the Structure of Sentences The explanation given here is a little wordy but the idea underlying is simple— you randomly select words from various lists and combine them so they’re correct English sentences. We all know what sounds right as a sentence, but to create correct sentences using a program, we have to be more precise. We need to define a grammar that specifies the syntax (i.e. the structure) of valid sentences. What the symbols mean when defining a grammar: • ::= is to be read as “is defined as”. • Square brackets [ ] around an item mean the item is optional—it may be included or omitted. • the vertical bar | means OR (choose either the item to the left or right of the |) • Angle brackets (e.g. <> ) around an item means it is defined in more detail later Grammatical Definition of the Sentences The following grammar defines the structure of a sentence. ::= [] [] . Read this as “a sentence is defined as a Lead-in (which, because is sur- rounded by square brackets, is optional) followed by a noun phrase, an adverb (also optional), then a verb phrase and a full stop”. ::= [ ] Include optional items (eg adjectives) 50% of the time ::= | is read as: “A verb phrase can be an intransitive verb1 OR a transitive-verb2 followed by a noun phrase”. Your program should at least work for the above sentence grammar rule. You can transfer the logic in this grammar directly into Python code. 1an intransitive verb does not take an object (i.e. there’s no noun after the verb) e.g. the boy laughed 2a transitive verb takes an object (there must be a noun after the verb) e.g. the cat bounced the ball Possible Extensions This is fun program and there are lots of way you could extend it. You could: • make your program work for any grammar rule—ie, it can intelli- gently process any grammar string that is input to it. Doing this will require you to really put into practice your computational skills that you have learned. • remember a noun from one sentence and reusing it in the next sen- tences • there’s a conjunction wordlist (e.g. and) which isn’t currently used. You could make your program create paragraphs by creating multi- ple 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 sen- tence to a list of favourites. Note: the extensions mentioned above are not actually required for this assignment. However, for full marks your program should at least do the following: • 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 and any complex parts should be commented Part 2: A Picture Framer (6 marks) Using the functions from the cImage module, write a program that will frame a picture by augmenting the input image with a border of given width. The framed image should fully preserve the input image without overwriting any parts of the input image as in the following example: The image on the left is the original input image. The image on the right is the image with a 20 pixel wide magenta frame drawn around it. Note all features of the original image are preserved—nothing is overwritten. Therefore the output image is a little larger than the input image according the width of the frame. Your program should provide a user interface availabe as a menu, as can be summarized in this menu string: *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > The commands should be case insensitive and work as follows: q exit the program f select an image file from a directory listing of gif files in the image folder/directory. This would work in a simaliar manner to the “file chooser” application described in the lectures. Your program should only display gif files. c add a new colour to a pre-defined colour map dictionary. In response to this command, your program should prompt for the colour name and then the R, G, and B. p pick a colour from the pre-defined colourmap that is implemented as a python dictionary. This dictionary will map a named colour to a set of R, G, and B values. Here are some standard colour names and values that you can pre-load: colour name R G B black 0 0 0 white 255 255 255 red 255 0 0 green 0 255 0 blue 0 0 255 yellow 255 255 0 cyan 0 255 255 magenta 255 0 255 silver 192 192 192 brown 162 42 42 orange 255 165 0 d display a framed image of the currently selected image with the frame drawn in the currently selected colour. In response to this command, your program should prompt for the width of the frame border, read the image, then display the framed version of the image. This will involve some internal processing. Upon clicking on the image, the display window should close and your program returns to the menu. You can find more colour names and their RGB values here: https://www.rapidtables.com/web/color/RGB_Color.html. Feel free to use whichever ones you like. https://www.rapidtables.com/web/color/RGB_Color.html For this program, use exception handling rather than try and allow for all possible sources of run-time errors. Set up your handler so that if some run- time problem occurs, note the error and its causes, and then return to the menu without terminating the program. You need to decide which block of code to protect with the try...except... construct. Additional notes: • You can use the listdir() function of the os module to get a list of files in a given directory. • When drawing a display window, rather than using the default size, you can fix the dimensions by passing through width and height as named parameters, eg: win = cImage.ImageWin(width=1000, height=800) • To create an empty image (on to which you can write) use EmptyImage(), eg: new_width = 1000 new_height= 800 newimg = cImage.EmptyImage(new_width, new_height) Here is a sample interactive session Selected: images/angry.gif red *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > f These image files are visible: 0 : angry.gif 1 : lena.gif 2 : flower.gif 3 : dachshund.gif Select the file by number > 2 Selected: images/flower.gif red *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > d Enter the frame width > 20 An image of the flower with a red border 20 pixels wide should be displayed. Click on the image to return to the menu. *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > c These colours are available red : 255 0 0 green : 0 255 0 blue : 0 0 255 yellow : 255 255 0 white : 255 255 255 black : 0 0 0 cyan : 0 255 255 magenta : 255 0 255 silver : 192 192 192 maroon : 128 0 0 brown : 162 42 42 orange : 255 165 0 Colour name > sienna Enter R, G, and B > 160 82 45 Selected: images/flower.gif red *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > p These colours are available red : 255 0 0 green : 0 255 0 blue : 0 0 255 yellow : 255 255 0 white : 255 255 255 black : 0 0 0 cyan : 0 255 255 magenta : 255 0 255 silver : 192 192 192 maroon : 128 0 0 brown : 162 42 42 orange : 255 165 0 sienna : 160 82 45 Enter the name of the colour > sienna Selected: images/flower.gif sienna *** Image Framing Program *** q - quit f - select an image file c - add a new colour p - pick a colour d - display a framed image for the file Enter a command > d Enter the frame width > 20 The flower image should now be displayed with a border of the new colour added to the colour map. Click on the
May 27, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here