Download presentation
Presentation is loading. Please wait.
Published byRosamond O’Brien’ Modified over 8 years ago
1
CONTROL 3 DAY 15 - 9/29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
2
Course organization 29-Sept-2014NLP, Prof. Howard, Tulane University 2 http://www.tulane.edu/~howard/LING3820/ http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/CompCultEN/ http://www.tulane.edu/~howard/CompCultEN/ 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
3
The quiz was the review. Review of control 29-Sept-2014 3 NLP, Prof. Howard, Tulane University
4
Open Spyder 29-Sept-2014 4 NLP, Prof. Howard, Tulane University
5
6.2.1. Chained conditionals 29-Sept-2014 5 NLP, Prof. Howard, Tulane University
6
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
7
A chained conditional expression 1. >>> char = 'Y' 2. >>> if char.islower(): 3.... print 'yes' 4.... elif char.isupper(): 5.... print 'no' 6.... else: 7.... print 'whoops!' 8.... 9. no 29-Sept-2014NLP, Prof. Howard, Tulane University 7
8
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. 6.3. Iterating over the items of a collection with a loop 29-Sept-2014 8 NLP, Prof. Howard, Tulane University
9
6.3.1. 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: 3.... print char 4.... 5. Y 6. o 7. ! 29-Sept-2014NLP, Prof. Howard, Tulane University 9
10
Iteration over a list 1. >>> fruit = ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 2. >>> for word in fruit: 3.... print word 4.... 5. apple 6. cherry 7. mango 8. pear 9. watermelon 29-Sept-2014NLP, Prof. Howard, Tulane University 10
11
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: 2.... print char, 3.... 4. Y o ! 5. >>> for word in fruit: 6.... print word, 7.... 8. apple cherry mango pear watermelon 29-Sept-2014NLP, Prof. Howard, Tulane University 11
12
6.3.2. How to make a list during a loop with append() 1. >>> charlist = [] 2. >>> for char in greeting: 3.... charlist.append(char) 4.... 5. >>> charlist 6. ['Y', 'o', '!'] 29-Sept-2014NLP, Prof. Howard, Tulane University 12
13
Doing the same with a list, redundantly 1. >>> wordlist = [] 2. >>> for word in fruit: 3.... wordlist.append(word) 4.... 5. >>> wordlist 6. ['apple', 'cherry', 'mango', 'pear', 'watermelon'] 29-Sept-2014NLP, Prof. Howard, Tulane University 13
14
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. 29-Sept-2014NLP, Prof. Howard, Tulane University 14
15
List comprehension with string example 1. >>> charlist = [] 2. >>> for char in greeting: 3.... charlist.append(char) 4.... 5. >>> charlist 6. ['Y', 'o', '!'] 7. >>> charlist = [char for char in greeting] 8. charlist 9. ['Y', 'o', '!'] 29-Sept-2014NLP, Prof. Howard, Tulane University 15
16
List comprehension with list example 1. >>> wordlist = [] 2. >>> for word in fruit: 3.... wordlist.append(word) 4.... 5. >>> 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
17
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)”. 29-Sept-2014NLP, Prof. Howard, Tulane University 17
18
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: 1. >>> lowchar = [] 2. >>> for char in greeting: 3.... if char.islower(): 4.... lowchar.append(char) 5.... 6. >>> 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
19
List example 1. >>> melonlist = [] 2. >>> for word in fruit: 3.... if word.endswith('melon'): 4.... melonlist.append(word) 5.... 6. >>> melonlist 7. ['watermelon'] 8. >>> melonlist = [word for word in fruit if word.endswith('melon')] 9. ['watermelon'] 29-Sept-2014NLP, Prof. Howard, Tulane University 19
20
Chained conditions in a loop 1. >>> for char in greeting: 2.... if char.islower(): 3.... print 'yes' 4.... elif char.isupper(): 5.... print 'no' 6.... else: 7.... print 'whoops!'' 8.... 9. no 10. yes 11. whoops! 29-Sept-2014NLP, Prof. Howard, Tulane University 20
21
Finish control Next time 29-Sept-2014NLP, Prof. Howard, Tulane University 21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.