Transcribed TextTranscribed Text

Lab 1 home assignment: Semantics in programming languages: Sets and relations This homework is split into two parts. The first part is a follow-up to the predicate logic and the sets and relations of the pet examples in the class assignment. The second part relates to working on some corpus data. While working on the assignment, it is a good idea to download the notebook from time to time, so that you do not lose your work, if something goes wrong. Remember that you can ask for help on the Moodle discussion forum, as well as visit Panagiotis during the specified office hours. Discussions between student colleagues is encouraged, but always implement your own solutions and do not copy your friend's answers. Have fun and good luck! Task 1. Pets and food This first task (Pets and food) consists of six subtasks a - f. Each subtask is worth 1/2 of a point (= 0.5 points), and you can get up to 3 points for the task as a whole. In this task we return to our lovely pets, so we need to repeat some of the database of "world knowledge" that we have: In [18] : pets = { "Nasse", "Petya", "Maija", "Michelangelo", "Kostya", "Mansikki", "Anders", "Leonarco", "Rosa", "Monty", "Ronja", "Lady", "Mosya", "3ella", "Donatello", "Eloveena" } animals = { "dog", "cat", "cow", "duck", "snake", "turtle", "pig" } species of pets = { "Nasse" "pig", "?etya": "oog", "Maija": "cat", "Michelangelo" "turtle", "Kostya" : "cat", "Mansikki": "cow", "Anders": "duck", "Leonardo": "turtle", "3osa" : "cow", "Monty" : "snake", "Ronja": "dog", "Lady": "oog", "Mosya": "cat", "3ella": "dog", "Donatello": "turtle", "Eloveena": "cow" } (Remember to hit Ctrl-Enter in every code cell, because otherwise Python will not assign the values to the variables, and your program will fail later on.) Now it is your turn to enter some more "world knowledge", namely the favorite foods of different animals. First, list the foods in a set variable, called foods - Then create relations from animal species to the foods in the dict variable favorite_food.s_of_animals - So, if you add "fish" as one food, you might need to define that the favorite food of cats is fish. 1 a) In [19] : # First list some foods here: # foods = { "marmelade", "apples", "oranges", "milkshakes", "cat food.") H Then create a mapping (relation), from animal species (such as "cat") # to foods (such as "fish") Pay attention that you use the correct type of # punctuation and brackets: it all matters to Python! # favorite foods of animals = { "dog": "cat food", "cat": "milkshakes", "cow": "apples", " duck": "apples", "snake": "cat food.", "turtle": "oranges", "pig": "marmelade") Next you want to brag about how knowledgeable you are about the food preferences of animals, so you need to write program code that accesses your "database" and prints for every animal species what food it likes. Note that you are not allowed to hard-code any answer such as writing print ("A cat likes fish") , but you need to make use of for loops and variables to have your program figure out the information from the database. 1b) In [23]: H Write Python code here that iterates over all animal species and prints # what foods they like, such as: # A cat likes fish # A dog likes # etc. for animal in animals: print (animal, "likes", favorite_foods_of_animals[animal]) dog likes cat food pig likes marmelade duck likes apples turtle likes oranges snake likes cat food cat likes milkshakes COW likes apples To make the whole thing a bit more interesting, the next step is to have your program figure out not just what food some animal species in general likes, but what food all our pets prefer. So, you need to find out what the favorite foods of Nasse, Petya, Maija and the other pets are. For instance, since Maija is a cat, we can conclude that Maija likes fish (if fish is the favorite food of cats in your world). 1 c) In [21] # Write Python code here (using for loops and variables) that prints H for every pet what its favorite food is, such as: H Maija loves fish # Mansikki loves # etc. for pet in pets: pet=species_of_pets[pet - print (pet, "loves", favorite food of animals [pet]) HI have struggled with this one, for some reason I could not connect the names with species dog loves cat food. dog loves cat food. turtle loves oranges duck loves apples cat loves milkshakes COW loves apples COW loves apples COW loves apples turtle loves oranges snake loves cat food dog loves cat food pig loves marmelade dog loves cat food cat loves milkshakes cat loves milkshakes turtle loves oranges Next, you need to define how much food each animal species needs a day. Create a mapping from animal species to the amount of food in grams that they eat per day. For instance, a cat might need 230 grams of food a day: 1 d) In [23]: Define for each animal species (such as cat), how much food in grams it # needs to be fed a day: # daily rations for animals = "dog": "500 gramms", "cat" "230 gramms", "cow" "3000 gramms", - "óuck": "1000 gramms", "snake" : "10 gramms", "turtle" "15 gramms", "pig" "5000 gramms" } Assuming that each pet only eats its favorite food, print how much it eats of what food every day: 1 e) In [ ] : H Write code here that prints for each pet how much it eats of what food H every day, such as: H Maija eats 230 grams of fish every day. H Mansikki eats grams of every day. H Lady eats grams of every day. H etc. Finally, write Python code that calculates how much food you need a day to feed all our pets: 1f) In [ ]: # Write code here that calculates one single number, which is # the weight of all food combined that is needed to feed all the # pets: Nasse, Petya, Maija, etc. H Then print the result: amount=0 for animal in c.aily_rations_for_animals: print ("You need.", amount, "grams of food every day to feed all the pets. ") Task 2. Working with text corpora The toy examples with imaginary pets might be entertaining, but what about doing something more useful from a linguistic point of view? Next, let us work on text data from two novels that are included in NLTK, the Natural Language Toolkit Python package. This task is divided into three subtasks a - c, each worth 1/3 of a point (0.33 points). Task 2 as a whole is thus worth 1 point maximum. In the first code cell below we import the data to work with: In [ ] : from import textl as moby dick, text2 as sense and sensibility You don't need to worry about this code cell too much, but here is what it does in a nutshell: from tells us from which package we want to get data. We have installed the nitkpackage for you in the background so just take its existence for granted. After import we list which pieces of data we want to import, and because the data have non-descriptive names (text1, text2), we also rename them using the as keyword. The two texts we import are Herman Melville's Moby Dick and Jane Austen's Sense and Sensibility. As a result of the importing we have at our disposal two entire novels, represented as lists of word tokens. These lists are contained in the variables moby dic and sense_ano._sensibility In the second code cell below we print out the first few word tokens of the texts to make sure we got the right data. Remember, if you restart the notebook (kernel), you need to import the texts again. In [ ]: print ("Sense and. Sensibility starts like this: ") print (sense_ano._sensibility[0:30]) print ("Moby Dick starts like this:") print (moby dick [0:30]) As you may have noticed, we have introduced a new data type: the list. A list is like a set, but unlike the set, the list can contain the same element multiple times, and in the list the order of the elements is preserved. For instance: In[]: my_set = { "Nasse", "Nasse", "Maija", "Nasse", "Petya" } curly brackets are used with sets my list = "Nasse", "Nasse", "Maija", "Nasse", "?etya" ] square brackets are used with lists print ("My set is:", my_set) print ("My list is:",my list) You can extract the n th element from a list by putting the number in square brackets after the list variable. However, the numbering starts from zero, so the n th element is actually in position n - 1: In [ J: print ("The first element of my list is:", my list [0]) print ("The second element of my list is:". my list[1]) print ("The thiro element of my list is:", my_list[2]) You can also extract a sublist (called a slice) from a list. Then you put two positions separate by comma: the first number tells Python where to start, and the second number where to end: In [ ] : print ("The three first elements of y list are:", my list 0:3]) print ("The two elements starting at the third position of my_list are:", my list [2:4]) Note that the end point is notincluded in the sublist! If we ask for my list [0:3] , we will get a slice containing [ my list my my ] The slice does not contain my list [3] - By now, you should understand what print(sense_ano._sensibility[0:30]) does. The whole novel is stored in a list consisting of word tokens. We take a slice that starts at position 0 and ends before position 30. That is, we print the 30 first word tokens of Sense and Sensibility. Next, it is your turn to extract sentences from the novels. 2a) In[]: Write code here that prints 5 word tokens from Moby Dick starting at the 38th word token: Then write code here that prints 38 word tokens starting at the 271th word token of Sense and Sensibility: You can retrieve the number of elements in a list using the len () function, just as with sets. You can also iterate through a list using the for command: In [ ] : print ("my_] list contains", "elements. ") print ("The elements are, each printed on a line of its own: for x in my_list: print (x) A list can be converted to a set, using the set () keyword. This means that all duplicates are dropped from the list and the order of the elements becomes undefined: In III: nysecono.set = set (my list) print (":my second set contains", len(my_secono_set), "elements.") print ("The elements are, each printed on a line of its own: for x in mysecond set: print (x) When talking about text corpora, the word tokens, or the flow of text, is best represented using a list. However, if we want a collection of all unique words, so-called word types, then we are working with a set, in which every word occurs only once. Next, you will need to compute the number of word tokens and word types in both novels: 2 b) In [ ] : # Replace the zeros below with your actual calculations print ("The number of word tokens in Moby Dick is", 0) # Hint: the correct answer is 260819 print ("The number of word types in Moby Dick is", 0) # Hint: the correct answer is 19317 print ("The number of word tokens in Sense and Sensibility is", 0) # Hint: no more hints given! print ("The number of word types in Sense and Sensibility is", 0) Sometimes it is useful to know how many times a particular word occurs in a corpus. Now create a for floop that iterates over Sense and Sensibility and counts how many times the word "really" occurs in the novel: 2c) In [ 1: # Count the occurrences of "really" in "Sense and Sensibility" H using a for loop: That is all for now. Download this page as a Notebook (.ipynb) file and submit on Moodle. If you are interested to know more about this topic, an additional source of information is the NLTK book. Chapter 1 of the book Natural Language Processing with Python covers a lot of additional material as well, so be selective if you decide to read it. Most relevant for this lab are the sections 1.4, 2 and 4.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

    By purchasing this solution you'll be able to access the following files:
    Solution.ipynb and Solution.pdf.

    for this solution

    or FREE if you
    register a new account!

    PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

    Find A Tutor

    View available Python Programming Tutors

    Get College Homework Help.

    Are you sure you don't want to upload any files?

    Fast tutor response requires as much info as possible.

    Upload a file
    Continue without uploading

    We couldn't find that subject.
    Please select the best match from the list below.

    We'll send you an email right away. If it's not in your inbox, check your spam folder.

    • 1
    • 2
    • 3
    Live Chats