CONTROL 3 DAY /29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
Course organization 29-Sept-2014NLP, Prof. Howard, Tulane University 2 The syllabus is under construction. Chapter numbering 3.7. How to deal with non-English characters 3.7. How to deal with non-English characters 4.5. How to create a pattern with Unicode characters 4.5. How to create a pattern with Unicode characters 6. Control 6. Control
The quiz was the review. Review of control 29-Sept NLP, Prof. Howard, Tulane University
Open Spyder 29-Sept NLP, Prof. Howard, Tulane University
Chained conditionals 29-Sept NLP, Prof. Howard, Tulane University
Chained conditions Imagine that you wanted to check whether a character is lowercase. You would test for two conditions: whether it is lowercase or whether it is uppercase. But there are a lot of leftovers which are neither one – the punctuation. These three conditions are mutually exclusive, so they cannot be stated as three ifs. 29-Sept-2014NLP, Prof. Howard, Tulane University 6
A chained conditional expression 1. >>> char = 'Y' 2. >>> if char.islower(): print 'yes' elif char.isupper(): print 'no' else: print 'whoops!' no 29-Sept-2014NLP, Prof. Howard, Tulane University 7
In computer science, the programming construct for examining every item of a collection is called a loop. This could be every character in a string or every word in a list Iterating over the items of a collection with a loop 29-Sept NLP, Prof. Howard, Tulane University
How to examine every item with for As a simple example, consider printing every letter of a word: 1. >>> greeting = 'Yo!' 2. >>> for char in greeting: print char Y 6. o 7. ! 29-Sept-2014NLP, Prof. Howard, Tulane University 9
Iteration over a list 1. >>> fruit = ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 2. >>> for word in fruit: print word apple 6. cherry 7. mango 8. pear 9. watermelon 29-Sept-2014NLP, Prof. Howard, Tulane University 10
A trick for printing To save space in your Python console, adding a comma after the variable to be printed puts the output on the same line: 1. >>> for char in greeting: print char, Y o ! 5. >>> for word in fruit: print word, apple cherry mango pear watermelon 29-Sept-2014NLP, Prof. Howard, Tulane University 11
How to make a list during a loop with append() 1. >>> charlist = [] 2. >>> for char in greeting: charlist.append(char) >>> charlist 6. ['Y', 'o', '!'] 29-Sept-2014NLP, Prof. Howard, Tulane University 12
Doing the same with a list, redundantly 1. >>> wordlist = [] 2. >>> for word in fruit: wordlist.append(word) >>> wordlist 6. ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 29-Sept-2014NLP, Prof. Howard, Tulane University 13
How to pack a loop into a list with a list comprehension Creating a list from a loop is such a frequent task that Python has a breathtakingly elegant idiom for accomplishing it, the list comprehension. It consists of putting the whole for statement within square brackets, with the appending signaled by the brackets themselves. 29-Sept-2014NLP, Prof. Howard, Tulane University 14
List comprehension with string example 1. >>> charlist = [] 2. >>> for char in greeting: charlist.append(char) >>> charlist 6. ['Y', 'o', '!'] 7. >>> charlist = [char for char in greeting] 8. charlist 9. ['Y', 'o', '!'] 29-Sept-2014NLP, Prof. Howard, Tulane University 15
List comprehension with list example 1. >>> wordlist = [] 2. >>> for word in fruit: wordlist.append(word) >>> wordlist 6. ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 7. >>> wordlist = [word for word in fruit] 8. >>> wordlist 9. ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 29-Sept-2014NLP, Prof. Howard, Tulane University 16
The list comprehension in set theory The format of the list comprehension is inspired on a similar expression in set theory: {e | e ∈ F & P(e)} This is read as “the set of e’s such that e is an element of F and P of e (e has the property P)”. 29-Sept-2014NLP, Prof. Howard, Tulane University 17
How to check a condition in a loop The ultimate step in making a decision about a collection of items is to make membership in the output contingent on a condition: 1. >>> lowchar = [] 2. >>> for char in greeting: if char.islower(): lowchar.append(char) >>> lowchar 7. ['o'] 8. >>> lowchar = [char for char in greeting if char.islower()] 9. >>> lowchar 10. ['o'] 29-Sept-2014NLP, Prof. Howard, Tulane University 18
List example 1. >>> melonlist = [] 2. >>> for word in fruit: if word.endswith('melon'): melonlist.append(word) >>> melonlist 7. ['watermelon'] 8. >>> melonlist = [word for word in fruit if word.endswith('melon')] 9. ['watermelon'] 29-Sept-2014NLP, Prof. Howard, Tulane University 19
Chained conditions in a loop 1. >>> for char in greeting: if char.islower(): print 'yes' elif char.isupper(): print 'no' else: print 'whoops!'' no 10. yes 11. whoops! 29-Sept-2014NLP, Prof. Howard, Tulane University 20
Finish control Next time 29-Sept-2014NLP, Prof. Howard, Tulane University 21