public:t-malv-15-3:1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
public:t-malv-15-3:1 [2018/08/15 21:58] – Hiding orvark | public:t-malv-15-3:1 [2024/04/29 13:33] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Lab 1 ====== | ====== Lab 1 ====== | ||
+ | **Hand in your code in MySchool before midnight today (20 August). A single .py file containing the code in the same order as the given problems.** You can use File-> | ||
+ | |||
+ | If you can't manage to complete a particular problem please hand in your incomplete code -- comment it out if it produces an error. | ||
+ | |||
+ | ===== 1. Getting to know some helpful functions ===== | ||
+ | |||
+ | <code python> | ||
+ | #Use dir() to see the names that exist in the current scope. | ||
+ | |||
+ | dir() | ||
+ | |||
+ | #You can use help() to see what dir does. | ||
+ | |||
+ | help(dir) | ||
+ | |||
+ | #Now define three variables and a list. Feel free to change the values: | ||
+ | |||
+ | my_str = "This is an ordinary string" | ||
+ | my_int = 5 | ||
+ | my_float = 4.6 | ||
+ | my_list = [' | ||
+ | |||
+ | #Use dir() again. Has anything changed? | ||
+ | |||
+ | #Use type() to see the type of each. | ||
+ | |||
+ | type(my_str) | ||
... | ... | ||
+ | |||
+ | #Now use dir on the four types you defined. | ||
+ | |||
+ | dir(my_str) | ||
+ | ... | ||
+ | |||
+ | #Most of the names are functions that can be applied to the types. | ||
+ | #For example dir(my_str) lists ' | ||
+ | |||
+ | my_str.upper() | ||
+ | |||
+ | #You can use help to see what each function does: | ||
+ | |||
+ | help(my_str.upper) | ||
+ | |||
+ | #Use dir and help to select one function to apply to each of the variables | ||
+ | #and the list. | ||
+ | |||
+ | </ | ||
+ | |||
+ | **Return the your code for applying the four functions you selected in the last part.** | ||
+ | Note to use print(my_...) to show the change the function made. | ||
+ | |||
+ | |||
+ | ===== 2. Naive is_male ===== | ||
+ | |||
+ | <code python> | ||
+ | #Define a very simple and naive function to check if an Icelandic proper name belongs to a male | ||
+ | #(e.g. ends with " | ||
+ | |||
+ | def is_male(proper_name): | ||
+ | return #add code here | ||
+ | </ | ||
+ | |||
+ | Example usage: | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | True | ||
+ | </ | ||
+ | |||
+ | **Can you improve the function so it handles some of the false positives in the example above?** | ||
+ | |||
+ | ===== 3. Replace bad with good ===== | ||
+ | |||
+ | Define a function that takes a string (text), list (bad_list) and an optional string (good_str) as arguments. It should return the text-string where all occurances of the string items on the bad-list-list have been replaced by the good-string. | ||
+ | |||
+ | <code python> | ||
+ | def str_replace(text, | ||
+ | #add code here | ||
+ | return text | ||
+ | </ | ||
+ | |||
+ | Example usage: | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | ===== 4. NLTK functions ===== | ||
+ | |||
+ | Before you can get started with the NLTK corpora you have download it with nltk.download() once. | ||
+ | |||
+ | <code python> | ||
+ | import nltk | ||
+ | </ | ||
+ | |||
+ | Apply NLTK functions to do the following: | ||
+ | |||
+ | * Import text6 from nltk | ||
+ | * Show the concordance of the word " | ||
+ | * Find words occuring in similar contexts to " | ||
+ | * Find the collocations in text6 | ||
+ | |||
+ | ===== 5. NLTK coding ===== | ||
+ | |||
+ | Write code to do the following with the NLTK: | ||
+ | |||
+ | * List all words starting with ' | ||
+ | * List all uppercase words in text6 (problem 23 [[http:// | ||
+ | |||
+ | ===== 6. A dictionary of rules ===== | ||
+ | |||
+ | You are given a dictionary (string: | ||
+ | |||
+ | <code python> | ||
+ | rules = {" | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | |||
+ | #Add code to add the verb " | ||
+ | |||
+ | #Add code to add the nouns " | ||
+ | |||
+ | #Add code to print out the rules giving the following output. | ||
+ | |||
+ | </ | ||
+ | //Hint: items() [[https:// | ||
+ | |||
+ | Expected output (the order of the rules does not matter): | ||
+ | |||
+ | < | ||
+ | N -> boy | girl | dog | cat | ||
+ | S -> NP VP | ||
+ | NP -> Det N | Adj NP | ||
+ | Adj -> big | small | ||
+ | Det -> a | the | ||
+ | VP -> V NP | ||
+ | V -> sees | likes | hates | ||
+ | </ | ||
+ | |||
+ | ===== Possible Solutions ===== | ||
+ | |||
+ | <code python> | ||
+ | #1 | ||
+ | my_str.upper() | ||
+ | my_int.str() | ||
+ | my_float.is_integer() | ||
+ | my_list.pop(2) | ||
+ | |||
+ | #2 | ||
+ | def is_male(proper_name): | ||
+ | return proper_name[-3: | ||
+ | #return proper_name.endswith(" | ||
+ | |||
+ | # return ... and ' ' in proper_name and proper_name.istitle() and " | ||
+ | # return ... and proper_name.find(' | ||
+ | |||
+ | #3 | ||
+ | def str_replace(text, | ||
+ | for bad in bad_list: | ||
+ | text = text.replace(bad, | ||
+ | return text | ||
+ | |||
+ | #4 | ||
+ | from nltk.book import text6 | ||
+ | text6.concordance(" | ||
+ | text6.similar(" | ||
+ | text6.collocations() | ||
+ | |||
+ | #5 | ||
+ | sorted(w for w in set(text6) if w.startswith(' | ||
+ | sorted(set(w for w in text6 if w.isupper()))) | ||
+ | |||
+ | #6 | ||
+ | rules[" | ||
+ | rules[" | ||
+ | |||
+ | for left, right in rules.items(): | ||
+ | print(left, " | ||
+ | |||
+ | |||
+ | </ |
/var/www/cadia.ru.is/wiki/data/attic/public/t-malv-15-3/1.1534370312.txt.gz · Last modified: 2024/04/29 13:32 (external edit)