140 likes | 233 Views
control 4 Day 16 - 10/01/14. LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University. Course organization. http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/CompCultEN/ Chapter numbering
E N D
control 4Day 16 - 10/01/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
Course organization • http://www.tulane.edu/~howard/LING3820/ • The syllabus is under construction. • http://www.tulane.edu/~howard/CompCultEN/ • Chapter numbering • 3.7. How to deal with non-English characters • 4.5. How to create a pattern with Unicode characters • 6. Control NLP, Prof. Howard, Tulane University
Review of control NLP, Prof. Howard, Tulane University
Conditional expressions • >>> if True: • ... do something • ... • >>> if True: • ... do something • ... elif True: • ... do something • ... else: • ... do something • ... NLP, Prof. Howard, Tulane University
For loop & list conmprehension • >>> for item in container: • ... do something to item • ... • >>> newList = [] • >>> for item in container: • ... newList.append(item) • ... • >>> newList = [item for item in container] NLP, Prof. Howard, Tulane University
Add a condition • >>> newList = [] • >>> for item in container: • ... if condition: • ... newList.append(item) • ... • >>> newList = [item for item in container if condition] NLP, Prof. Howard, Tulane University
Open Spyder NLP, Prof. Howard, Tulane University
6.3.4. How to check a condition in a loop NLP, Prof. Howard, Tulane University
Chained conditions in a loop >>> greeting = 'Yo!' >>> caseList = [] >>> for char in greeting: ... if char.islower(): ... caseList.append('yes') ... elif char.isupper(): ... caseList.append('no') ... else: ... caseList.append('whoops!') ... >>> caseList ['no', 'yes', 'whoops!'] NLP, Prof. Howard, Tulane University
A chained conditional list comprehension • However there is no list comprehension that is exactly analogous to a chained conditional, since elif is not allowed in them. • A list comprehension only allows if -- else, so the elif has to be decomposed into else -- if. • Here is what it looks like in a loop: NLP, Prof. Howard, Tulane University
Example • >>> caseList = [] • >>> for char in greeting: • ... if char.islower(): • ... caseList.append('yes') • ... else: • ... if char.isupper(): • ... caseList.append('no') • ... else: • ... caseList.append('whoops!') • ... • >>> caseList • ['no', 'yes', 'whoops!'] • >> caseList = ['yes' if char.islower() else 'no' if char.isupper() else 'whoops!' for char in greeting] • >>> caseList • ['no', 'yes', 'whoops!'] NLP, Prof. Howard, Tulane University
6.3.5. How to transform items within a loop • The argument of append() takes any type that can be an element of a list, such as strings or integers, so it can hold the result of a method: • >>> upperList = [] • >>> for char in greeting: • ... upperList.append(char.upper()) • ... • >>> upperList ['Y', 'O', '!'] • >>> lenList = [] • >>> for word in fruit: • ... lenList.append(len(word)) • ... • >>> lenList • [5, 6, 5, 4, 10] NLP, Prof. Howard, Tulane University
A list comprehension can perform the same change by applying it to the first mention of the item: • >>> upperList = [char.upper() for char in greeting] • >>> upperList • ['Y', 'O', '!'] • >>> lenList = [len(word) for word in fruit] • >>> lenList • [5, 6, 5, 4, 10] NLP, Prof. Howard, Tulane University
Next time Finish control NLP, Prof. Howard, Tulane University