210 likes | 336 Views
control 3 Day 15 - 9/29/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 3Day 15 - 9/29/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 The quiz was the review. NLP, Prof. Howard, Tulane University
Open Spyder NLP, Prof. Howard, Tulane University
6.2.1. Chained conditionals 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. NLP, Prof. Howard, Tulane University
A chained conditional expression • >>> char = 'Y' • >>> if char.islower(): • ... print 'yes' • ... elif char.isupper(): • ... print 'no' • ... else: • ... print 'whoops!' • ... • no NLP, Prof. Howard, Tulane University
6.3. Iterating over the items of a collection with a loop 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. NLP, Prof. Howard, Tulane University
6.3.1. How to examine every item with for • As a simple example, consider printing every letter of a word: • >>> greeting = 'Yo!' • >>> for char in greeting: • ... print char • ... • Y • o • ! NLP, Prof. Howard, Tulane University
Iteration over a list • >>> fruit = ['apple', 'cherry', 'mango', 'pear', 'watermelon'] • >>> for word in fruit: • ... print word • ... • apple • cherry • mango • pear • watermelon NLP, Prof. Howard, Tulane University
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: • >>> for char in greeting: • ... print char, • ... • Y o ! • >>> for word in fruit: • ... print word, • ... • apple cherry mango pear watermelon NLP, Prof. Howard, Tulane University
6.3.2. How to make a list during a loop with append() • >>> charlist = [] • >>> for char in greeting: • ... charlist.append(char) • ... • >>> charlist • ['Y', 'o', '!'] NLP, Prof. Howard, Tulane University
Doing the same with a list, redundantly • >>> wordlist = [] • >>> for word in fruit: • ... wordlist.append(word) • ... • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] NLP, Prof. Howard, Tulane University
6.3.3. 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. NLP, Prof. Howard, Tulane University
List comprehension with string example • >>> charlist = [] • >>> for char in greeting: • ... charlist.append(char) • ... • >>> charlist • ['Y', 'o', '!'] • >>> charlist = [char for char in greeting] • charlist • ['Y', 'o', '!'] NLP, Prof. Howard, Tulane University
List comprehension with list example • >>> wordlist = [] • >>> for word in fruit: • ... wordlist.append(word) • ... • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] • >>> wordlist = [word for word in fruit] • >>> wordlist • ['apple', 'cherry', 'mango', 'pear', 'watermelon'] NLP, Prof. Howard, Tulane University
6.3.4. 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)”. NLP, Prof. Howard, Tulane University
6.3.5. 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: • >>> lowchar = [] • >>> for char in greeting: • ... if char.islower(): • ... lowchar.append(char) • ... • >>> lowchar • ['o'] • >>> lowchar = [char for char in greeting if char.islower()] • >>> lowchar • ['o'] NLP, Prof. Howard, Tulane University
List example • >>> melonlist = [] • >>> for word in fruit: • ... if word.endswith('melon'): • ... melonlist.append(word) • ... • >>> melonlist • ['watermelon'] • >>> melonlist = [word for word in fruit if word.endswith('melon')] • ['watermelon'] NLP, Prof. Howard, Tulane University
Chained conditions in a loop • >>> for char in greeting: • ... if char.islower(): • ... print 'yes' • ... elif char.isupper(): • ... print 'no' • ... else: • ... print 'whoops!'' • ... • no • yes • whoops! NLP, Prof. Howard, Tulane University
Next time Finish control NLP, Prof. Howard, Tulane University