Presentation is loading. Please wait.

Presentation is loading. Please wait.

control 4 Day /01/14 LING 3820 & 6820 Natural Language Processing

Similar presentations


Presentation on theme: "control 4 Day /01/14 LING 3820 & 6820 Natural Language Processing"— Presentation transcript:

1 control 4 Day /01/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

2 Course organization http://www.tulane.edu/~howard/LING3820/
The syllabus is under construction. 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 29-Sept-2014

3 Review of control NLP, Prof. Howard, Tulane University 29-Sept-2014

4 Conditional expressions
>>> if True: ... do something ... ... elif True: ... else: NLP, Prof. Howard, Tulane University 29-Sept-2014

5 For loop & list conmprehension
>>> for item in container: ... do something to item ... >>> newList = [] ... newList.append(item) >>> newList = [item for item in container] NLP, Prof. Howard, Tulane University 29-Sept-2014

6 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 29-Sept-2014

7 Open Spyder NLP, Prof. Howard, Tulane University 29-Sept-2014

8 6.3.4. How to check a condition in a loop
NLP, Prof. Howard, Tulane University 29-Sept-2014

9 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 29-Sept-2014

10 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 29-Sept-2014

11 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] NLP, Prof. Howard, Tulane University 29-Sept-2014

12 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 29-Sept-2014

13 >>> upperList = [char.upper() for char in greeting]
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 29-Sept-2014

14 Next time Finish control NLP, Prof. Howard, Tulane University
29-Sept-2014


Download ppt "control 4 Day /01/14 LING 3820 & 6820 Natural Language Processing"

Similar presentations


Ads by Google